Skip to content

Commit

Permalink
Improve prevent-fetch scriptlet
Browse files Browse the repository at this point in the history
Related discussion:
uBlockOrigin/uBlock-discussions#848 (comment)

Added support for AdGuard's `responseType` parameter. Extended the
meaning of that 3rd parameter to also be a JSON string with
properties to set on the returned response instance. Currently
supported properties:

- `ok`, supported values: `false`, `true`
- `type, supported values: `"basic"`, `"cors"`, `"opaque"`

Reference:
https://github.com/AdguardTeam/Scriptlets/blob/master/wiki/about-scriptlets.md#-%EF%B8%8F-prevent-fetch
  • Loading branch information
gorhill committed Jul 13, 2024
1 parent a54d416 commit e785b99
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function safeSelf() {
'Math_random': Math.random,
'Object': Object,
'Object_defineProperty': Object.defineProperty.bind(Object),
'Object_defineProperties': Object.defineProperties.bind(Object),
'Object_fromEntries': Object.fromEntries.bind(Object),
'Object_getOwnPropertyDescriptor': Object.getOwnPropertyDescriptor.bind(Object),
'RegExp': self.RegExp,
Expand Down Expand Up @@ -2067,11 +2068,11 @@ builtinScriptlets.push({
});
function noFetchIf(
propsToMatch = '',
responseBody = ''
responseBody = '',
responseType = ''
) {
if ( typeof propsToMatch !== 'string' ) { return; }
const safe = safeSelf();
const logPrefix = safe.makeLogPrefix('prevent-fetch', propsToMatch, responseBody);
const logPrefix = safe.makeLogPrefix('prevent-fetch', propsToMatch, responseBody, responseType);
const needles = [];
for ( const condition of propsToMatch.split(/\s+/) ) {
if ( condition === '' ) { continue; }
Expand All @@ -2086,6 +2087,26 @@ function noFetchIf(
}
needles.push({ key, re: safe.patternToRegex(value) });
}
const validResponseProps = {
ok: [ false, true ],
type: [ 'basic', 'cors', 'opaque' ],
};
let responseProps;
if ( /^\{.*\}$/.test(responseType) ) {
responseProps = {};
try {
Object.entries(JSON.parse(responseType)).forEach(([ p, v ]) => {
if ( validResponseProps[p] === undefined ) { return; }
if ( validResponseProps[p].includes(v) === false ) { return; }
responseProps[p] = { value: v };
});
}
catch(ex) {}
} else if ( responseType !== '' ) {
if ( validResponseProps.type.includes(responseType) ) {
responseProps = { type: { value: responseType } };
}
}
self.fetch = new Proxy(self.fetch, {
apply: function(target, thisArg, args) {
const details = args[0] instanceof self.Request
Expand Down Expand Up @@ -2123,17 +2144,6 @@ function noFetchIf(
if ( proceed ) {
return Reflect.apply(target, thisArg, args);
}
let responseType = '';
if ( details.mode === undefined || details.mode === 'cors' ) {
try {
const desURL = new URL(details.url);
responseType = desURL.origin !== document.location.origin
? 'cors'
: 'basic';
} catch(ex) {
safe.uboErr(logPrefix, `Error: ${ex}`);
}
}
return generateContentFn(responseBody).then(text => {
safe.uboLog(logPrefix, `Prevented with response "${text}"`);
const response = new Response(text, {
Expand All @@ -2142,14 +2152,11 @@ function noFetchIf(
'Content-Length': text.length,
}
});
safe.Object_defineProperty(response, 'url', {
value: details.url
});
if ( responseType !== '' ) {
safe.Object_defineProperty(response, 'type', {
value: responseType
});
}
const props = Object.assign(
{ url: { value: details.url } },
responseProps
);
safe.Object_defineProperties(response, props);
return response;
});
}
Expand Down

0 comments on commit e785b99

Please sign in to comment.