-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson_rewrite.go
More file actions
220 lines (210 loc) · 6.35 KB
/
Copy pathjson_rewrite.go
File metadata and controls
220 lines (210 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"bytes"
"encoding/json"
"fmt"
)
type jsonStringEdit struct {
start int
end int
replacement []byte
}
type jsonStringRewriter struct {
body []byte
edits []jsonStringEdit
}
func rewriteJSONStrings(body []byte, original, redacted any) ([]byte, error) {
rewriter := jsonStringRewriter{body: body}
position := 0
if err := rewriter.walkValue(&position, original, redacted); err != nil {
return nil, err
}
rewriter.skipSpace(&position)
if position != len(body) {
return nil, fmt.Errorf("privacy JSON rewrite stopped at byte %d of %d", position, len(body))
}
if len(rewriter.edits) == 0 {
return body, nil
}
var output bytes.Buffer
last := 0
for _, edit := range rewriter.edits {
if edit.start < last || edit.end < edit.start || edit.end > len(body) {
return nil, fmt.Errorf("privacy JSON rewrite produced invalid edit [%d:%d]", edit.start, edit.end)
}
output.Write(body[last:edit.start])
output.Write(edit.replacement)
last = edit.end
}
output.Write(body[last:])
return output.Bytes(), nil
}
func (r *jsonStringRewriter) walkValue(position *int, original, redacted any) error {
r.skipSpace(position)
if *position >= len(r.body) {
return fmt.Errorf("privacy JSON rewrite reached unexpected end of input")
}
switch r.body[*position] {
case '"':
originalString, originalOK := original.(string)
redactedString, redactedOK := redacted.(string)
if !originalOK || !redactedOK {
return fmt.Errorf("privacy JSON rewrite found string with incompatible semantic values")
}
start := *position
end, decoded, err := r.scanString(start)
if err != nil {
return err
}
if decoded != originalString {
return fmt.Errorf("privacy JSON rewrite string mismatch at byte %d", start)
}
if redactedString != originalString {
replacement, errMarshal := marshalJSON(redactedString)
if errMarshal != nil {
return errMarshal
}
r.edits = append(r.edits, jsonStringEdit{start: start, end: end, replacement: replacement})
}
*position = end
return nil
case '{':
originalMap, originalOK := original.(map[string]any)
redactedMap, redactedOK := redacted.(map[string]any)
if !originalOK || !redactedOK {
return fmt.Errorf("privacy JSON rewrite found object with incompatible semantic values")
}
return r.walkObject(position, originalMap, redactedMap)
case '[':
originalList, originalOK := original.([]any)
redactedList, redactedOK := redacted.([]any)
if !originalOK || !redactedOK || len(originalList) != len(redactedList) {
return fmt.Errorf("privacy JSON rewrite found array with incompatible semantic values")
}
return r.walkArray(position, originalList, redactedList)
default:
return r.skipPrimitive(position)
}
}
func (r *jsonStringRewriter) walkObject(position *int, original, redacted map[string]any) error {
(*position)++
r.skipSpace(position)
if *position < len(r.body) && r.body[*position] == '}' {
(*position)++
return nil
}
for {
r.skipSpace(position)
if *position >= len(r.body) || r.body[*position] != '"' {
return fmt.Errorf("privacy JSON rewrite expected object key at byte %d", *position)
}
end, key, err := r.scanString(*position)
if err != nil {
return err
}
*position = end
r.skipSpace(position)
if *position >= len(r.body) || r.body[*position] != ':' {
return fmt.Errorf("privacy JSON rewrite expected colon at byte %d", *position)
}
(*position)++
originalChild, originalExists := original[key]
redactedChild, redactedExists := redacted[key]
if !originalExists || !redactedExists {
return fmt.Errorf("privacy JSON rewrite could not resolve object key %q", key)
}
if err := r.walkValue(position, originalChild, redactedChild); err != nil {
return err
}
r.skipSpace(position)
if *position >= len(r.body) {
return fmt.Errorf("privacy JSON rewrite reached unexpected end of object")
}
switch r.body[*position] {
case ',':
(*position)++
case '}':
(*position)++
return nil
default:
return fmt.Errorf("privacy JSON rewrite expected object delimiter at byte %d", *position)
}
}
}
func (r *jsonStringRewriter) walkArray(position *int, original, redacted []any) error {
(*position)++
r.skipSpace(position)
if *position < len(r.body) && r.body[*position] == ']' {
(*position)++
return nil
}
for index := range original {
if err := r.walkValue(position, original[index], redacted[index]); err != nil {
return err
}
r.skipSpace(position)
if index+1 < len(original) {
if *position >= len(r.body) || r.body[*position] != ',' {
return fmt.Errorf("privacy JSON rewrite expected array delimiter at byte %d", *position)
}
(*position)++
continue
}
if *position >= len(r.body) || r.body[*position] != ']' {
return fmt.Errorf("privacy JSON rewrite expected array end at byte %d", *position)
}
(*position)++
return nil
}
return fmt.Errorf("privacy JSON rewrite found empty semantic array with non-empty JSON")
}
func (r *jsonStringRewriter) scanString(start int) (int, string, error) {
if start >= len(r.body) || r.body[start] != '"' {
return start, "", fmt.Errorf("privacy JSON rewrite expected string at byte %d", start)
}
for position := start + 1; position < len(r.body); position++ {
switch r.body[position] {
case '\\':
position++
if position >= len(r.body) {
return start, "", fmt.Errorf("privacy JSON rewrite found truncated escape at byte %d", position-1)
}
case '"':
end := position + 1
var decoded string
if err := json.Unmarshal(r.body[start:end], &decoded); err != nil {
return start, "", fmt.Errorf("privacy JSON rewrite decoded string at byte %d: %w", start, err)
}
return end, decoded, nil
}
}
return start, "", fmt.Errorf("privacy JSON rewrite found unterminated string at byte %d", start)
}
func (r *jsonStringRewriter) skipPrimitive(position *int) error {
start := *position
for *position < len(r.body) {
switch r.body[*position] {
case ' ', '\t', '\r', '\n', ',', ']', '}':
if *position == start {
return fmt.Errorf("privacy JSON rewrite expected primitive at byte %d", start)
}
return nil
default:
(*position)++
}
}
if *position == start {
return fmt.Errorf("privacy JSON rewrite expected primitive at byte %d", start)
}
return nil
}
func (r *jsonStringRewriter) skipSpace(position *int) {
for *position < len(r.body) {
switch r.body[*position] {
case ' ', '\t', '\r', '\n':
(*position)++
default:
return
}
}
}