Skip to content

Commit 318f5b6

Browse files
fix(api): escape $regex value and scope filter allowlist to mongo fields
The contains operator on the mongo store passed the caller-supplied string straight to $regex, letting metacharacters match unintended patterns. Escape the value with regexp.QuoteMeta so only literal substring matching is performed. Drop PG-native flat field names from the device filter allowlist since the mongo schema only exposes the nested paths (identity.mac, info.platform).
1 parent 0b773b9 commit 318f5b6

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

api/services/device.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ const StatusAccepted = "accepted"
2424
var DeviceFilterFields = query.NewFieldConstraints(map[string][]string{
2525
"name": {"contains", "eq", "ne"},
2626
"status": {"eq", "ne"},
27-
"mac": {"contains", "eq", "ne"},
2827
"identity.mac": {"contains", "eq", "ne"},
29-
"platform": {"contains", "eq", "ne"},
3028
"info.platform": {"contains", "eq", "ne"},
3129
"tags.name": {"contains", "eq"},
3230
"online": {"bool", "eq"},

api/store/mongo/internal/filters.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package internal
33
import (
44
"errors"
55
"fmt"
6+
"regexp"
67
"strconv"
78
"time"
89

@@ -57,10 +58,12 @@ func ParseFilterProperty(fp *query.FilterProperty) (bson.M, bool, error) {
5758
}
5859

5960
// fromContains converts a "contains" JSON expression to a Bson expression using "$regex" or "$all".
61+
// String values are escaped with [regexp.QuoteMeta] so metacharacters supplied by the caller
62+
// cannot be used to match unintended patterns or build catastrophic regexes.
6063
func fromContains(value interface{}) (bson.M, error) {
61-
switch value.(type) {
64+
switch v := value.(type) {
6265
case string:
63-
return bson.M{"$regex": value, "$options": "i"}, nil
66+
return bson.M{"$regex": regexp.QuoteMeta(v), "$options": "i"}, nil
6467
case []interface{}:
6568
return bson.M{"$all": value}, nil
6669
}

0 commit comments

Comments
 (0)