Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions gabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ func JSONPointerToSlice(path string) ([]string, error) {
}
hierarchy := strings.Split(path, "/")[1:]
for i, v := range hierarchy {
for j := 0; j < len(v); j++ {
if v[j] != '~' {
continue
}
if j+1 == len(v) || (v[j+1] != '0' && v[j+1] != '1') {
return nil, fmt.Errorf("failed to resolve JSON pointer: invalid escape in token %q", v)
}
j++
}
hierarchy[i] = r1.Replace(v)
}
return hierarchy, nil
Expand Down
26 changes: 26 additions & 0 deletions gabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1950,3 +1950,29 @@ func BenchmarkWildcardSearch(b *testing.B) {
val.Search([]string{"test", "*", "value"}...)
}
}

func TestJSONPointerInvalidEscapes(t *testing.T) {
for _, path := range []string{"/~", "/~2", "/foo~bar", "/~0~", "/a/~9/b"} {
t.Run(path, func(t *testing.T) {
if _, err := JSONPointerToSlice(path); err == nil {
t.Error("expected an invalid escape error")
}
container := New()
if _, err := container.SetJSONPointer("value", path); err == nil {
t.Error("SetJSONPointer accepted invalid syntax")
}
if got := container.String(); got != "{}" {
t.Errorf("invalid pointer modified the document: %s", got)
}
})
}
for path, want := range map[string][]string{
"/~0": {"~"}, "/~1": {"/"}, "/~01": {"~1"},
"/~00": {"~0"}, "/~0~1": {"~/"}, "/café/~01": {"café", "~1"},
} {
got, err := JSONPointerToSlice(path)
if err != nil || !reflect.DeepEqual(got, want) {
t.Errorf("JSONPointerToSlice(%q) = %q, %v; want %q", path, got, err, want)
}
}
}