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
#6673)

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 445993d commit b6b658b
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 @@ -368,9 +368,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 @@ -483,9 +480,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 @@ -2261,8 +2255,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 @@ -5238,3 +5238,13 @@ func TestFilterWithEmpty(t *testing.T) {
require.NoError(t, err)
require.Equal(t, gq.Query[0].Filter.Func.Args[0].Value, "")
}

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 b6b658b

Please sign in to comment.