Skip to content

Commit

Permalink
ast/parser: guard against invalid domains for "some" and "every" (#4548)
Browse files Browse the repository at this point in the history
These would cause a runtime exception when attempting to parse

    some internal.member_2()

This is due to a technicality in how

    some x in xs

is parsed: `x in xs` will first become `internal.member_2(x, xs)`,
and `some internal.member_2(x, xs)` is then further processed. The
assumption that there're always two (resp. three for internal.member_3)
arguments won't hold if a snippet like the one above is fed into
the parser.

Thanks to Norbert Szetei of Doyensec, @doyensec, for reporting this.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
  • Loading branch information
srenatus committed Apr 5, 2022
1 parent 1af4428 commit e9d3828
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
19 changes: 18 additions & 1 deletion ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,16 @@ func (p *Parser) parseSome() *Expr {
if term := p.parseTermInfixCall(); term != nil {
if call, ok := term.Value.(Call); ok {
switch call[0].String() {
case Member.Name, MemberWithKey.Name: // OK
case Member.Name:
if len(call) != 3 {
p.illegal("illegal domain")
return nil
}
case MemberWithKey.Name:
if len(call) != 4 {
p.illegal("illegal domain")
return nil
}
default:
p.illegal("expected `x in xs` or `x, y in xs` expression")
return nil
Expand Down Expand Up @@ -972,9 +981,17 @@ func (p *Parser) parseEvery() *Expr {
}
switch call[0].String() {
case Member.Name: // x in xs
if len(call) != 3 {
p.illegal("illegal domain")
return nil
}
qb.Value = call[1]
qb.Domain = call[2]
case MemberWithKey.Name: // k, v in xs
if len(call) != 4 {
p.illegal("illegal domain")
return nil
}
qb.Key = call[1]
qb.Value = call[2]
qb.Domain = call[3]
Expand Down
6 changes: 6 additions & 0 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,10 @@ func TestSomeDeclExpr(t *testing.T) {
},
With: []*With{{Value: ArrayTerm(), Target: NewTerm(MustParseRef("input"))}},
}, opts)

assertParseErrorContains(t, "invalid domain (internal.member_2)", "some internal.member_2()", "illegal domain", opts)
assertParseErrorContains(t, "invalid domain (internal.member_3)", "some internal.member_3()", "illegal domain", opts)

}

func TestEvery(t *testing.T) {
Expand Down Expand Up @@ -851,6 +855,8 @@ func TestEvery(t *testing.T) {
"unexpected ident token: expected \\n or ; or }\n\tevery x\n", // this asserts that the tail of the error message doesn't contain a hint
)

assertParseErrorContains(t, "invalid domain (internal.member_2)", "every internal.member_2()", "illegal domain", opts)
assertParseErrorContains(t, "invalid domain (internal.member_3)", "every internal.member_3()", "illegal domain", opts)
}

func TestNestedExpressions(t *testing.T) {
Expand Down

0 comments on commit e9d3828

Please sign in to comment.