1
|
/**
|
2
|
* Hachette main content script run in all frames
|
3
|
*
|
4
|
* Copyright (C) 2021 Wojtek Kosior
|
5
|
* Copyright (C) 2021 jahoti
|
6
|
* Redistribution terms are gathered in the `copyright' file.
|
7
|
*/
|
8
|
|
9
|
/*
|
10
|
* IMPORTS_START
|
11
|
* IMPORT handle_page_actions
|
12
|
* IMPORT url_item
|
13
|
* IMPORT url_extract_target
|
14
|
* IMPORT gen_unique
|
15
|
* IMPORT gen_nonce
|
16
|
* IMPORT csp_rule
|
17
|
* IMPORT is_privileged_url
|
18
|
* IMPORT sanitize_attributes
|
19
|
* IMPORT mozilla_suppress_scripts
|
20
|
* IMPORT is_chrome
|
21
|
* IMPORT is_mozilla
|
22
|
* IMPORT start_activity_info_server
|
23
|
* IMPORTS_END
|
24
|
*/
|
25
|
|
26
|
/*
|
27
|
* Due to some technical limitations the chosen method of whitelisting sites
|
28
|
* is to smuggle whitelist indicator in page's url as a "magical" string
|
29
|
* after '#'. Right now this is only supplemental in HTTP(s) pages where
|
30
|
* blocking of native scripts also happens through CSP header injection but is
|
31
|
* necessary for protocols like ftp:// and file://.
|
32
|
*
|
33
|
* The code that actually injects the magical string into ftp:// and file://
|
34
|
* urls has not yet been added to the extension.
|
35
|
*/
|
36
|
|
37
|
var nonce = undefined;
|
38
|
|
39
|
function handle_mutation(mutations, observer)
|
40
|
{
|
41
|
if (document.readyState === 'complete') {
|
42
|
console.log("mutation handling complete");
|
43
|
observer.disconnect();
|
44
|
return;
|
45
|
}
|
46
|
for (const mutation of mutations) {
|
47
|
for (const node of mutation.addedNodes)
|
48
|
block_node(node);
|
49
|
}
|
50
|
}
|
51
|
|
52
|
function block_nodes_recursively(node)
|
53
|
{
|
54
|
block_node(node);
|
55
|
for (const child of node.children)
|
56
|
block_nodes_recursively(child);
|
57
|
}
|
58
|
|
59
|
function block_node(node)
|
60
|
{
|
61
|
/*
|
62
|
* Modifying <script> element doesn't always prevent its execution in some
|
63
|
* Mozilla browsers. This is Chromium-specific code.
|
64
|
*/
|
65
|
if (node.tagName === "SCRIPT") {
|
66
|
block_script(node);
|
67
|
return;
|
68
|
}
|
69
|
|
70
|
sanitize_attributes(node);
|
71
|
|
72
|
if (node.tagName === "HEAD")
|
73
|
inject_csp(node);
|
74
|
}
|
75
|
|
76
|
function block_script(node)
|
77
|
{
|
78
|
/*
|
79
|
* Disabling scripts this way allows them to still be relatively
|
80
|
* easily accessed in case they contain some useful data.
|
81
|
*/
|
82
|
if (node.hasAttribute("type"))
|
83
|
node.setAttribute("blocked-type", node.getAttribute("type"));
|
84
|
node.setAttribute("type", "application/json");
|
85
|
}
|
86
|
|
87
|
function inject_csp(head)
|
88
|
{
|
89
|
console.log('injecting CSP');
|
90
|
|
91
|
let meta = document.createElement("meta");
|
92
|
meta.setAttribute("http-equiv", "Content-Security-Policy");
|
93
|
meta.setAttribute("content", csp_rule(nonce));
|
94
|
|
95
|
if (head.firstElementChild === null)
|
96
|
head.appendChild(meta);
|
97
|
else
|
98
|
head.insertBefore(meta, head.firstElementChild);
|
99
|
}
|
100
|
|
101
|
if (!is_privileged_url(document.URL)) {
|
102
|
const targets = url_extract_target(document.URL);
|
103
|
if (targets.policy) {
|
104
|
if (targets.target2)
|
105
|
window.location.href = targets.base_url + targets.target2;
|
106
|
else
|
107
|
history.replaceState(null, "", targets.base_url);
|
108
|
}
|
109
|
|
110
|
const policy = targets.current ? targets.policy : {};
|
111
|
|
112
|
nonce = policy.nonce || gen_nonce();
|
113
|
handle_page_actions(nonce);
|
114
|
|
115
|
if (!policy.allow) {
|
116
|
block_nodes_recursively(document.documentElement);
|
117
|
|
118
|
if (is_chrome) {
|
119
|
var observer = new MutationObserver(handle_mutation);
|
120
|
observer.observe(document.documentElement, {
|
121
|
attributes: true,
|
122
|
childList: true,
|
123
|
subtree: true
|
124
|
});
|
125
|
}
|
126
|
|
127
|
if (is_mozilla)
|
128
|
addEventListener('beforescriptexecute', mozilla_suppress_scripts, true);
|
129
|
}
|
130
|
|
131
|
start_activity_info_server();
|
132
|
}
|