Skip to content

Commit 6b8d4c7

Browse files
authored
Merge commit from fork
fix: ejson walker comment state scoping
2 parents 9150dfc + 57f1930 commit 6b8d4c7

3 files changed

Lines changed: 49 additions & 13 deletions

File tree

‎ejson_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ func TestEncryptFileInPlace(t *testing.T) {
108108
So(match.Find(output), ShouldNotBeNil)
109109
})
110110
})
111+
112+
Convey("called with a nested underscore key followed by an array value", func() {
113+
plaintext := []byte(`{"_public_key": "` + validPubKey + `", "a": [{"_b": "c"}, ["d"]]}`)
114+
setData(tempFileName, plaintext)
115+
116+
_, err := EncryptFileInPlace(tempFileName)
117+
So(err, ShouldBeNil)
118+
encrypted, err := os.ReadFile(tempFileName)
119+
So(err, ShouldBeNil)
120+
So(string(encrypted), ShouldNotContainSubstring, `"d"`)
121+
122+
decrypted, err := DecryptFile(tempFileName, tempDir, validPrivKey)
123+
So(err, ShouldBeNil)
124+
So(decrypted, ShouldResemble, plaintext)
125+
})
111126
})
112127
}
113128

‎json/walker.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func (ew *Walker) Walk(data []byte) ([]byte, error) {
9999
inLiteral bool
100100
literalStart int
101101
isComment bool
102+
commentStack []bool
102103
scanner json.Scanner
103104
)
104105
scanner.Reset()
@@ -117,6 +118,15 @@ func (ew *Walker) Walk(data []byte) ([]byte, error) {
117118
inLiteral = false
118119
isComment = data[literalStart+1] == '_'
119120
pline.appendBytes(data[literalStart:i])
121+
case json.ScanBeginObject:
122+
// Underscore keys do not apply to values inside an object, but remember
123+
// the enclosing state so it is restored when the object closes.
124+
commentStack = append(commentStack, isComment)
125+
isComment = false
126+
case json.ScanBeginArray:
127+
// Array elements are part of their referencing value, so they inherit
128+
// its comment state until the array closes.
129+
commentStack = append(commentStack, isComment)
120130
case json.ScanError:
121131
// Some error happened; just bail.
122132
pline.flush()
@@ -144,6 +154,12 @@ func (ew *Walker) Walk(data []byte) ([]byte, error) {
144154
pline.appendPromise(res)
145155
}
146156
}
157+
// A container-close event may also terminate the preceding literal, so
158+
// restore the enclosing comment state only after handling that literal.
159+
if v == json.ScanEndObject || v == json.ScanEndArray {
160+
isComment = commentStack[len(commentStack)-1]
161+
commentStack = commentStack[:len(commentStack)-1]
162+
}
147163
}
148164
if !inLiteral {
149165
// If we're in a literal, we save up bytes because we may have to encrypt

‎json/walker_test.go‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,24 @@ type testCase struct {
3535

3636
// "E" means encrypted.
3737
var walkTestCases = []testCase{
38-
{`{"a": "b"}`, `{"a": "E"}`}, // encryption
39-
{`{"a" : "b"}`, `{"a" : "E"}`}, // weird spacing
40-
{` { "a" :"b" } `, ` { "a" :"E" } `}, // trailing spaces
41-
{`{"a": "b"}` + "\n", `{"a": "E"}` + "\n"}, // trailing newline
42-
{`{"_a": "b"}`, `{"_a": "b"}`}, // commenting
43-
{`{"a": "b", "c": "d"}`, `{"a": "E", "c": "E"}`}, // order-dependence
44-
{`{"a": 1}`, `{"a": 1}`}, // numbers
45-
{`{"a": true}`, `{"a": true}`}, // booleans
46-
{`{"a": ["b", "c"]}`, `{"a": ["E", "E"]}`}, // encrypting arrays
47-
{`{"_a": ["b", "c"]}`, `{"_a": ["b", "c"]}`}, // commenting arrays
48-
{`{"a": {"b": "c"}}`, `{"a": {"b": "E"}}`}, // nesting
49-
{`{"a": {"_b": "c"}}`, `{"a": {"_b": "c"}}`}, // nested comment
50-
{`{"_a": {"b": "c"}}`, `{"_a": {"b": "E"}}`}, // comments don't inherit
38+
{`{"a": "b"}`, `{"a": "E"}`}, // encryption
39+
{`{"a" : "b"}`, `{"a" : "E"}`}, // weird spacing
40+
{` { "a" :"b" } `, ` { "a" :"E" } `}, // trailing spaces
41+
{`{"a": "b"}` + "\n", `{"a": "E"}` + "\n"}, // trailing newline
42+
{`{"_a": "b"}`, `{"_a": "b"}`}, // commenting
43+
{`{"a": "b", "c": "d"}`, `{"a": "E", "c": "E"}`}, // order-dependence
44+
{`{"a": 1}`, `{"a": 1}`}, // numbers
45+
{`{"a": true}`, `{"a": true}`}, // booleans
46+
{`{"a": ["b", "c"]}`, `{"a": ["E", "E"]}`}, // encrypting arrays
47+
{`{"_a": ["b", "c"]}`, `{"_a": ["b", "c"]}`}, // commenting arrays
48+
{`{"a": {"b": "c"}}`, `{"a": {"b": "E"}}`}, // nesting
49+
{`{"a": {"_b": "c"}}`, `{"a": {"_b": "c"}}`}, // nested comment
50+
{`{"a": [{"_b": "c"}, "d"]}`, `{"a": [{"_b": "c"}, "E"]}`}, // nested comment does not leak to a sibling
51+
{`{"a": [{"_b": "c"}, ["d"]]}`, `{"a": [{"_b": "c"}, ["E"]]}`}, // nested comment does not leak to a sibling array
52+
{`{"a": [{"_b": {}}, "d"]}`, `{"a": [{"_b": {}}, "E"]}`}, // empty nested object restores the enclosing state
53+
{`{"_a": [{"_b": "c"}, "d"]}`, `{"_a": [{"_b": "c"}, "d"]}`}, // enclosing array comment persists through a nested object
54+
{`{"_a": [{}, "d"]}`, `{"_a": [{}, "d"]}`}, // enclosing array comment persists through an empty object
55+
{`{"_a": {"b": "c"}}`, `{"_a": {"b": "E"}}`}, // comments don't inherit
5156
}
5257

5358
func TestQuoteBytes(t *testing.T) {

0 commit comments

Comments
 (0)