Skip to content

Commit b546637

Browse files
authored
fix: ignore casing in ApplyJSONPatch deny list (#837)
1 parent 8c9dd01 commit b546637

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

jsonx/patch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func ApplyJSONPatch(p json.RawMessage, object interface{}, denyPaths ...string)
5353
return err
5454
}
5555

56-
denyPattern := fmt.Sprintf("{%s}", strings.Join(denyPaths, ","))
56+
denyPattern := fmt.Sprintf("{%s}", strings.ToLower(strings.Join(denyPaths, ",")))
5757
matcher, err := glob.Compile(denyPattern, '/')
5858
if err != nil {
5959
return err
@@ -68,7 +68,7 @@ func ApplyJSONPatch(p json.RawMessage, object interface{}, denyPaths ...string)
6868
if err != nil {
6969
return fmt.Errorf("error parsing patch operations: %v", err)
7070
}
71-
if matcher.Match(path) {
71+
if matcher.Match(strings.ToLower(path)) {
7272
return fmt.Errorf("patch includes denied path: %s", path)
7373
}
7474

jsonx/patch_test.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,19 @@ func TestApplyJSONPatch(t *testing.T) {
108108
require.Equal(t, expected, obj)
109109
})
110110
t.Run("case=patch denied path", func(t *testing.T) {
111-
rawPatch := []byte(`[{"op": "replace", "path": "/Field1", "value": "bar"}]`)
112-
obj := deepcopy.Copy(object).(TestType)
113-
require.Error(t, ApplyJSONPatch(rawPatch, &obj, "/Field1"))
114-
require.Equal(t, object, obj)
111+
for _, path := range []string{
112+
"/Field1",
113+
"/field1",
114+
"/fIeld1",
115+
"/FIELD1",
116+
} {
117+
t.Run("path="+path, func(t *testing.T) {
118+
rawPatch := []byte(`[{"op": "replace", "path": "/Field1", "value": "bar"}]`)
119+
obj := deepcopy.Copy(object).(TestType)
120+
assert.Error(t, ApplyJSONPatch(rawPatch, &obj, path))
121+
require.Equal(t, object, obj)
122+
})
123+
}
115124
})
116125
t.Run("case=patch denied sub-path", func(t *testing.T) {
117126
rawPatch := []byte(`[{"op": "replace", "path": "/Field3/Field1", "value": true}]`)

0 commit comments

Comments
 (0)