Project

General

Profile

Download (3.31 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / content / main.js @ ecb78704

1
/**
2
 * Myext 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 url_extract_policy
15
 * IMPORT gen_unique
16
 * IMPORT gen_nonce
17
 * IMPORT csp_rule
18
 * IMPORT is_privileged_url
19
 * IMPORT sanitize_attributes
20
 * IMPORT mozilla_suppress_scripts
21
 * IMPORT is_chrome
22
 * IMPORT is_mozilla
23
 * IMPORT start_activity_info_server
24
 * IMPORTS_END
25
 */
26

    
27
/*
28
 * Due to some technical limitations the chosen method of whitelisting sites
29
 * is to smuggle whitelist indicator in page's url as a "magical" string
30
 * after '#'. Right now this is not needed in HTTP(s) pages where native
31
 * script blocking happens through CSP header injection but is needed for
32
 * protocols like ftp:// and file://.
33
 *
34
 * The code that actually injects the magical string into ftp:// and file://
35
 * urls has not yet been added to the extension.
36
 */
37

    
38
function handle_mutation(mutations, observer)
39
{
40
    if (document.readyState === 'complete') {
41
	console.log("mutation handling complete");
42
	observer.disconnect();
43
	return;
44
    }
45
    for (const mutation of mutations) {
46
	for (const node of mutation.addedNodes)
47
	    block_node(node);
48
    }
49
}
50

    
51
function block_nodes_recursively(node)
52
{
53
    block_node(node);
54
    for (const child of node.children)
55
	block_nodes_recursively(child);
56
}
57

    
58
function block_node(node)
59
{
60
    /*
61
     * Modifying <script> element doesn't always prevent its
62
     * execution in some Mozilla browsers. Additional blocking
63
     * through CSP meta tag injection is required.
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_policy(document.URL);
103
    if (targets.policy) {
104
	if (targets.target2 !== undefined)
105
	    window.location.href = targets.base_url + targets.target2;
106
	else
107
	    history.replaceState(null, "", targets.base_url);
108
    }
109
    
110
    targets.policy = targets.current ? targets.policy : {};
111
    
112
    const nonce = targets.policy.nonce || gen_nonce();
113
    start_activity_info_server();
114
    handle_page_actions(nonce);
115

    
116
    if (!targets.policy.allow) {
117
	block_nodes_recursively(document.documentElement);
118

    
119
	if (is_chrome) {
120
	    var observer = new MutationObserver(handle_mutation);
121
	    observer.observe(document.documentElement, {
122
		attributes: true,
123
		childList: true,
124
		subtree: true
125
	    });
126
	}
127

    
128
	if (is_mozilla)
129
	    addEventListener('beforescriptexecute', mozilla_suppress_scripts, true);
130
    }
131
}
(3-3/4)