Skip to content

Commit 75ef3c5

Browse files
Fix whitespace sensitivity issues
Update tests
1 parent 76c508a commit 75ef3c5

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

edgraph/server.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,22 @@ func validateCondValue(cond string) error {
721721
}
722722

723723
lower := strings.ToLower(cond)
724-
if !strings.HasPrefix(lower, "@if(") && !strings.HasPrefix(lower, "@filter(") {
724+
if !strings.HasPrefix(lower, "@if") && !strings.HasPrefix(lower, "@filter") {
725725
return errors.Errorf("invalid cond value: must start with @if( or @filter(")
726726
}
727727

728+
// Strip the directive prefix and verify the remainder (after optional whitespace) starts with '('.
729+
prefix := "@if"
730+
if strings.HasPrefix(lower, "@filter") {
731+
prefix = "@filter"
732+
}
733+
rest := strings.TrimSpace(cond[len(prefix):])
734+
if len(rest) == 0 || rest[0] != '(' {
735+
return errors.Errorf("invalid cond value: must start with @if( or @filter(")
736+
}
737+
// Rebuild cond without the space so the paren-balancing logic works on the normalized form.
738+
cond = prefix + rest
739+
728740
openIdx := strings.Index(cond, "(")
729741
if openIdx == -1 {
730742
return errors.Errorf("invalid cond value: missing opening parenthesis")
@@ -781,6 +793,7 @@ var valVarRegexp = regexp.MustCompile(`^val\([a-zA-Z_][a-zA-Z0-9_.]*\)$`)
781793
// validateValObjectId checks that an ObjectId starting with "val(" is a well-formed
782794
// val(variableName) reference and contains no injected DQL syntax.
783795
func validateValObjectId(objectId string) error {
796+
objectId = strings.TrimSpace(objectId)
784797
if !valVarRegexp.MatchString(objectId) {
785798
return errors.Errorf("invalid val() reference in ObjectId: %q", objectId)
786799
}
@@ -792,6 +805,7 @@ var langTagRegexp = regexp.MustCompile(`^[a-zA-Z]+(-[a-zA-Z0-9]+)*$`)
792805

793806
// validateLangTag checks that a language tag contains only safe characters.
794807
func validateLangTag(lang string) error {
808+
lang = strings.TrimSpace(lang)
795809
if lang == "" {
796810
return nil
797811
}

edgraph/server_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ func TestValidateCondValue(t *testing.T) {
388388
`@if(not(eq(len(v), 0)))`,
389389
`@if(eq(name, "has (parens) inside"))`,
390390
`@filter(eq(len(v), 0))`,
391+
// Spaces between directive and opening paren should be allowed (issue #9687).
392+
`@if (eq(len(v), 0))`,
393+
`@if (eq(len(v), 0))`,
394+
`@filter (eq(len(v), 0))`,
395+
` @if ( NOT eq(len(RoutesId), 0) ) `,
391396
}
392397
for _, c := range valid {
393398
require.NoError(t, validateCondValue(c), "expected valid: %q", c)
@@ -430,6 +435,10 @@ func TestValidateValObjectId(t *testing.T) {
430435
"val(queryVariable)",
431436
"val(my_var_123)",
432437
"val(Amt)",
438+
// Leading/trailing whitespace should be tolerated.
439+
" val(v)",
440+
"val(v) ",
441+
" val(v) ",
433442
}
434443
for _, v := range valid {
435444
require.NoError(t, validateValObjectId(v), "expected valid: %q", v)
@@ -454,6 +463,10 @@ func TestValidateLangTag(t *testing.T) {
454463
"fr",
455464
"zh-Hans",
456465
"en-US",
466+
// Leading/trailing whitespace should be tolerated.
467+
" en",
468+
"en ",
469+
" en ",
457470
}
458471
for _, v := range valid {
459472
require.NoError(t, validateLangTag(v), "expected valid: %q", v)

0 commit comments

Comments
 (0)