Skip to content
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

Token limit fix CVE-2023-49559 #291

Merged
merged 7 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"fmt"
"strconv"

"github.com/vektah/gqlparser/v2/ast"
Expand All @@ -20,6 +21,9 @@ type parser struct {

comment *ast.CommentGroup
commentConsuming bool

tokenCount int
maxTokenLimit int
}
StevenACoffman marked this conversation as resolved.
Show resolved Hide resolved

func (p *parser) consumeComment() (*ast.Comment, bool) {
Expand Down Expand Up @@ -95,6 +99,12 @@ func (p *parser) next() lexer.Token {
if p.err != nil {
return p.prev
}
// Increment the token count before reading the next token
p.tokenCount++
if p.tokenCount > p.maxTokenLimit {
StevenACoffman marked this conversation as resolved.
Show resolved Hide resolved
p.err = fmt.Errorf("exceeded token limit of %d", p.maxTokenLimit)
return p.prev
}
if p.peeked {
p.peeked = false
p.comment = nil
Expand Down
5 changes: 4 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,8 @@ func TestParserUtils(t *testing.T) {
}

func newParser(input string) parser {
return parser{lexer: lexer.New(&ast.Source{Input: input, Name: "input.graphql"})}
return parser{
lexer: lexer.New(&ast.Source{Input: input, Name: "input.graphql"}),
maxTokenLimit: 15000, // 15000 is the default value
}
}
5 changes: 3 additions & 2 deletions parser/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package parser

import (
"github.com/vektah/gqlparser/v2/lexer"

//nolint:revive
. "github.com/vektah/gqlparser/v2/ast"
)

func ParseQuery(source *Source) (*QueryDocument, error) {
p := parser{
lexer: lexer.New(source),
lexer: lexer.New(source),
maxTokenLimit: 15000, // 15000 is the default value
StevenACoffman marked this conversation as resolved.
Show resolved Hide resolved
}
return p.parseQueryDocument(), p.err
}
Expand Down Expand Up @@ -321,6 +321,7 @@ func (p *parser) parseDirectives(isConst bool) []*Directive {
break
}
directives = append(directives, p.parseDirective(isConst))

}
return directives
}
Expand Down
3 changes: 2 additions & 1 deletion parser/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func ParseSchemas(inputs ...*Source) (*SchemaDocument, error) {

func ParseSchema(source *Source) (*SchemaDocument, error) {
p := parser{
lexer: lexer.New(source),
lexer: lexer.New(source),
maxTokenLimit: 15000, // default value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be 0 too I think? especially since parsing a schema is usually if anything the safer operation

}
sd, err := p.parseSchemaDocument(), p.err
if err != nil {
Expand Down
Loading