- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.5k
feat: add YAML anchors and aliases support to ClusterConfig #8535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            guessi
  wants to merge
  1
  commit into
  eksctl-io:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
guessi:feature/yaml-anchor-support
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +406
        
        
          −2
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -212,18 +212,100 @@ func newAWSProvider(spec *api.ProviderConfig, configurationLoader AWSConfigurati | |
| return provider, nil | ||
| } | ||
|  | ||
| // resolveYAMLAnchors processes YAML anchors and aliases, returning clean YAML | ||
| // that can be safely parsed by strict unmarshaling. It removes the "aliases" | ||
| // field commonly used for anchor definitions as it's not part of ClusterConfig schema. | ||
| func resolveYAMLAnchors(data []byte) ([]byte, error) { | ||
| // Security: Limit input size to prevent memory exhaustion attacks | ||
| const maxInputSize = 1024 * 1024 // 1MB limit | ||
| if len(data) > maxInputSize { | ||
| return nil, fmt.Errorf("YAML input too large: %d bytes exceeds limit of %d bytes", len(data), maxInputSize) | ||
| } | ||
|  | ||
| // Security: Check for excessive nesting depth to prevent stack overflow | ||
| const maxNestingDepth = 10 | ||
| if nestingDepth := countNestingDepth(data); nestingDepth > maxNestingDepth { | ||
| return nil, fmt.Errorf("YAML nesting too deep: %d levels exceeds limit of %d", nestingDepth, maxNestingDepth) | ||
| } | ||
|  | ||
| // Resolve YAML anchors and aliases by unmarshaling to interface{} first. | ||
| // This step processes any YAML anchors (&anchor) and aliases (*alias) in the input, | ||
| // expanding them to their full values. | ||
| var resolved interface{} | ||
| if err := yaml.Unmarshal(data, &resolved); err != nil { | ||
| return nil, err | ||
| } | ||
|  | ||
| // Marshal back to get resolved YAML without anchors/aliases | ||
| resolvedData, err := yaml.Marshal(resolved) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|  | ||
| // Security: Check for excessive expansion (YAML bomb protection) | ||
| const maxExpansionRatio = 10 | ||
| if len(resolvedData) > len(data)*maxExpansionRatio { | ||
| return nil, fmt.Errorf("YAML expansion too large: %d bytes expanded from %d bytes (ratio: %d, limit: %d)", | ||
| len(resolvedData), len(data), len(resolvedData)/len(data), maxExpansionRatio) | ||
| } | ||
|  | ||
| // Remove the "aliases" field commonly used for YAML anchor definitions | ||
| // as it's not part of the ClusterConfig schema | ||
| var temp map[string]interface{} | ||
| if err := yaml.Unmarshal(resolvedData, &temp); err != nil { | ||
| return nil, err | ||
| } | ||
|  | ||
| // Remove only the aliases field, maintaining strict validation for everything else | ||
| delete(temp, "aliases") | ||
| 
      Comment on lines
    
      +259
     to 
      +260
    
   There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the implementation to support special "aliases" section only. | ||
|  | ||
| // Marshal back to clean YAML | ||
| return yaml.Marshal(temp) | ||
| } | ||
|  | ||
| // countNestingDepth estimates YAML nesting depth by counting indentation | ||
| func countNestingDepth(data []byte) int { | ||
| lines := strings.Split(string(data), "\n") | ||
| maxDepth := 0 | ||
| for _, line := range lines { | ||
| if strings.TrimSpace(line) == "" || strings.HasPrefix(strings.TrimSpace(line), "#") { | ||
| continue | ||
| } | ||
| depth := 0 | ||
| for _, char := range line { | ||
| if char == ' ' { | ||
| depth++ | ||
| } else if char == '\t' { | ||
| depth += 2 // Count tabs as 2 spaces | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
| if depth/2 > maxDepth { // Assuming 2-space indentation | ||
| maxDepth = depth / 2 | ||
| } | ||
| } | ||
| return maxDepth | ||
| } | ||
|  | ||
| // ParseConfig parses data into a ClusterConfig | ||
| func ParseConfig(data []byte) (*api.ClusterConfig, error) { | ||
| // Resolve YAML anchors and aliases before parsing | ||
| cleanData, err := resolveYAMLAnchors(data) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|  | ||
| // strict mode is not available in runtime.Decode, so we use the parser | ||
| // directly; we don't store the resulting object, this is just the means | ||
| // of detecting any unknown keys | ||
| // NOTE: we must use sigs.k8s.io/yaml, as it behaves differently from | ||
| // github.com/ghodss/yaml, which didn't handle nested structs well | ||
| if err := yaml.UnmarshalStrict(data, &api.ClusterConfig{}); err != nil { | ||
| if err := yaml.UnmarshalStrict(cleanData, &api.ClusterConfig{}); err != nil { | ||
| return nil, err | ||
| } | ||
|  | ||
| obj, err := runtime.Decode(scheme.Codecs.UniversalDeserializer(), data) | ||
| obj, err := runtime.Decode(scheme.Codecs.UniversalDeserializer(), cleanData) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|  | ||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YAML file over 1MB should be considered as abnormal, any concern?