Skip to content

Commit

Permalink
fix(gqlParser): Handle strings with only whitespace in parseID (#6615) (
Browse files Browse the repository at this point in the history
#6672)

ParseID function would panic if the input consists of only whitespace characters. This PR fixes it.
Fixes GRAPHQL-720

(cherry picked from commit d994cb3)

Conflicts:
	gql/parser_test.go
  • Loading branch information
Ibrahim Jarif authored Nov 2, 2020
1 parent a5c8b24 commit 2f3f980
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 4 additions & 7 deletions gql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,6 @@ func substituteVariables(gq *GraphQuery, vmap varMap) error {

idVal, ok := gq.Args["id"]
if ok && len(gq.UID) == 0 {
if idVal == "" {
return errors.Errorf("Id can't be empty")
}
uids, err := parseID(idVal)
if err != nil {
return err
Expand Down Expand Up @@ -475,9 +472,6 @@ func substituteVariablesFilter(f *FilterTree, vmap varMap) error {
if !ok {
return errors.Errorf("Couldn't find value for GraphQL variable: [%s]", v.Value)
}
if idVal.Value == "" {
return errors.Errorf("Id can't be empty")
}
uids, err := parseID(idVal.Value)
if err != nil {
return err
Expand Down Expand Up @@ -2253,8 +2247,11 @@ loop:
// Parses ID list. Only used for GraphQL variables.
// TODO - Maybe get rid of this by lexing individual IDs.
func parseID(val string) ([]uint64, error) {
var uids []uint64
val = x.WhiteSpace.Replace(val)
if val == "" {
return nil, errors.Errorf("ID can't be empty")
}
var uids []uint64
if val[0] != '[' {
uid, err := strconv.ParseUint(val, 0, 64)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions gql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5109,3 +5109,13 @@ func TestParseExpandFilterErr(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "expand is only compatible with type filters")
}

func TestEmptyId(t *testing.T) {
q := "query me($a: string) { q(func: uid($a)) { name }}"
r := Request{
Str: q,
Variables: map[string]string{"$a": " "},
}
_, err := Parse(r)
require.Error(t, err, "ID cannot be empty")
}

0 comments on commit 2f3f980

Please sign in to comment.