Skip to content

Commit 8243c65

Browse files
committed
Regard as "mutually safe" for x-load directories sharing a common ancestor.
1 parent ae763e7 commit 8243c65

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/content/eventsHook.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
'use strict';
2222

2323
if (location.protocol == "file:") {
24+
// Regard as "mutually safe" directories sharing a common ancestor at least this deep
25+
const SAFE_PATH_DEPTH = 5;
26+
const safePath = path => path.split("/", SAFE_PATH_DEPTH).join("/");
27+
const toDir = url => url.replace(/[^\/]+$/, "")
28+
const CURRENT_DIR = safePath(toDir(location.pathname));
2429

2530
const watchList = new WeakSet();
2631
const blockedList = new WeakSet();
@@ -31,9 +36,10 @@ if (location.protocol == "file:") {
3136
if (url.protocol != "file:") {
3237
return true;
3338
}
34-
const curDir = location.pathname.replace(/[^\/]+$/, "");
35-
const filePath = url.pathname;
36-
if (filePath.startsWith(curDir)) {
39+
40+
const filePath = safePath(url.pathname);
41+
42+
if (filePath.startsWith(CURRENT_DIR)) {
3743
return true;
3844
}
3945
const {href} = url;
@@ -42,13 +48,12 @@ if (location.protocol == "file:") {
4248
return allowed;
4349
};
4450

45-
4651
const notify = (url, allowed) => {
4752
const type = "x-load";
4853
const request = {
4954
id: "noscript-x-load",
5055
type,
51-
url: url.replace(/[^\/]+$/, ""), // truncate to dir
56+
url: toDir(url),
5257
documentUrl: document.URL,
5358
embeddingDocument: true,
5459
};
@@ -73,19 +78,22 @@ if (location.protocol == "file:") {
7378
} catch (e) {
7479
error(e);
7580
}
76-
el.srcset = el.src = "data:"; `data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"/>`;
81+
el.srcset = el.src = "data:";
7782
blockedList.add(el);
7883
};
7984

8085
const suppress = e => {
8186
if (!e.isTrusted) return;
8287
const { target } = e;
83-
const url = new URL(e.filename ||
84-
target.currentSrc ||
85-
target.src ||
86-
target.data ||
87-
target.href?.animVal ||
88-
target.href,
88+
const sURL = e.filename ||
89+
target.currentSrc ||
90+
target.src ||
91+
target.data ||
92+
target.href?.animVal ||
93+
target.href;
94+
if (!sURL) return;
95+
96+
const url = new URL(sURL,
8997
document.baseURI);
9098
if (!isAllowedPath(url)) {
9199
if (e.type == "loadstart") {

0 commit comments

Comments
 (0)