Skip to content

Commit af6c992

Browse files
committed
Add permissions= filter option
1 parent d40bd2a commit af6c992

File tree

4 files changed

+68
-38
lines changed

4 files changed

+68
-38
lines changed

src/js/background.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ const µBlock = { // jshint ignore:line
176176

177177
// Read-only
178178
systemSettings: {
179-
compiledMagic: 52, // Increase when compiled format changes
180-
selfieMagic: 52, // Increase when selfie format changes
179+
compiledMagic: 53, // Increase when compiled format changes
180+
selfieMagic: 53, // Increase when selfie format changes
181181
},
182182

183183
// https://github.com/uBlockOrigin/uBlock-issues/issues/759#issuecomment-546654501

src/js/static-filtering-parser.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,20 +2434,21 @@ const OPTTokenMp4 = 26;
24342434
const OPTTokenNoop = 27;
24352435
const OPTTokenObject = 28;
24362436
const OPTTokenOther = 29;
2437-
const OPTTokenPing = 30;
2438-
const OPTTokenPopunder = 31;
2439-
const OPTTokenPopup = 32;
2440-
const OPTTokenRedirect = 33;
2441-
const OPTTokenRedirectRule = 34;
2442-
const OPTTokenRemoveparam = 35;
2443-
const OPTTokenScript = 36;
2444-
const OPTTokenShide = 37;
2445-
const OPTTokenTo = 38;
2446-
const OPTTokenXhr = 39;
2447-
const OPTTokenWebrtc = 40;
2448-
const OPTTokenWebsocket = 41;
2449-
const OPTTokenMethod = 42;
2450-
const OPTTokenCount = 43;
2437+
const OPTTokenPermissions = 30;
2438+
const OPTTokenPing = 31;
2439+
const OPTTokenPopunder = 32;
2440+
const OPTTokenPopup = 33;
2441+
const OPTTokenRedirect = 34;
2442+
const OPTTokenRedirectRule = 35;
2443+
const OPTTokenRemoveparam = 36;
2444+
const OPTTokenScript = 37;
2445+
const OPTTokenShide = 38;
2446+
const OPTTokenTo = 39;
2447+
const OPTTokenXhr = 40;
2448+
const OPTTokenWebrtc = 41;
2449+
const OPTTokenWebsocket = 42;
2450+
const OPTTokenMethod = 43;
2451+
const OPTTokenCount = 44;
24512452

24522453
//const OPTPerOptionMask = 0x0000ff00;
24532454
const OPTCanNegate = 1 << 8;
@@ -2540,6 +2541,7 @@ Parser.prototype.OPTTokenMp4 = OPTTokenMp4;
25402541
Parser.prototype.OPTTokenNoop = OPTTokenNoop;
25412542
Parser.prototype.OPTTokenObject = OPTTokenObject;
25422543
Parser.prototype.OPTTokenOther = OPTTokenOther;
2544+
Parser.prototype.OPTTokenPermissions = OPTTokenPermissions;
25432545
Parser.prototype.OPTTokenPing = OPTTokenPing;
25442546
Parser.prototype.OPTTokenPopunder = OPTTokenPopunder;
25452547
Parser.prototype.OPTTokenPopup = OPTTokenPopup;
@@ -2606,6 +2608,7 @@ const netOptionTokenDescriptors = new Map([
26062608
[ 'object', OPTTokenObject | OPTCanNegate | OPTNetworkType | OPTModifiableType | OPTRedirectableType | OPTNonCspableType ],
26072609
/* synonym */ [ 'object-subrequest', OPTTokenObject | OPTCanNegate | OPTNetworkType | OPTModifiableType | OPTRedirectableType | OPTNonCspableType ],
26082610
[ 'other', OPTTokenOther | OPTCanNegate | OPTNetworkType | OPTModifiableType | OPTRedirectableType | OPTNonCspableType ],
2611+
[ 'permissions', OPTTokenPermissions | OPTMustAssign | OPTAllowMayAssign | OPTModifierType ],
26092612
[ 'ping', OPTTokenPing | OPTCanNegate | OPTNetworkType | OPTModifiableType | OPTNonCspableType | OPTNonRedirectableType ],
26102613
/* synonym */ [ 'beacon', OPTTokenPing | OPTCanNegate | OPTNetworkType | OPTModifiableType | OPTNonCspableType | OPTNonRedirectableType ],
26112614
[ 'popunder', OPTTokenPopunder | OPTNonNetworkType | OPTNonCspableType | OPTNonRedirectableType ],
@@ -2668,6 +2671,7 @@ Parser.netOptionTokenIds = new Map([
26682671
[ 'object', OPTTokenObject ],
26692672
/* synonym */ [ 'object-subrequest', OPTTokenObject ],
26702673
[ 'other', OPTTokenOther ],
2674+
[ 'permissions', OPTTokenPermissions ],
26712675
[ 'ping', OPTTokenPing ],
26722676
/* synonym */ [ 'beacon', OPTTokenPing ],
26732677
[ 'popunder', OPTTokenPopunder ],
@@ -2717,6 +2721,7 @@ Parser.netOptionTokenNames = new Map([
27172721
[ OPTTokenNoop, '_' ],
27182722
[ OPTTokenObject, 'object' ],
27192723
[ OPTTokenOther, 'other' ],
2724+
[ OPTTokenPermissions, 'permissions' ],
27202725
[ OPTTokenPing, 'ping' ],
27212726
[ OPTTokenPopunder, 'popunder' ],
27222727
[ OPTTokenPopup, 'popup' ],
@@ -2929,8 +2934,7 @@ const NetOptionsIterator = class {
29292934
}
29302935
}
29312936
}
2932-
// `csp=`: only to "csp-able" types, which currently are only
2933-
// document types.
2937+
// `csp=`: only document types.
29342938
{
29352939
const i = this.tokenPos[OPTTokenCsp];
29362940
if ( i !== -1 && hasBits(allBits, OPTNonCspableType) ) {
@@ -2940,6 +2944,16 @@ const NetOptionsIterator = class {
29402944
}
29412945
}
29422946
}
2947+
// `permissions=`: only to document types.
2948+
{
2949+
const i = this.tokenPos[OPTTokenPermissions];
2950+
if ( i !== -1 && hasBits(allBits, OPTNonCspableType) ) {
2951+
optSlices[i] = OPTTokenInvalid;
2952+
if ( this.interactive ) {
2953+
this.parser.errorSlices(optSlices[i+1], optSlices[i+5]);
2954+
}
2955+
}
2956+
}
29432957
// `removeparam=`: only for network requests.
29442958
{
29452959
const i = this.tokenPos[OPTTokenRemoveparam];

src/js/static-net-filtering.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3323,7 +3323,13 @@ class FilterCompiler {
33233323
if ( val !== undefined && this.reBadCSP.test(val) ) {
33243324
return false;
33253325
}
3326-
this.optionUnitBits |= this.CSP_BIT;
3326+
this.optionUnitBits |= this.HTTP_HEADERS_BIT;
3327+
break;
3328+
case parser.OPTTokenPermissions:
3329+
if ( this.processModifierOption(id, val) === false ) {
3330+
return false;
3331+
}
3332+
this.optionUnitBits |= this.HTTP_HEADERS_BIT;
33273333
break;
33283334
// https://github.com/gorhill/uBlock/issues/2294
33293335
// Detect and discard filter if domain option contains
@@ -3607,22 +3613,22 @@ class FilterCompiler {
36073613
// Mind `\b` directives: `/\bads\b/` should result in token being `ads`,
36083614
// not `bads`.
36093615
extractTokenFromRegex(pattern) {
3610-
pattern = StaticFilteringParser.utils.regex.toTokenizableStr(pattern);
3616+
const tokenizable = StaticFilteringParser.utils.regex.toTokenizableStr(pattern);
36113617
this.reToken.lastIndex = 0;
36123618
let bestToken;
36133619
let bestBadness = 0x7FFFFFFF;
36143620
for (;;) {
3615-
const matches = this.reToken.exec(pattern);
3621+
const matches = this.reToken.exec(tokenizable);
36163622
if ( matches === null ) { break; }
36173623
const { 0: token, index } = matches;
3618-
if ( index === 0 || pattern.charAt(index - 1) === '\x01' ) {
3624+
if ( index === 0 || tokenizable.charAt(index - 1) === '\x01' ) {
36193625
continue;
36203626
}
36213627
const { lastIndex } = this.reToken;
36223628
if (
36233629
token.length < MAX_TOKEN_LENGTH && (
3624-
lastIndex === pattern.length ||
3625-
pattern.charAt(lastIndex) === '\x01'
3630+
lastIndex === tokenizable.length ||
3631+
tokenizable.charAt(lastIndex) === '\x01'
36263632
)
36273633
) {
36283634
continue;
@@ -3902,7 +3908,7 @@ FilterCompiler.prototype.TO_BIT = 0b00000000010;
39023908
FilterCompiler.prototype.DENYALLOW_BIT = 0b00000000100;
39033909
FilterCompiler.prototype.HEADER_BIT = 0b00000001000;
39043910
FilterCompiler.prototype.STRICT_PARTY_BIT = 0b00000010000;
3905-
FilterCompiler.prototype.CSP_BIT = 0b00000100000;
3911+
FilterCompiler.prototype.HTTP_HEADERS_BIT = 0b00000100000;
39063912
FilterCompiler.prototype.REMOVEPARAM_BIT = 0b00001000000;
39073913
FilterCompiler.prototype.REDIRECT_BIT = 0b00010000000;
39083914
FilterCompiler.prototype.NOT_TYPE_BIT = 0b00100000000;

src/js/traffic.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ window.addEventListener('webextFlavor', function() {
6161
vAPI.webextFlavor.soup.has('firefox');
6262
}, { once: true });
6363

64-
// https://github.com/uBlockOrigin/uBlock-issues/issues/1553
65-
const supportsFloc = document.interestCohort instanceof Function;
66-
6764
/******************************************************************************/
6865

6966
const patchLocalRedirectURL = url => url.charCodeAt(0) === 0x2F /* '/' */
@@ -562,7 +559,7 @@ const onHeadersReceived = function(details) {
562559
if ( injectCSP(fctxt, pageStore, responseHeaders) === true ) {
563560
modifiedHeaders = true;
564561
}
565-
if ( supportsFloc && foilFloc(fctxt, responseHeaders) ) {
562+
if ( injectPP(fctxt, pageStore, responseHeaders) ) {
566563
modifiedHeaders = true;
567564
}
568565

@@ -1012,18 +1009,31 @@ const injectCSP = function(fctxt, pageStore, responseHeaders) {
10121009

10131010
/******************************************************************************/
10141011

1015-
// https://github.com/uBlockOrigin/uBlock-issues/issues/1553
1016-
// https://github.com/WICG/floc#opting-out-of-computation
1012+
const injectPP = function(fctxt, pageStore, responseHeaders) {
1013+
const permissions = [];
1014+
const directives = staticNetFilteringEngine.matchAndFetchModifiers(fctxt, 'permissions');
1015+
if ( directives !== undefined ) {
1016+
for ( const directive of directives ) {
1017+
if ( directive.result !== 1 ) { continue; }
1018+
permissions.push(directive.value.replace('|', ', '));
1019+
}
1020+
}
10171021

1018-
const foilFloc = function(fctxt, responseHeaders) {
1019-
const hn = fctxt.getHostname();
1020-
if ( scriptletFilteringEngine.hasScriptlet(hn, 1, 'no-floc') === false ) {
1021-
return false;
1022+
if ( logger.enabled && directives !== undefined ) {
1023+
fctxt.setRealm('network')
1024+
.pushFilters(directives.map(a => a.logData()))
1025+
.toLogger();
10221026
}
1027+
1028+
if ( permissions.length === 0 ) { return; }
1029+
1030+
µb.updateToolbarIcon(fctxt.tabId, 0x02);
1031+
10231032
responseHeaders.push({
1024-
name: 'Permissions-Policy',
1025-
value: 'interest-cohort=()' }
1026-
);
1033+
name: 'permissions-policy',
1034+
value: permissions.join(', ')
1035+
});
1036+
10271037
return true;
10281038
};
10291039

0 commit comments

Comments
 (0)