Skip to content

Commit 6aafbef

Browse files
authored
Detect recursion in aliases (#835)
1 parent 9d85601 commit 6aafbef

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

pkg/parser/node/yaml.go

+8
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ func convert(
175175
case yaml.ScalarNode:
176176
result.Value = &ScalarValue{Value: yamlNode.Value}
177177
case yaml.AliasNode:
178+
// test for recursion
179+
for aParent := parent; aParent != nil; aParent = aParent.Parent {
180+
// If we've found a parent with the same anchor, it means we have a recursion
181+
if aParent.YAMLNode != nil && aParent.YAMLNode.Anchor == yamlNode.Value {
182+
return nil, parsererror.NewRich(yamlNode.Line, yamlNode.Column, "recursive alias '%s'", yamlNode.Value)
183+
}
184+
}
185+
178186
// YAML aliases generally don't need line and column helper values
179187
// since they are merged into some other data structure afterwards
180188
// and this helps to find bugs easier in the future

pkg/parser/parser_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,8 @@ func TestRichErrors(t *testing.T) {
618618
"expected a scalar value or a list with scalar values")},
619619
{"testdata/rich-errors-matrix.yml", parsererror.NewRich(3, 5,
620620
"matrix can be defined only under a task, docker_builder or pipe")},
621+
{"testdata/rich-errors-recursion.yml", parsererror.NewRich(6, 9,
622+
"recursive alias 'RECURSION'")},
621623
}
622624

623625
for _, testCase := range testCases {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
container:
2+
image: debian:latest
3+
4+
recursion: &RECURSION
5+
env:
6+
<<: *RECURSION
7+
8+
task:
9+
script: true

0 commit comments

Comments
 (0)