Skip to content

Commit 9bbe242

Browse files
authored
Merge pull request #41 from CiscoM31/strict-parsing
Disallow extraneous commas in $expand $select $orderby query options
2 parents 79f323b + 5efe396 commit 9bbe242

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

expand_parser.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ func ParseExpandItem(ctx context.Context, input tokenQueue) (*ExpandItem, error)
162162
item.Path = append(item.Path, queue.Dequeue())
163163
}
164164

165+
cfg, hasComplianceConfig := ctx.Value(odataCompliance).(OdataComplianceConfig)
166+
if !hasComplianceConfig {
167+
// Strict ODATA compliance by default.
168+
cfg = ComplianceStrict
169+
}
170+
171+
if len(item.Path) == 0 && cfg&ComplianceIgnoreInvalidComma == 0 {
172+
return nil, BadRequestError("Extra comma in $expand.")
173+
}
174+
165175
return item, nil
166176
}
167177

expand_parser_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,18 @@ func TestExpandNestedParens(t *testing.T) {
121121
return
122122
}
123123
}
124+
125+
func TestExpandNegativeCases(t *testing.T) {
126+
input := "Products," // Extraneous comma
127+
ctx := context.Background()
128+
output, err := ParseExpandString(ctx, input)
129+
130+
if err == nil {
131+
t.Error("Expected parsing to return error.")
132+
return
133+
}
134+
if output != nil {
135+
t.Error("Expected parsing to return nil output.")
136+
return
137+
}
138+
}

orderby_parser.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ func (p *ExpressionParser) ParseOrderByString(ctx context.Context, orderby strin
3131

3232
for _, v := range items {
3333
v = strings.TrimSpace(v)
34+
35+
cfg, hasComplianceConfig := ctx.Value(odataCompliance).(OdataComplianceConfig)
36+
if !hasComplianceConfig {
37+
// Strict ODATA compliance by default.
38+
cfg = ComplianceStrict
39+
}
40+
41+
if len(v) == 0 && cfg&ComplianceIgnoreInvalidComma == 0 {
42+
return nil, BadRequestError("Extra comma in $orderby.")
43+
}
44+
3445
var order string
3546
vLower := strings.ToLower(v)
3647
if strings.HasSuffix(vLower, " "+ASC) {

select_parser.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ func ParseSelectString(ctx context.Context, sel string) (*GoDataSelectQuery, err
1616
result := []*SelectItem{}
1717

1818
for _, item := range items {
19+
20+
cfg, hasComplianceConfig := ctx.Value(odataCompliance).(OdataComplianceConfig)
21+
if !hasComplianceConfig {
22+
// Strict ODATA compliance by default.
23+
cfg = ComplianceStrict
24+
}
25+
26+
if len(strings.TrimSpace(item)) == 0 && cfg&ComplianceIgnoreInvalidComma == 0 {
27+
return nil, BadRequestError("Extra comma in $select.")
28+
}
29+
1930
segments := []*Token{}
2031
for _, val := range strings.Split(item, "/") {
2132
segments = append(segments, &Token{Value: val})

url_parser_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,42 @@ func TestUnescapeStringTokens(t *testing.T) {
494494
expectedFilterTree: nil,
495495
expectedOrderBy: nil,
496496
},
497+
{
498+
url: "/Product?$orderby=Name,",
499+
errRegex: regexp.MustCompile(`Extra comma in \$orderby\.`),
500+
expectedFilterTree: nil,
501+
expectedOrderBy: nil,
502+
},
503+
{
504+
url: "/Product?$orderby=Name,,Count",
505+
errRegex: regexp.MustCompile(`Extra comma in \$orderby\.`),
506+
expectedFilterTree: nil,
507+
expectedOrderBy: nil,
508+
},
509+
{
510+
url: "/Product?$orderby=,Name",
511+
errRegex: regexp.MustCompile(`Extra comma in \$orderby\.`),
512+
expectedFilterTree: nil,
513+
expectedOrderBy: nil,
514+
},
515+
{
516+
url: "/Product?$select=Name,",
517+
errRegex: regexp.MustCompile(`Extra comma in \$select\.`),
518+
expectedFilterTree: nil,
519+
expectedOrderBy: nil,
520+
},
521+
{
522+
url: "/Product?$select=Name,,Count",
523+
errRegex: regexp.MustCompile(`Extra comma in \$select\.`),
524+
expectedFilterTree: nil,
525+
expectedOrderBy: nil,
526+
},
527+
{
528+
url: "/Product?$select=,Name",
529+
errRegex: regexp.MustCompile(`Extra comma in \$select\.`),
530+
expectedFilterTree: nil,
531+
expectedOrderBy: nil,
532+
},
497533
{
498534
url: "/Product?$compute=Price mul Quantity as TotalPrice",
499535
errRegex: nil,

0 commit comments

Comments
 (0)