Project

General

Profile

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

haketilo / background / webrequest.js @ cb70284d

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Modify HTTP traffic usng webRequest API.
5
 *
6
 * Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
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 of this code in a
41
 * proprietary program, I am not going to enforce this in court.
42
 */
43

    
44
#IMPORT common/indexeddb.js        AS haketilodb
45
#IF MOZILLA
46
#IMPORT background/stream_filter.js
47
#ENDIF
48

    
49
#FROM common/browser.js IMPORT browser
50
#FROM common/misc.js    IMPORT is_privileged_url, csp_header_regex
51
#FROM common/policy.js  IMPORT decide_policy
52

    
53
#FROM background/patterns_query_manager.js IMPORT tree, default_allow
54

    
55
let secret;
56

    
57
function on_headers_received(details)
58
{
59
    const url = details.url;
60
    if (is_privileged_url(details.url))
61
	return;
62

    
63
    let headers = details.responseHeaders;
64

    
65
    const policy =
66
	  decide_policy(tree, details.url, !!default_allow.value, secret);
67
    if (policy.allow)
68
	return;
69

    
70
    if (policy.payload)
71
	headers = headers.filter(h => !csp_header_regex.test(h.name));
72

    
73
    headers.push({name: "Content-Security-Policy", value: policy.csp});
74

    
75
#IF MOZILLA
76
    let skip = false;
77
    for (const header of headers) {
78
	if (header.name.toLowerCase().trim() !== "content-disposition")
79
	    continue;
80

    
81
	if (/^\s*attachment\s*(;.*)$/i.test(header.value)) {
82
		skip = true;
83
	} else {
84
	    skip = false;
85
	    break;
86
	}
87
    }
88
    skip = skip || (details.statusCode >= 300 && details.statusCode < 400);
89

    
90
    if (!skip)
91
	headers = stream_filter.apply(details, headers, policy);
92
#ENDIF
93

    
94
    return {responseHeaders: headers};
95
}
96

    
97
#IF CHROMIUM && MV2
98
const request_url_regex = /^[^?]*\?url=(.*)$/;
99
const redirect_url_template = browser.runtime.getURL("dummy") + "?settings=";
100

    
101
function on_before_request(details)
102
{
103
    /*
104
     * Content script will make a synchronous XmlHttpRequest to extension's
105
     * `dummy` file to query settings for given URL. We smuggle that
106
     * information in query parameter of the URL we redirect to.
107
     * A risk of fingerprinting arises if a page with script execution allowed
108
     * guesses the dummy file URL and makes an AJAX call to it. It is currently
109
     * a problem in ManifestV2 Chromium-family port of Haketilo because Chromium
110
     * uses predictable URLs for web-accessible resources. We plan to fix it in
111
     * the future ManifestV3 port.
112
     */
113
    if (details.type !== "xmlhttprequest")
114
	return {cancel: true};
115

    
116
    if (details.url.startsWith(redirect_url_template))
117
	return;
118

    
119
#IF DEBUG
120
    console.debug(`Haketilo: Settings queried using XHR for '${details.url}'.`);
121
#ENDIF
122

    
123
    /*
124
     * request_url should be of the following format:
125
     *     <url_for_extension's_dummy_file>?url=<valid_urlencoded_url>
126
     */
127
    const match = request_url_regex.exec(details.url);
128
    if (match) {
129
	const queried_url = decodeURIComponent(match[1]);
130

    
131
	if (details.initiator && details.initiator !== "null" &&
132
	    !queried_url.startsWith(details.initiator)) {
133
	    console.warn(`Haketilo: Blocked suspicious query of '${queried_url}' by '${details.initiator}'. This might be the result of page fingerprinting the browser.`);
134
	    return {cancel: true};
135
	}
136

    
137
	const policy =
138
	      decide_policy(tree, queried_url, !!default_allow.value, secret);
139
	if (!policy.error) {
140
	    const encoded_policy = encodeURIComponent(JSON.stringify(policy));
141
	    return {redirectUrl: redirect_url_template + encoded_policy};
142
	}
143
    }
144

    
145
    console.warn(`Haketilo: Bad request! Expected ${browser.runtime.getURL("dummy")}?url=<valid_urlencoded_url>. Got ${details.url}. This might be the result of page fingerprinting the browser.`);
146

    
147
    return {cancel: true};
148
}
149

    
150
const all_types = [
151
    "main_frame", "sub_frame", "stylesheet", "script", "image", "font",
152
    "object", "xmlhttprequest", "ping", "csp_report", "media", "websocket",
153
    "other", "main_frame", "sub_frame"
154
];
155
#ENDIF
156

    
157
async function start(secret_)
158
{
159
    secret = secret_;
160

    
161
#IF CHROMIUM
162
    const extra_opts = ["blocking", "extraHeaders"];
163
#ELSE
164
    const extra_opts = ["blocking"];
165
#ENDIF
166

    
167
    browser.webRequest.onHeadersReceived.addListener(
168
	on_headers_received,
169
	{urls: ["<all_urls>"], types: ["main_frame", "sub_frame"]},
170
	extra_opts.concat("responseHeaders")
171
    );
172

    
173
#IF CHROMIUM && MV2
174
    browser.webRequest.onBeforeRequest.addListener(
175
	on_before_request,
176
	{urls: [browser.runtime.getURL("dummy") + "*"], types: all_types},
177
	extra_opts
178
    );
179
#ENDIF
180
}
181
#EXPORT start
(7-7/7)