Skip to content

Commit

Permalink
handle circular references
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Oct 25, 2024
1 parent 3305c37 commit 18b5d80
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/yamler/yamler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ export default class YamlER {
if (!this.parentFile) {
// expressions can return an expression or reference an attribute wiith an expression later in the
// object that hasn't been evaluated yet so we parse expressions in a loop until there are no more
while (await this.parseAllExpressions())
;
const maxIterations = 100 // prevent infinite loops
let iterations = 0

while (await this.parseAllExpressions()) {
if (++iterations >= maxIterations)
throw new Error(`Maximum expression parsing iterations (${maxIterations}) exceeded. Possible circular reference in expressions.`)
}
}

return this.attributes
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/circular_expressions.staxfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
stax:
vars:
value1: ${{ get stax.vars.value2 }}
value2: ${{ get stax.vars.value1 }}
7 changes: 7 additions & 0 deletions tests/unit/yamler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ describe('YamlER', () => {
it('handles expressions that return an expression', () => {
expect(yaml.stax.vars.value5).toBe('some_service')
})

it('throws an error when expressions have circular references', async () => {
const circularYaml = resolve(fixturesDir, 'circular_expressions.staxfile')
const promise = loadFile(circularYaml, expressionCallback)

await expect(promise).rejects.toThrow('Maximum expression parsing iterations (100) exceeded. Possible circular reference in expressions.')
})
})

0 comments on commit 18b5d80

Please sign in to comment.