Project

General

Profile

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

haketilo / background / webrequest.js @ 9d825eaa

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 DEBUG
117
    console.debug(`Settings queried using XHR for '${details.url}'.`);
118
#ENDIF
119

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

    
128
	if (details.initiator && !queried_url.startsWith(details.initiator)) {
129
	    console.warn(`Blocked suspicious query of '${url}' by '${details.initiator}'. This might be the result of page fingerprinting the browser.`);
130
	    return {cancel: true};
131
	}
132

    
133
	const policy = decide_policy(tree, details.url, default_allow, secret);
134
	if (!policy.error) {
135
	    const encoded_policy = encodeURIComponent(JSON.stringify(policy));
136
	    return {redirectUrl: redirect_url_template + encoded_policy};
137
	}
138
    }
139

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

    
142
    return {cancel: true};
143
}
144

    
145
const all_types = [
146
    "main_frame", "sub_frame", "stylesheet", "script", "image", "font",
147
    "object", "xmlhttprequest", "ping", "csp_report", "media", "websocket",
148
    "other", "main_frame", "sub_frame"
149
];
150
#ENDIF
151

    
152
async function start(secret_)
153
{
154
    secret = secret_;
155

    
156
#IF CHROMIUM
157
    const extra_opts = ["blocking", "extraHeaders"];
158
#ELSE
159
    const extra_opts = ["blocking"];
160
#ENDIF
161

    
162
    browser.webRequest.onHeadersReceived.addListener(
163
	on_headers_received,
164
	{urls: ["<all_urls>"], types: ["main_frame", "sub_frame"]},
165
	extra_opts.concat("responseHeaders")
166
    );
167

    
168
#IF CHROMIUM && MV2
169
    browser.webRequest.onBeforeRequest.addListener(
170
	on_before_request,
171
	{urls: [browser.runtime.getURL("dummy") + "*"], types: all_types},
172
	extra_opts
173
    );
174
#ENDIF
175

    
176
    await track_default_allow();
177
}
178
#EXPORT start
(10-10/10)