-
Notifications
You must be signed in to change notification settings - Fork 30
/
json-prune-xhr-response.ts
374 lines (339 loc) · 13 KB
/
json-prune-xhr-response.ts
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
import {
hit,
logMessage,
toRegExp,
jsonPruner,
getPrunePath,
objectToString,
matchRequestProps,
getXhrData,
type XMLHttpRequestSharedRequestData,
isPruningNeeded,
matchStackTrace,
getMatchPropsData,
getRequestProps,
isValidParsedData,
parseMatchProps,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getWildcardPropertyInChain,
shouldAbortInlineOrInjectedScript,
getNativeRegexpTest,
backupRegExpValues,
restoreRegExpValues,
} from '../helpers';
import { type Source } from './scriptlets';
/**
* @scriptlet json-prune-xhr-response
*
* @description
* Removes specified properties from the JSON response of a `XMLHttpRequest` call.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/commit/3152896d428c54c76cfd66c3da110bd4d6506cbc
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('json-prune-xhr-response'[, propsToRemove[, obligatoryProps[, propsToMatch[, stack]]]])
* ```
*
* - `propsToRemove` — optional, string of space-separated properties to remove
* - `obligatoryProps` — optional, string of space-separated properties
* which must be all present for the pruning to occur
* - `propsToMatch` — optional, string of space-separated properties to match for extra condition; possible props:
* - string or regular expression for matching the URL passed to `XMLHttpRequest.open()` call;
* - colon-separated pairs `name:value` where
* - `name` — string or regular expression for matching XMLHttpRequest property name
* - `value` — string or regular expression for matching the value of the option
* passed to `XMLHttpRequest.open()` call
* - `stack` — optional, string or regular expression that must match the current function call stack trace;
* if regular expression is invalid it will be skipped
*
* > Note please that you can use wildcard `*` for chain property name,
* > e.g. `ad.*.src` instead of `ad.0.src ad.1.src ad.2.src`.
*
* > Usage with with only propsToMatch argument will log XMLHttpRequest calls to browser console.
* > It may be useful for debugging but it is not allowed for prod versions of filter lists.
*
* > Scriptlet does nothing if response body can't be converted to JSON.
*
* ### Examples
*
* 1. Removes property `example` from the JSON response of any XMLHttpRequest call
*
* ```adblock
* example.org#%#//scriptlet('json-prune-xhr-response', 'example')
* ```
*
* For instance, if the JSON response of a XMLHttpRequest call is:
*
* ```js
* {one: 1, example: true}
* ```
*
* then the response will be modified to:
*
* ```js
* {one: 1}
* ```
*
* 2. A property in a list of properties can be a chain of properties
*
* ```adblock
* example.org#%#//scriptlet('json-prune-xhr-response', 'a.b', 'ads.url.first')
* ```
*
* 3. Removes property `content.ad` from the JSON response of a XMLHttpRequest call if URL contains `content.json`
*
* ```adblock
* example.org#%#//scriptlet('json-prune-xhr-response', 'content.ad', '', 'content.json')
* ```
*
* 4. Removes property `content.ad` from the JSON response of a XMLHttpRequest call
* if its error stack trace contains `test.js`
*
* ```adblock
* example.org#%#//scriptlet('json-prune-xhr-response', 'content.ad', '', '', 'test.js')
* ```
*
* 5. A property in a list of properties can be a chain of properties with wildcard in it
*
* ```adblock
* example.org#%#//scriptlet('json-prune-xhr-response', 'content.*.media.src', 'content.*.media.ad')
* ```
*
* 6. Log all JSON responses of a XMLHttpRequest call
*
* ```adblock
* example.org#%#//scriptlet('json-prune-xhr-response')
* ```
*
* @added v1.10.25.
*/
interface CustomXMLHttpRequest extends XMLHttpRequest {
xhrShouldBePruned: boolean;
headersReceived: boolean;
collectedHeaders: string[];
}
export function jsonPruneXhrResponse(
source: Source,
propsToRemove: string,
obligatoryProps: string,
propsToMatch = '',
stack = '',
) {
// Do nothing if browser does not support Proxy (e.g. Internet Explorer)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
if (typeof Proxy === 'undefined') {
return;
}
const shouldLog = !propsToRemove && !obligatoryProps;
const prunePaths = getPrunePath(propsToRemove);
const requiredPaths = getPrunePath(obligatoryProps);
const nativeParse = window.JSON.parse;
const nativeStringify = window.JSON.stringify;
const nativeOpen = window.XMLHttpRequest.prototype.open;
const nativeSend = window.XMLHttpRequest.prototype.send;
const setRequestHeaderWrapper = (
setRequestHeader: typeof XMLHttpRequest.prototype.setRequestHeader,
thisArgument: CustomXMLHttpRequest,
argsList: any,
): void => {
// Collect headers
thisArgument.collectedHeaders.push(argsList);
return Reflect.apply(setRequestHeader, thisArgument, argsList);
};
const setRequestHeaderHandler = {
apply: setRequestHeaderWrapper,
};
let xhrData: XMLHttpRequestSharedRequestData<any>;
const openWrapper = (
target: typeof XMLHttpRequest.prototype.open,
thisArg: CustomXMLHttpRequest,
args: [method: string, url: string, async: string, user: string, password: string],
): void => {
// eslint-disable-next-line prefer-spread
xhrData = getXhrData.apply(null, args);
if (matchRequestProps(source, propsToMatch, xhrData) || shouldLog) {
thisArg.xhrShouldBePruned = true;
thisArg.headersReceived = !!thisArg.headersReceived;
}
// Trap setRequestHeader of target xhr object to mimic request headers later
if (thisArg.xhrShouldBePruned && !thisArg.headersReceived) {
thisArg.headersReceived = true;
thisArg.collectedHeaders = [];
// setRequestHeader can only be called on open xhr object,
// so we can safely proxy it here
thisArg.setRequestHeader = new Proxy(thisArg.setRequestHeader, setRequestHeaderHandler);
}
return Reflect.apply(target, thisArg, args);
};
const sendWrapper = (
target: typeof XMLHttpRequest.prototype.send,
thisArg: CustomXMLHttpRequest,
args: any,
): void => {
// Stack trace cannot be checked in jsonPruner helper,
// because in this case it returns stack trace of our script,
// so it has to be checked earlier
const stackTrace = new Error().stack || '';
if (!thisArg.xhrShouldBePruned || (stack && !matchStackTrace(stack, stackTrace))) {
return Reflect.apply(target, thisArg, args);
}
/**
* Create separate XHR request with original request's input
* to be able to collect response data without triggering
* listeners on original XHR object
*/
const forgedRequest = new XMLHttpRequest();
forgedRequest.addEventListener('readystatechange', () => {
if (forgedRequest.readyState !== 4) {
return;
}
const {
readyState,
response,
responseText,
responseURL,
responseXML,
status,
statusText,
} = forgedRequest;
// Extract content from response
const content = responseText || response;
if (typeof content !== 'string' && typeof content !== 'object') {
return;
}
let modifiedContent;
if (typeof content === 'string') {
try {
const jsonContent = nativeParse(content);
if (shouldLog) {
// eslint-disable-next-line max-len
logMessage(source, `${window.location.hostname}\n${nativeStringify(jsonContent, null, 2)}\nStack trace:\n${stackTrace}`, true);
logMessage(source, jsonContent, true, false);
modifiedContent = content;
} else {
modifiedContent = jsonPruner(
source,
jsonContent,
prunePaths,
requiredPaths,
stack = '',
{
nativeStringify,
},
);
// Convert content to appropriate response type, only if it has been modified
try {
const { responseType } = thisArg;
switch (responseType) {
case '':
case 'text':
modifiedContent = nativeStringify(modifiedContent);
break;
case 'arraybuffer':
modifiedContent = new TextEncoder()
.encode(nativeStringify(modifiedContent))
.buffer;
break;
case 'blob':
modifiedContent = new Blob([nativeStringify(modifiedContent)]);
break;
default:
break;
}
} catch (error) {
const message = `Response body cannot be converted to reponse type: '${content}'`;
logMessage(source, message);
modifiedContent = content;
}
}
} catch (error) {
const message = `Response body cannot be converted to json: '${content}'`;
logMessage(source, message);
modifiedContent = content;
}
}
// Manually put required values into target XHR object
// as thisArg can't be redefined and XHR objects can't be (re)assigned or copied
Object.defineProperties(thisArg, {
// original values
readyState: { value: readyState, writable: false },
responseURL: { value: responseURL, writable: false },
responseXML: { value: responseXML, writable: false },
status: { value: status, writable: false },
statusText: { value: statusText, writable: false },
// modified values
response: { value: modifiedContent, writable: false },
responseText: { value: modifiedContent, writable: false },
});
// Mock events
setTimeout(() => {
const stateEvent = new Event('readystatechange');
thisArg.dispatchEvent(stateEvent);
const loadEvent = new Event('load');
thisArg.dispatchEvent(loadEvent);
const loadEndEvent = new Event('loadend');
thisArg.dispatchEvent(loadEndEvent);
}, 1);
hit(source);
});
nativeOpen.apply(forgedRequest, [xhrData.method, xhrData.url, Boolean(xhrData.async)]);
// Mimic request headers before sending
// setRequestHeader can only be called on open request objects
thisArg.collectedHeaders.forEach((header) => {
forgedRequest.setRequestHeader(header[0], header[1]);
});
thisArg.collectedHeaders = [];
try {
nativeSend.call(forgedRequest, args);
} catch {
return Reflect.apply(target, thisArg, args);
}
return undefined;
};
const openHandler = {
apply: openWrapper,
};
const sendHandler = {
apply: sendWrapper,
};
XMLHttpRequest.prototype.open = new Proxy(XMLHttpRequest.prototype.open, openHandler);
XMLHttpRequest.prototype.send = new Proxy(XMLHttpRequest.prototype.send, sendHandler);
}
export const jsonPruneXhrResponseNames = [
'json-prune-xhr-response',
// aliases are needed for matching the related scriptlet converted into our syntax
'json-prune-xhr-response.js',
'ubo-json-prune-xhr-response.js',
'ubo-json-prune-xhr-response',
];
// eslint-disable-next-line prefer-destructuring
jsonPruneXhrResponse.primaryName = jsonPruneXhrResponseNames[0];
jsonPruneXhrResponse.injections = [
hit,
logMessage,
toRegExp,
jsonPruner,
getPrunePath,
objectToString,
matchRequestProps,
getXhrData,
isPruningNeeded,
matchStackTrace,
getMatchPropsData,
getRequestProps,
isValidParsedData,
parseMatchProps,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getWildcardPropertyInChain,
shouldAbortInlineOrInjectedScript,
getNativeRegexpTest,
backupRegExpValues,
restoreRegExpValues,
];