Skip to content

Commit

Permalink
scrub: fix edge case with nested objects in arrays
Browse files Browse the repository at this point in the history
In certain conditions the scrubbing would end up getting
out of sync with its position in the stream.

Thanks Dan Upton for reporting.
  • Loading branch information
eandre committed Nov 6, 2023
1 parent 025368d commit 2a5490a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pkg/scrub/scrub.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,26 @@ func (s *stream) processValue(nodes []node) {
}

func (s *stream) processArray(nodes []node) {
s.next() // Move to first array value
for {
s.next()
if s.tok == unknown || s.tok == arrayEnd {
if s.tok == unknown {
return
} else if s.tok == arrayEnd {
s.next()
return
}

s.processValue(nodes)
}
}

func (s *stream) processObject(nodes []node) {
s.next() // Move to the first key
for {
if s.tok == unknown || s.tok == objectEnd {
if s.tok == unknown {
return
} else if s.tok == objectEnd {
s.next()
return
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/scrub/scrub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ func TestJSON(t *testing.T) {
},
wantOutput: `{"One":["\"one\""]}}`,
},
{
name: "multiple_array_nested_obj",
input: `{"one": [{"two": {"three": "ABC"}}, {"two": {"three": "DEF"}}]}`,
paths: []Path{
[]PathEntry{
{Kind: ObjectField, FieldName: `"one"`, CaseSensitive: true},
{Kind: ObjectField, FieldName: `"two"`, CaseSensitive: true},
{Kind: ObjectField, FieldName: `"three"`, CaseSensitive: true},
},
},
wantOutput: `{"one": [{"two": {"three": "[sensitive]"}}, {"two": {"three": "[sensitive]"}}]}`,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 2a5490a

Please sign in to comment.