Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

topdown/json: Fix panic in json.filter on empty JSON paths. #5200

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions topdown/json.go
Expand Up @@ -203,6 +203,11 @@ func pathsToObject(paths []ast.Ref) ast.Object {
node := root
var done bool

// If it's a null JSON path, skip all further processing.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, but it's an empty string, not null.. right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I wrote that, I was thinking about how the empty string in the list of paths-to-be-filtered is translated into a blank ast.Ref, which is basically a null/nil value. I'll embellish the comment with more details.

if len(path) == 0 {
done = true
}

for i := 0; i < len(path)-1 && !done; i++ {

k := path[i]
Expand Down
10 changes: 10 additions & 0 deletions topdown/json_test.go
Expand Up @@ -16,6 +16,11 @@ func TestFiltersToObject(t *testing.T) {
filters []string
expected string
}{
{
note: "empty path",
filters: []string{`""`},
expected: `{}`,
},
{
note: "base",
filters: []string{`"a/b/c"`},
Expand Down Expand Up @@ -81,6 +86,11 @@ func TestFiltersToObject(t *testing.T) {
filters: []string{`"a/~0b~1c/d~1~0"`},
expected: `{"a": {"~b/c": {"d/~": null}}}`,
},
{
note: "empty strings mixed with normal paths",
filters: []string{`"a/b/c"`, `""`, `"a/b/d"`, `"a/e/f"`, `""`},
expected: `{"a": {"b": {"c": null, "d": null}, "e": {"f": null}}}`,
},
}

for _, tc := range cases {
Expand Down