Work-around for Chromium not supporting frameAncestors in webRequest.

This commit is contained in:
hackademix 2019-09-29 00:47:58 +02:00
parent 78063f3412
commit c3dcf300a6
3 changed files with 39 additions and 8 deletions

View File

@ -261,12 +261,18 @@ var RequestGuard = (() => {
return redirected; return redirected;
} }
function intersectCapabilities(perms, frameAncestors) { function intersectCapabilities(perms, request) {
if (frameAncestors && frameAncestors.length && ns.sync.cascadeRestrictions) { let {frameId, frameAncestors, tabId} = request;
// cascade top document's restrictions to subframes if (frameId !== 0 && ns.sync.cascadeRestrictions) {
perms = policy.cascadeRestrictions(perms, let topUrl = frameAncestors && frameAncestors.length
frameAncestors[frameAncestors.length - 1].url) && frameAncestors[frameAncestors.length - 1].url;
.capabilities; if (!topUrl) {
let tab = TabCache.get(tabId);
if (tab) topUrl = tab.url;
}
if (topUrl) {
return ns.policy.cascadeRestrictions(perms, topUrl).capabilities;
}
} }
return perms.capabilities; return perms.capabilities;
} }
@ -300,7 +306,7 @@ var RequestGuard = (() => {
!ns.isEnforced(request.tabId) || !ns.isEnforced(request.tabId) ||
intersectCapabilities( intersectCapabilities(
policy.get(url, documentUrl).perms, policy.get(url, documentUrl).perms,
request.frameAncestors request
).has(policyType); ).has(policyType);
Content.reportTo(request, allowed, policyType); Content.reportTo(request, allowed, policyType);
@ -347,7 +353,7 @@ var RequestGuard = (() => {
} }
capabilities = perms.capabilities; capabilities = perms.capabilities;
} else { } else {
capabilities = intersectCapabilities(perms, request.frameAncestors); capabilities = intersectCapabilities(perms, request);
} }
} // else unrestricted, either globally or per-tab } // else unrestricted, either globally or per-tab
if (isMainFrame && !TabStatus.map.has(tabId)) { if (isMainFrame && !TabStatus.map.has(tabId)) {

24
src/lib/TabCache.js Normal file
View File

@ -0,0 +1,24 @@
var TabCache = (() => {
let cache = new Map();
browser.tabs.onUpdated.addListener(tab => {
cache.set(tab.id, tab);
});
browser.tabs.onRemoved.addListener(tab => {
cache.delete(tab.id);
});
(async () => {
for (let tab of await browser.tabs.query({})) {
cache.set(tab.id, tab);
}
})();
return {
get(tabId) {
return cache.get(tabId);
}
};
})();

View File

@ -47,6 +47,7 @@
"lib/Messages.js", "lib/Messages.js",
"lib/CSP.js", "lib/CSP.js",
"lib/NetCSP.js", "lib/NetCSP.js",
"lib/TabCache.js",
"common/CapsCSP.js", "common/CapsCSP.js",
"common/Policy.js", "common/Policy.js",
"common/locale.js", "common/locale.js",