-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtrusted-json-set-fetch-response.ts
More file actions
380 lines (349 loc) · 12.3 KB
/
trusted-json-set-fetch-response.ts
File metadata and controls
380 lines (349 loc) · 12.3 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import {
logMessage,
getFetchData,
objectToString,
matchRequestProps,
jsonSetter,
jsonPath,
getPrunePath,
forgeResponse,
type FetchResource,
isPruningNeeded,
matchStackTrace,
resolveJsonSyntaxMode,
buildJsonPathExpression,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getRequestData,
getRequestProps,
parseMatchProps,
isValidParsedData,
getMatchPropsData,
getWildcardPropertyInChain,
shouldAbortInlineOrInjectedScript,
getNativeRegexpTest,
backupRegExpValues,
restoreRegExpValues,
isKeyInObject,
noopArray,
noopObject,
noopCallbackFunc,
noopFunc,
trueFunc,
falseFunc,
throwFunc,
noopPromiseReject,
noopPromiseResolve,
nativeIsNaN,
extractRegexAndReplacement,
getJsonSetValue,
jsonLineEdit,
hit,
parseJsonSetArgumentValue,
} from '../helpers';
import { type Source } from './scriptlets';
/* eslint-disable max-len */
/**
* @trustedScriptlet trusted-json-set-fetch-response
*
* @description
* Sets a property at the given path in the JSON response of a fetch call.
* If the path does not exist, it is created together with any missing intermediate objects.
*
* ### Syntax
*
* <!-- markdownlint-disable line-length -->
*
* ```text
* example.org#%#//scriptlet('trusted-json-set-fetch-response', propsPath, argumentValue[, requiredInitialProps[, propsToMatch[, stack[, mode[, verbose]]]]])
* ```
*
* <!-- markdownlint-enable line-length -->
*
* - `propsPath` — required, dot-separated path to the property to set.
* Supports wildcards `*` and `[]`, and value filtering with `.[=].value`.
* In `jsonpath` mode only single JSONPath prune expression is supported.
* - `argumentValue` — required, value to write at the target path.
* Supports the same constants, `json:{...}`, and `replace:/regex/replacement/` syntax
* as `trusted-json-set`.
* In `jsonpath` mode this argument may be omitted when `propsPath` already includes
* an inline mutation suffix such as `=` or `+=`.
* - `requiredInitialProps` — optional, space-separated list of property paths
* which must all be present for the modification to occur.
* - `propsToMatch` — optional, string of space-separated properties to match.
* Possible props:
* - string or regular expression for matching the URL passed to fetch call;
* - colon-separated pairs `name:value` for matching fetch init options.
* - `stack` — optional, string or regular expression that must match the current function call stack trace.
* - `mode` — optional, syntax mode selector.
* Supported values:
* - `legacy` — force the existing legacy path syntax
* - `jsonpath` — force JSONPath syntax
* If omitted, the scriptlet detects JSONPath automatically for clearly JSONPath-shaped expressions.
* - `verbose` — optional, if set to `true`, the scriptlet will log the original and modified JSON content.
*
* > Scriptlet does nothing if response body cannot be converted to JSON.
* > If the response is line-delimited JSON, each JSON line is processed independently.
*
* ### Examples
*
* <!-- markdownlint-disable line-length -->
*
* 1. Sets `ads.enabled` to `false` in the JSON response of any fetch call
*
* ```adblock
* example.org#%#//scriptlet('trusted-json-set-fetch-response', 'ads.enabled', 'false')
* ```
*
* or `JSONPath` syntax:
*
* ```adblock
* example.org#%#//scriptlet('trusted-json-set-fetch-response', '$.ads.enabled', 'false')
* ```
*
* 1. Creates `config.flags.blocked` path in matching fetch responses
*
* ```adblock
* example.org#%#//scriptlet('trusted-json-set-fetch-response', 'config.flags.blocked', 'true', '', 'api/config')
* ```
*
* or `JSONPath` syntax:
*
* ```adblock
* example.org#%#//scriptlet('trusted-json-set-fetch-response', '$.+={"config":{"flags":{"blocked":true}}}', '', '', 'api/config')
* ```
*
* 1. Merges a parsed JSON object into an existing response object property
*
* ```adblock
* example.org#%#//scriptlet('trusted-json-set-fetch-response', 'foo', 'json:{"a":{"test":1},"b":{"c":1}}')
* ```
*
* or `JSONPath` syntax:
*
* ```adblock
* example.org#%#//scriptlet('trusted-json-set-fetch-response', '$.foo', 'json:{"a":{"test":1},"b":{"c":1}}')
* ```
*
* 1. Replaces a value in the JSON response using a regular expression
*
* ```adblock
* example.org#%#//scriptlet('trusted-json-set-fetch-response', 'foo', 'replace:/advertisement/article/')
* ```
*
* or `JSONPath` syntax:
*
* ```adblock
* example.org#%#//scriptlet('trusted-json-set-fetch-response', '$.foo=replace({"regex":"advertisement","replacement":"article"})')
* ```
*
* <!-- markdownlint-enable line-length -->
*
* @added v2.3.0.
*/
/* eslint-enable max-len */
export function trustedJsonSetFetchResponse(
source: Source,
propsPath: string,
argumentValue: any,
requiredInitialProps = '',
propsToMatch = '',
stack = '',
mode = '',
verbose = '',
) {
const syntaxModeDetails = resolveJsonSyntaxMode(propsPath, mode);
const jsonPathExpression = syntaxModeDetails.mode === 'jsonpath'
? buildJsonPathExpression(propsPath, argumentValue)
: '';
if (!propsPath) {
return;
}
if (
typeof fetch === 'undefined'
|| typeof Proxy === 'undefined'
|| typeof Response === 'undefined'
) {
return;
}
if (syntaxModeDetails.mode === 'legacy' && argumentValue === undefined) {
return;
}
if (syntaxModeDetails.mode === 'jsonpath' && jsonPathExpression === '') {
logMessage(source, 'JSONPath mode requires argumentValue unless propsPath already contains an inline mutation');
return;
}
const shouldLogContent = verbose === 'true';
const nativeObjects = {
nativeFetch: window.fetch,
nativeParse: window.JSON.parse,
nativeStringify: window.JSON.stringify,
nativeRequestClone: window.Request.prototype.clone,
nativeResponseClone: window.Response.prototype.clone,
};
const parsedArgumentValue = syntaxModeDetails.mode === 'legacy'
? parseJsonSetArgumentValue(source, argumentValue, nativeObjects.nativeParse)
: null;
if (syntaxModeDetails.mode === 'legacy' && !parsedArgumentValue) {
return;
}
const parsedSetPaths = syntaxModeDetails.mode === 'legacy' ? getPrunePath(propsPath) : [];
const setPathObj = parsedSetPaths[0];
const requiredPaths = syntaxModeDetails.mode === 'legacy' ? getPrunePath(requiredInitialProps) : [];
const getValueToSet = (currentValue: any): any => {
if (!parsedArgumentValue) {
return currentValue;
}
return getJsonSetValue(currentValue, parsedArgumentValue);
};
const applyJsonMutation = (jsonValue: Record<string, any>) => {
if (syntaxModeDetails.mode === 'jsonpath') {
return jsonPath(source, jsonValue, jsonPathExpression, nativeObjects, () => hit(source), stack);
}
return jsonSetter(
source,
jsonValue,
setPathObj?.path || '',
setPathObj?.value,
getValueToSet,
requiredPaths,
stack,
nativeObjects,
);
};
// TODO: Consider to move it to helper and share it with json-prune-fetch-response scriptlet
const fetchHandlerWrapper = async (
target: typeof fetch,
thisArg: any,
args: [FetchResource, RequestInit],
): Promise<Response> => {
const fetchData = getFetchData(args, nativeObjects.nativeRequestClone);
if (!matchRequestProps(source, propsToMatch, fetchData)) {
return Reflect.apply(target, thisArg, args);
}
let originalResponse;
let clonedResponse;
try {
// eslint-disable-next-line prefer-spread
originalResponse = await nativeObjects.nativeFetch.apply(null, args);
clonedResponse = nativeObjects.nativeResponseClone.call(originalResponse);
} catch {
logMessage(source, `Could not make an original fetch request: ${fetchData.url}`);
return Reflect.apply(target, thisArg, args);
}
let textContent;
try {
textContent = await originalResponse.text();
} catch {
const message = `Response body can't be converted to text: ${objectToString(fetchData)}`;
logMessage(source, message);
return clonedResponse;
}
try {
const json = nativeObjects.nativeParse(textContent);
if (shouldLogContent) {
// eslint-disable-next-line max-len
logMessage(source, `Original content:\n${window.location.hostname}\n${nativeObjects.nativeStringify(json, null, 2)}\nStack trace:\n${new Error().stack || ''}`, true);
logMessage(source, json, true, false);
}
const modifiedJson = applyJsonMutation(json);
if (shouldLogContent) {
// eslint-disable-next-line max-len
logMessage(source, `Modified content:\n${window.location.hostname}\n${nativeObjects.nativeStringify(modifiedJson, null, 2)}\nStack trace:\n${new Error().stack || ''}`, true);
logMessage(source, modifiedJson, true, false);
}
return forgeResponse(
originalResponse,
nativeObjects.nativeStringify(modifiedJson),
);
} catch {
// If response body is not a single JSON document, try to process it as line-delimited JSON
}
try {
const lineEditResult = jsonLineEdit(
(parsedLine) => applyJsonMutation(parsedLine),
nativeObjects,
textContent,
);
if (lineEditResult.hasJsonLines) {
if (shouldLogContent) {
// eslint-disable-next-line max-len
logMessage(source, `Original content:\n${window.location.hostname}\n${textContent}\nStack trace:\n${new Error().stack || ''}`, true);
// eslint-disable-next-line max-len
logMessage(source, `Modified content:\n${window.location.hostname}\n${lineEditResult.text}\nStack trace:\n${new Error().stack || ''}`, true);
}
return forgeResponse(originalResponse, lineEditResult.text);
}
const message = `Response body can't be converted to json: ${objectToString(fetchData)}`;
logMessage(source, message);
return clonedResponse;
} catch (error) {
const message = `Response body can't be converted to json: ${objectToString(fetchData)}`;
logMessage(source, message);
return clonedResponse;
}
};
const getWrapper = (target: typeof fetch, propName: string, receiver: any) => {
if (propName === 'toString') {
return target.toString.bind(target);
}
return Reflect.get(target, propName, receiver);
};
const fetchHandler = {
apply: fetchHandlerWrapper,
get: getWrapper,
};
window.fetch = new Proxy(window.fetch, fetchHandler);
}
export const trustedJsonSetFetchResponseNames = [
'trusted-json-set-fetch-response',
// trusted scriptlets support no aliases
];
// eslint-disable-next-line prefer-destructuring
trustedJsonSetFetchResponse.primaryName = trustedJsonSetFetchResponseNames[0];
trustedJsonSetFetchResponse.injections = [
logMessage,
getFetchData,
objectToString,
matchRequestProps,
jsonSetter,
jsonPath,
getPrunePath,
forgeResponse,
isPruningNeeded,
matchStackTrace,
resolveJsonSyntaxMode,
buildJsonPathExpression,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getRequestData,
getRequestProps,
parseMatchProps,
isValidParsedData,
getMatchPropsData,
getWildcardPropertyInChain,
shouldAbortInlineOrInjectedScript,
getNativeRegexpTest,
backupRegExpValues,
restoreRegExpValues,
isKeyInObject,
noopArray,
noopObject,
noopCallbackFunc,
noopFunc,
trueFunc,
falseFunc,
throwFunc,
noopPromiseReject,
noopPromiseResolve,
nativeIsNaN,
extractRegexAndReplacement,
getJsonSetValue,
jsonLineEdit,
hit,
parseJsonSetArgumentValue,
];