Project

General

Profile

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

haketilo / background / main.js @ 263d03d5

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Main background script.
5
 *
6
 * Copyright (C) 2021 Wojtek Kosior
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * As additional permission under GNU GPL version 3 section 7, you
19
 * may distribute forms of that code without the copy of the GNU
20
 * GPL normally required by section 4, provided you include this
21
 * license notice and, in case of non-source distribution, a URL
22
 * through which recipients can access the Corresponding Source.
23
 * If you modify file(s) with this exception, you may extend this
24
 * exception to your version of the file(s), but you are not
25
 * obligated to do so. If you do not wish to do so, delete this
26
 * exception statement from your version.
27
 *
28
 * As a special exception to the GPL, any HTML file which merely
29
 * makes function calls to this code, and for that purpose
30
 * includes it by reference shall be deemed a separate work for
31
 * copyright law purposes. If you modify this code, you may extend
32
 * this exception to your version of the code, but you are not
33
 * obligated to do so. If you do not wish to do so, delete this
34
 * exception statement from your version.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
38
 *
39
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
40
 * license. Although I request that you do not make use this code in a
41
 * proprietary program, I am not going to enforce this in court.
42
 */
43

    
44
/*
45
 * IMPORTS_START
46
 * IMPORT TYPE_PREFIX
47
 * IMPORT get_storage
48
 * IMPORT light_storage
49
 * IMPORT start_storage_server
50
 * IMPORT start_page_actions_server
51
 * IMPORT browser
52
 * IMPORT is_privileged_url
53
 * IMPORT query_best
54
 * IMPORT gen_nonce
55
 * IMPORT inject_csp_headers
56
 * IMPORT apply_stream_filter
57
 * IMPORT filter_cookie_headers
58
 * IMPORT is_chrome
59
 * IMPORTS_END
60
 */
61

    
62
start_storage_server();
63
start_page_actions_server();
64

    
65
async function init_ext(install_details)
66
{
67
    if (install_details.reason != "install")
68
	return;
69

    
70
    let storage = await get_storage();
71

    
72
    await storage.clear();
73

    
74
    /*
75
     * Below we add sample settings to the extension.
76
     */
77

    
78
    for (let setting of // The next line is replaced with the contents of /default_settings.json by the build script
79
        `DEFAULT SETTINGS`
80
    ) {
81
	let [key, value] = Object.entries(setting)[0];
82
	storage.set(key[0], key.substring(1), value);
83
    }
84
}
85

    
86
browser.runtime.onInstalled.addListener(init_ext);
87

    
88

    
89
let storage;
90
let policy_observable = {};
91

    
92
function on_headers_received(details)
93
{
94
    const url = details.url;
95
    if (is_privileged_url(details.url))
96
	return;
97

    
98
    const [pattern, settings] = query_best(storage, details.url);
99
    const has_payload = !!(settings && settings.components);
100
    const allow = !has_payload &&
101
	  !!(settings ? settings.allow : policy_observable.value);
102
    const nonce = gen_nonce();
103
    const policy = {allow, url, nonce, has_payload};
104

    
105
    let headers = details.responseHeaders;
106
    let skip = false;
107
    for (const header of headers) {
108
	if ((header.name.toLowerCase().trim() === "content-disposition" &&
109
	     /^\s*attachment\s*(;.*)$/i.test(header.value)))
110
	    skip = true;
111
    }
112

    
113
    headers = inject_csp_headers(headers, policy);
114

    
115
    skip = skip || (details.statusCode >= 300 && details.statusCode < 400);
116
    if (!skip) {
117
	/* Check for API availability. */
118
	if (browser.webRequest.filterResponseData)
119
	    headers = apply_stream_filter(details, headers, policy);
120
    }
121

    
122
    return {responseHeaders: headers};
123
}
124

    
125
function on_before_send_headers(details)
126
{
127
    let headers = details.requestHeaders;
128
    headers = filter_cookie_headers(headers);
129
    return {requestHeaders: headers};
130
}
131

    
132
const all_types = [
133
    "main_frame", "sub_frame", "stylesheet", "script", "image", "font",
134
    "object", "xmlhttprequest", "ping", "csp_report", "media", "websocket",
135
    "other", "main_frame", "sub_frame"
136
];
137

    
138
async function start_webRequest_operations()
139
{
140
    storage = await get_storage();
141

    
142
    const extra_opts = ["blocking"];
143
    if (is_chrome)
144
	extra_opts.push("extraHeaders");
145

    
146
    browser.webRequest.onHeadersReceived.addListener(
147
	on_headers_received,
148
	{urls: ["<all_urls>"], types: ["main_frame", "sub_frame"]},
149
	extra_opts.concat("responseHeaders")
150
    );
151

    
152
    browser.webRequest.onBeforeSendHeaders.addListener(
153
	on_before_send_headers,
154
	{urls: ["<all_urls>"], types: all_types},
155
	extra_opts.concat("requestHeaders")
156
    );
157

    
158
    policy_observable = await light_storage.observe_var("default_allow");
159
}
160

    
161
start_webRequest_operations();
(2-2/7)