Skip to content

Commit b46572e

Browse files
committed
Improve prevent-fetch scriptlet
Related issue: uBlockOrigin/uAssets#15692 (comment)
1 parent fe95862 commit b46572e

File tree

1 file changed

+37
-47
lines changed

1 file changed

+37
-47
lines changed

src/js/resources/prevent-fetch.js

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
2121
*/
2222

23-
import { generateContentFn } from './utils.js';
23+
import {
24+
generateContentFn,
25+
matchObjectPropertiesFn,
26+
parsePropertiesToMatchFn,
27+
} from './utils.js';
2428
import { proxyApplyFn } from './proxy-apply.js';
2529
import { registerScriptlet } from './base.js';
2630
import { safeSelf } from './safe-self.js';
@@ -43,20 +47,7 @@ function preventFetchFn(
4347
responseType
4448
);
4549
const extraArgs = safe.getExtraArgs(Array.from(arguments), 4);
46-
const needles = [];
47-
for ( const condition of safe.String_split.call(propsToMatch, /\s+/) ) {
48-
if ( condition === '' ) { continue; }
49-
const pos = condition.indexOf(':');
50-
let key, value;
51-
if ( pos !== -1 ) {
52-
key = condition.slice(0, pos);
53-
value = condition.slice(pos + 1);
54-
} else {
55-
key = 'url';
56-
value = condition;
57-
}
58-
needles.push({ key, pattern: safe.initPattern(value, { canNegate: true }) });
59-
}
50+
const propNeedles = parsePropertiesToMatchFn(propsToMatch, 'url');
6051
const validResponseProps = {
6152
ok: [ false, true ],
6253
statusText: [ '', 'Not Found' ],
@@ -86,41 +77,38 @@ function preventFetchFn(
8677
}
8778
proxyApplyFn('fetch', function fetch(context) {
8879
const { callArgs } = context;
89-
const details = callArgs[0] instanceof self.Request
90-
? callArgs[0]
91-
: Object.assign({ url: callArgs[0] }, callArgs[1]);
92-
let proceed = true;
93-
try {
94-
const props = new Map();
95-
for ( const prop in details ) {
96-
let v = details[prop];
97-
if ( typeof v !== 'string' ) {
98-
try { v = safe.JSON_stringify(v); }
99-
catch { }
80+
const details = (( ) => {
81+
const fetchProps = (src, out) => {
82+
if ( typeof src !== 'object' || src === null ) { return; }
83+
const props = [
84+
'body', 'cache', 'credentials', 'duplex', 'headers',
85+
'integrity', 'keepalive', 'method', 'mode', 'priority',
86+
'redirect', 'referrer', 'referrerPolicy', 'signal',
87+
];
88+
for ( const prop of props ) {
89+
if ( src[prop] === undefined ) { continue; }
90+
out[prop] = src[prop];
10091
}
101-
if ( typeof v !== 'string' ) { continue; }
102-
props.set(prop, v);
103-
}
104-
if ( safe.logLevel > 1 || propsToMatch === '' && responseBody === '' ) {
105-
const out = Array.from(props).map(a => `${a[0]}:${a[1]}`);
106-
safe.uboLog(logPrefix, `Called: ${out.join('\n')}`);
107-
}
108-
if ( propsToMatch === '' && responseBody === '' ) {
109-
return context.reflect();
92+
};
93+
const out = {};
94+
if ( callArgs[0] instanceof self.Request ) {
95+
out.url = `${callArgs[0].url}`;
96+
fetchProps(callArgs[0], out);
97+
} else {
98+
out.url = `${callArgs[0]}`;
11099
}
111-
proceed = needles.length === 0;
112-
for ( const { key, pattern } of needles ) {
113-
if (
114-
pattern.expect && props.has(key) === false ||
115-
safe.testPattern(pattern, props.get(key)) === false
116-
) {
117-
proceed = true;
118-
break;
119-
}
120-
}
121-
} catch {
100+
fetchProps(callArgs[1], out);
101+
return out;
102+
})();
103+
if ( safe.logLevel > 1 || propsToMatch === '' && responseBody === '' ) {
104+
const out = Array.from(details).map(a => `${a[0]}:${a[1]}`);
105+
safe.uboLog(logPrefix, `Called: ${out.join('\n')}`);
106+
}
107+
if ( propsToMatch === '' && responseBody === '' ) {
108+
return context.reflect();
122109
}
123-
if ( proceed ) {
110+
const matched = matchObjectPropertiesFn(propNeedles, details);
111+
if ( matched === undefined ) {
124112
return context.reflect();
125113
}
126114
return Promise.resolve(generateContentFn(trusted, responseBody)).then(text => {
@@ -148,6 +136,8 @@ registerScriptlet(preventFetchFn, {
148136
name: 'prevent-fetch.fn',
149137
dependencies: [
150138
generateContentFn,
139+
matchObjectPropertiesFn,
140+
parsePropertiesToMatchFn,
151141
proxyApplyFn,
152142
safeSelf,
153143
],

0 commit comments

Comments
 (0)