Skip to content

Commit

Permalink
Detect recursion in aliases (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
fkorotkov authored Feb 6, 2025
1 parent 9d85601 commit 6aafbef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/parser/node/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ func convert(
case yaml.ScalarNode:
result.Value = &ScalarValue{Value: yamlNode.Value}
case yaml.AliasNode:
// test for recursion
for aParent := parent; aParent != nil; aParent = aParent.Parent {
// If we've found a parent with the same anchor, it means we have a recursion
if aParent.YAMLNode != nil && aParent.YAMLNode.Anchor == yamlNode.Value {
return nil, parsererror.NewRich(yamlNode.Line, yamlNode.Column, "recursive alias '%s'", yamlNode.Value)
}
}

// YAML aliases generally don't need line and column helper values
// since they are merged into some other data structure afterwards
// and this helps to find bugs easier in the future
Expand Down
2 changes: 2 additions & 0 deletions pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ func TestRichErrors(t *testing.T) {
"expected a scalar value or a list with scalar values")},
{"testdata/rich-errors-matrix.yml", parsererror.NewRich(3, 5,
"matrix can be defined only under a task, docker_builder or pipe")},
{"testdata/rich-errors-recursion.yml", parsererror.NewRich(6, 9,
"recursive alias 'RECURSION'")},
}

for _, testCase := range testCases {
Expand Down
9 changes: 9 additions & 0 deletions pkg/parser/testdata/rich-errors-recursion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
container:
image: debian:latest

recursion: &RECURSION
env:
<<: *RECURSION

task:
script: true

0 comments on commit 6aafbef

Please sign in to comment.