Skip to content

Commit

Permalink
[mv3] Slightly mitigate DNR flaw re. removeparam filters
Browse files Browse the repository at this point in the history
Related issue:
uBlockOrigin/uBOL-home#140

This will allow more specific `removeparam` filters to override
generic ones. This doesn't fix the related issue but should help
more specific `removeparam` filters to be applied.

Related webextensions issue:
w3c/webextensions#468
  • Loading branch information
gorhill committed Nov 15, 2024
1 parent 7fcd2a5 commit 947602d
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/js/static-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -4452,22 +4452,23 @@ StaticNetFilteringEngine.prototype.dnrFromCompiled = function(op, context, ...ar
}

// Priority:
// Block: 1 (default priority)
// Redirect: 2-9
// Excepted redirect: 12-19
// Allow: 20
// Block important: 30
// Redirect important: 32-39
// Removeparam: 1-4
// Block: 10 (default priority)
// Redirect: 11-19
// Excepted redirect: 21-29
// Allow: 30
// Block important: 40
// Redirect important: 41-49

const realms = new Map([
[ BLOCK_REALM, { type: 'block', priority: 0 } ],
[ ALLOW_REALM, { type: 'allow', priority: 20 } ],
[ REDIRECT_REALM, { type: 'redirect', priority: 2 } ],
[ BLOCK_REALM, { type: 'block', priority: 10 } ],
[ ALLOW_REALM, { type: 'allow', priority: 30 } ],
[ REDIRECT_REALM, { type: 'redirect', priority: 11 } ],
[ REMOVEPARAM_REALM, { type: 'removeparam', priority: 0 } ],
[ CSP_REALM, { type: 'csp', priority: 0 } ],
[ PERMISSIONS_REALM, { type: 'permissions', priority: 0 } ],
[ URLTRANSFORM_REALM, { type: 'uritransform', priority: 0 } ],
[ HEADERS_REALM, { type: 'block', priority: 0 } ],
[ HEADERS_REALM, { type: 'block', priority: 10 } ],
[ URLSKIP_REALM, { type: 'urlskip', priority: 0 } ],
]);
const partyness = new Map([
Expand Down Expand Up @@ -4605,7 +4606,7 @@ StaticNetFilteringEngine.prototype.dnrFromCompiled = function(op, context, ...ar
if ( token !== '' ) {
const match = /:(\d+)$/.exec(token);
if ( match !== null ) {
rule.priority = Math.min(rule.priority + parseInt(match[1], 10), 9);
rule.priority += Math.min(rule.priority + parseInt(match[1], 10), 9);
token = token.slice(0, match.index);
}
}
Expand All @@ -4623,7 +4624,7 @@ StaticNetFilteringEngine.prototype.dnrFromCompiled = function(op, context, ...ar
}
break;
}
case 'removeparam':
case 'removeparam': {
rule.action.type = 'redirect';
if ( rule.__modifierValue === '|' ) {
rule.__modifierValue = '';
Expand Down Expand Up @@ -4657,10 +4658,21 @@ StaticNetFilteringEngine.prototype.dnrFromCompiled = function(op, context, ...ar
'xmlhttprequest',
];
}
// https://github.com/uBlockOrigin/uBOL-home/issues/140
// Mitigate until DNR API flaw is addressed by browser vendors
let priority = rule.priority || 1;
if ( rule.condition.urlFilter !== undefined ) { priority += 1; }
if ( rule.condition.regexFilter !== undefined ) { priority += 1; }
if ( rule.condition.initiatorDomains !== undefined ) { priority += 1; }
if ( rule.condition.requestDomains !== undefined ) { priority += 1; }
if ( priority !== 1 ) {
rule.priority = priority;
}
if ( rule.__modifierAction === ALLOW_REALM ) {
dnrAddRuleError(rule, `Unsupported removeparam exception: ${rule.__modifierValue}`);
}
break;
}
case 'uritransform': {
dnrAddRuleError(rule, `Incompatible with DNR: uritransform=${rule.__modifierValue}`);
break;
Expand Down

0 comments on commit 947602d

Please sign in to comment.