Skip to content

Commit f76d4b4

Browse files
committed
Make contextual policies override restriction cascading (tor-browser#43397).
1 parent 8e763be commit f76d4b4

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

src/bg/DNRPolicy.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
{
2323
const DEFAULT_PRIORITY = 1;
2424
const SITE_PRIORITY = 10;
25-
const CTX_PRIORITY = 20;
26-
const CASCADE_PRIORITY = 30;
25+
const CASCADE_PRIORITY = 20;
26+
const CTX_PRIORITY = 30;
2727
const TAB_PRIORITY = 40;
2828
const REPORT_PRIORITY = 50;
2929
const MAX_PRIORITY = 100;
@@ -255,7 +255,7 @@
255255
return rules;
256256
}
257257
const tabPresets = new Map();
258-
for({url, id} of tabs) {
258+
for(const {url, id} of tabs) {
259259
const resourceTypes = ResourceTypeFor.block(policy.get(url).perms.capabilities);
260260
if (!resourceTypes.length) continue;
261261
const key = JSON.stringify(resourceTypes);

src/bg/RequestGuard.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,16 @@
378378

379379
const checked = ret.checks.map((i) => checks[i]._val);
380380

381+
const wantsContext = checked.includes("ctx")
382+
381383
let { siteMatch, contextMatch, perms } = ns.policy.get(key, contextUrl);
382384

383-
if (!perms.capabilities.has(policyType)) {
385+
if (!perms.capabilities.has(policyType) ||
386+
!contextMatch && wantsContext && ctxKey) {
384387
if (!contextMatch) {
385388
perms = perms.clone();
386389
ns.policy.set(key, perms);
387-
if (ctxKey && checked.includes("ctx")) {
390+
if (ctxKey && wantsContext) {
388391
perms.contextual.set(ctxKey, perms = perms.clone(/* noContext = */ true));
389392
}
390393
}
@@ -494,17 +497,17 @@
494497
}
495498
};
496499

497-
function intersectCapabilities(perms, request) {
500+
function intersectCapabilities(policyMatch, request) {
498501
if (request.frameId !== 0 && ns.sync.cascadeRestrictions) {
499502
const {tabUrl, frameAncestors} = request;
500503
const topUrl = tabUrl ||
501504
frameAncestors && frameAncestors[frameAncestors?.length - 1]?.url ||
502505
TabCache.get(request.tabId)?.url;
503506
if (topUrl) {
504-
return ns.policy.cascadeRestrictions(perms, topUrl).capabilities;
507+
return ns.policy.cascadeRestrictions(policyMatch, topUrl).capabilities;
505508
}
506509
}
507-
return perms.capabilities;
510+
return policyMatch.perms.capabilities;
508511
}
509512

510513
const ABORT = {cancel: true},
@@ -652,8 +655,8 @@
652655
.some(tabId => TabStatus.hasOrigin(tabId, documentUrl));
653656
}
654657
if (!allowed) {
655-
let capabilities = intersectCapabilities(
656-
policy.get(url, ns.policyContext(request)).perms,
658+
const capabilities = intersectCapabilities(
659+
policy.get(url, ns.policyContext(request)),
657660
request);
658661
allowed = !policyType || capabilities.has(policyType);
659662
if (allowed && request._dataUrl && type.endsWith("frame")) {
@@ -762,16 +765,17 @@
762765
try {
763766
let capabilities;
764767
if (ns.isEnforced(tabId)) {
765-
let policy = ns.policy;
766-
let {perms} = policy.get(url, ns.policyContext(request));
768+
const { policy } = ns;
769+
const policyMatch = policy.get(url, ns.policyContext(request));
770+
let perms = { match: policyMatch };
767771
if (isMainFrame) {
768772
const autoPerms = policy.autoAllow(url, perms);
769773
if (autoPerms) {
770774
perms = autoPerms;
771775
}
772776
capabilities = perms.capabilities;
773777
} else {
774-
capabilities = intersectCapabilities(perms, request);
778+
capabilities = intersectCapabilities(policyMatch, request);
775779
}
776780
} // else unrestricted, either globally or per-tab
777781
if (isMainFrame && !TabStatus.map.has(tabId)) {

src/bg/main.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@
340340

341341
let permissions, unrestricted, cascaded;
342342
if (policy) {
343-
let perms = policy.get(url, contextUrl).perms;
343+
const policyMatch = policy.get(url, contextUrl);
344+
let { perms } = policyMatch;
344345
if (isTop) {
345346
const autoPerms = policy.autoAllow(url, perms);
346347
if (autoPerms) {
@@ -355,7 +356,7 @@
355356
} else {
356357
cascaded = topUrl && ns.sync.cascadeRestrictions;
357358
if (cascaded) {
358-
perms = policy.cascadeRestrictions(perms, topUrl);
359+
perms = policy.cascadeRestrictions(policyMatch, topUrl);
359360
}
360361
}
361362
permissions = perms.dry();

src/nscl

0 commit comments

Comments
 (0)