Project

General

Profile

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

haketilo / content / main.js @ 6bae771d

1
/**
2
 * Myext main content script run in all frames
3
 *
4
 * Copyright (C) 2021 Wojtek Kosior
5
 *
6
 * This code is dual-licensed under:
7
 * - Asshole license 1.0,
8
 * - GPLv3 or (at your option) any later version
9
 *
10
 * "dual-licensed" means you can choose the license you prefer.
11
 *
12
 * This code is released under a permissive license because I disapprove of
13
 * copyright and wouldn't be willing to sue a violator. Despite not putting
14
 * this code under copyleft (which is also kind of copyright), I do not want
15
 * it to be made proprietary. Hence, the permissive alternative to GPL is the
16
 * Asshole license 1.0 that allows me to call you an asshole if you use it.
17
 * This means you're legally ok regardless of how you utilize this code but if
18
 * you make it into something nonfree, you're an asshole.
19
 *
20
 * You should have received a copy of both GPLv3 and Asshole license 1.0
21
 * together with this code. If not, please see:
22
 * - https://www.gnu.org/licenses/gpl-3.0.en.html
23
 * - https://koszko.org/asshole-license.txt
24
 */
25

    
26
"use strict";
27

    
28
(() => {
29
    const handle_page_actions = window.handle_page_actions;
30
    const url_item = window.url_item;
31
    const gen_unique = window.gen_unique;
32

    
33
    var url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/;
34
    var match = url_re.exec(document.URL);
35
    var base_url = match[1];
36
    var first_target = match[3];
37
    var second_target = match[4];
38

    
39
    // TODO: can be refactored *a little bit* with policy_smuggler.js
40
    let url = url_item(document.URL);
41
    let unique = gen_unique(url);
42

    
43
    let nonce = unique.substring(1);
44

    
45
    var block = true;
46
    if (first_target !== undefined &&
47
	first_target === unique) {
48
	block = false;
49
	console.log(["allowing", document.URL]);
50
	if (second_target !== undefined)
51
	    window.location.href = base_url + second_target;
52
	else
53
	    history.replaceState(null, "", base_url);
54
    } else {
55
	console.log(["not allowing", document.URL]);
56
    }
57

    
58
    function handle_mutation(mutations, observer)
59
    {
60
	if (document.readyState === 'complete') {
61
	    console.log("complete");
62
	    observer.disconnect();
63
	    return;
64
	}
65
	for (let mutation of mutations) {
66
	    for (let node of mutation.addedNodes) {
67
		/*
68
		 * Modifying <script> element doesn't always prevent its
69
		 * execution in some Mozilla browsers. Additional blocking
70
		 * through CSP meta tag injection is required.
71
		 */
72
		if (node.tagName === "SCRIPT") {
73
		    block_script(node);
74
		    continue;
75
		}
76

    
77
		sanitize_attributes(node);
78

    
79
		if (node.tagName === "HEAD")
80
		    inject_csp(node);
81
	    }
82
	}
83
    }
84

    
85
    function block_script(node)
86
    {
87
	console.log(node);
88

    
89
	/*
90
	 * Disabling scripts this way allows them to still be relatively
91
	 * easily accessed in case they contain some useful data.
92
	 */
93
	if (node.hasAttribute("type"))
94
	    node.setAttribute("blocked-type", node.getAttribute("type"));
95
	node.setAttribute("type", "application/json");
96
    }
97

    
98
    function inject_csp(node)
99
    {
100
	console.log('injecting CSP');
101
	let meta = document.createElement("meta");
102
	meta.setAttribute("http-equiv", "Content-Security-Policy");
103
	meta.setAttribute("content", `\
104
script-src 'nonce-${nonce}'; \
105
script-src-elem 'nonce-${nonce}';\
106
`);
107
	node.appendChild(meta);
108
    }
109

    
110
    function sanitize_attributes(node)
111
    {
112
	if (node.attributes === undefined)
113
	    return;
114

    
115
	/*
116
	 * We have to do it in 2 loops, removing attribute modifies
117
	 * our iterator
118
	 */
119
	let attr_names = [];
120
	for (let attr of node.attributes) {
121
	    let attr_name = attr.localName;
122
	    if (attr_name.startsWith("on"))
123
		attr_names.push(attr_name);
124
	}
125

    
126
	for (let attr_name of attr_names) {
127
	    node.removeAttribute(attr_name);
128
	    console.log("sanitized", attr_name);
129
	}
130
    }
131

    
132
    if (block) {
133
	var observer = new MutationObserver(handle_mutation);
134
	observer.observe(document.documentElement, {
135
	    attributes: true,
136
	    childList: true,
137
	    subtree: true
138
	});
139
    }
140

    
141
    handle_page_actions(nonce);
142
})();
(1-1/2)