Skip to content

Commit 808fd65

Browse files
committed
Use cookie instead of window.name as a tab-configuration hack.
1 parent e44fce3 commit 808fd65

4 files changed

Lines changed: 30 additions & 36 deletions

File tree

src/bg/ChildPolicies.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
{
3-
let marker = JSON.stringify(uuid());
3+
let marker = uuid();
44
let allUrls = ["<all_urls>"];
55

66
let Scripts = {
@@ -47,7 +47,7 @@
4747
if (typeof perms !== "string") {
4848
perms = JSON.stringify(perms);
4949
}
50-
return `ns.setup(${perms}, ${marker});`
50+
return `ns.setup(${perms}, "${marker}");`
5151
}
5252
};
5353

@@ -104,26 +104,25 @@
104104
: [];
105105

106106
var ChildPolicies = {
107-
async storeTabInfo(tabId, info) {
108-
try {
109-
let preamble = info ? `${marker} + ${JSON.stringify(JSON.stringify([info]))} + ${marker} + "," + ` : "";
110-
await browser.tabs.executeScript(tabId, {
111-
code: `window.name = ${preamble}window.name.split(${marker} + ",").pop();`,
112-
allFrames: true,
113-
matchAboutBlank: true,
114-
runAt: "document_start",
115-
});
116-
} catch (e) {
117-
error(e);
107+
addTabInfoCookie(request, info) {
108+
let h = {
109+
name: "Set-Cookie",
110+
value: `${marker}=${JSON.stringify(info)}`
111+
};
112+
let {responseHeaders} = request;
113+
if (responseHeaders.some(({value, name}) => h.value === value && h.name === name)) {
114+
return false;
118115
}
116+
responseHeaders.push(h);
117+
return true;
119118
},
120119
async update(policy, tracing) {
121120
if (tracing !== "undefined") Scripts.debug = tracing;
122121
let t0 = Date.now();
123122
await Scripts.init();
124123

125124
if (!policy.enforced) {
126-
await Scripts.register(`ns.setup(null, ${marker});`, allUrls);
125+
await Scripts.register(Scripts.buildPerms("null"), allUrls);
127126
return;
128127
}
129128

src/bg/RequestGuard.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ var RequestGuard = (() => {
292292
async onHeadersReceived(request) {
293293
// called for main_frame, sub_frame and object
294294
// check for duplicate calls
295+
let headersModified = false;
295296
let pending = pendingRequests.get(request.requestId);
296297
if (pending) {
297298
if (pending.headersProcessed) {
@@ -319,7 +320,10 @@ var RequestGuard = (() => {
319320
capabilities = perms.capabilities;
320321
} else {
321322
if (isMainFrame || type === "sub_frame") {
322-
await Settings.enforceTabRestrictions(tabId);
323+
let unrestricted = ns.unrestrictedTabs.has(tabId) && {unrestricted: true};
324+
if (unrestricted) {
325+
headersModified = ChildPolicies.addTabInfoCookie(request, unrestricted);
326+
}
323327
}
324328
}
325329
if (isMainFrame && !TabStatus.map.has(tabId)) {
@@ -331,6 +335,9 @@ var RequestGuard = (() => {
331335
if (header) {
332336
pending.cspHeader = header;
333337
debug(`CSP blocker on %s:`, url, header.value);
338+
headersModified = true;
339+
}
340+
if (headersModified) {
334341
return {responseHeaders};
335342
}
336343
} catch (e) {

src/bg/Settings.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ var Settings = {
9090

9191
if (typeof unrestrictedTab === "boolean") {
9292
ns.unrestrictedTabs[unrestrictedTab ? "add" : "delete"](tabId);
93-
this.enforceTabRestrictions(tabId, unrestrictedTab);
9493
}
9594
if (reloadAffected) {
9695
browser.tabs.reload(tabId);

src/content/staticNS.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,22 @@
4747
this.config.permissions = permissions;
4848

4949
// ugly hack: since now we use registerContentScript instead of the
50-
// filterRequest dynamic script injection hack, we use window.name
51-
// to store per-tab information. We don't want web content to
52-
// mess with it, though, so we wrap it around auto-hiding accessors
50+
// filterRequest dynamic script injection hack, we use a session cookie
51+
// to store per-tab information, erasing it as soon as we see it
52+
// (before any content can access it)
5353

5454
if (this.config.MARKER = MARKER) {
55-
let splitter = `${MARKER},`;
56-
this.getWindowName = () => window.name.split(splitter).pop();
57-
58-
let tabInfoRx = new RegExp(`^${MARKER}\\[([^]*?)\\]${MARKER},`);
59-
60-
let tabInfoMatch = window.name.match(tabInfoRx);
61-
if (tabInfoMatch) {
55+
let cookieRx = new RegExp(`(?:^|;\\s*)${MARKER}=([^;]*)`);
56+
let match = document.cookie.match(cookieRx);
57+
if (match) {
58+
// delete cookie NOW
59+
document.cookie = `${MARKER}=;expires=${new Date(Date.now() - 31536000000).toGMTString()}`;
6260
try {
63-
this.config.tabInfo = JSON.parse(tabInfoMatch[1]);
61+
this.config.tabInfo = JSON.parse(decodeURIComponent(match[1]));
6462
} catch (e) {
6563
error(e);
6664
}
6765
}
68-
69-
Reflect.defineProperty(window.wrappedJSObject, "name", {
70-
get: exportFunction(() => this.getWindowName(), window.wrappedJSObject),
71-
set: exportFunction(value => {
72-
let tabInfoMatch = window.name.match(tabInfoRx);
73-
window.name = tabInfoMatch ? `${tabInfoMatch[0]}${value}` : value;
74-
return value;
75-
}, window.wrappedJSObject)
76-
});
7766
}
7867
if (!this.config.permissions || this.config.tabInfo.unrestricted) {
7968
debug("%s is loading unrestricted by user's choice (%o).", document.URL, this.config);

0 commit comments

Comments
 (0)