aboutsummaryrefslogtreecommitdiff
path: root/background/policy_smuggler.js
blob: 6d0da38eaca057870eed221b0bf465010069c0d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Myext smuggling policy to content script through url
*
* Copyright (C) 2021 Wojtek Kosior
*
* Dual-licensed under:
*   - 0BSD license
*   - GPLv3 or (at your option) any later version
*/

"use strict";

(() => {
    const TYPE_PREFIX = window.TYPE_PREFIX;
    const get_storage = window.get_storage;
    const browser = window.browser;
    const url_item = window.url_item;

    var storage;

    function redirect(request)
    {
	let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/;
	let match = url_re.exec(request.url);
	let base_url = match[1];
	let first_target = match[3];
	let second_target = match[4];

	if (first_target === "#myext-allow") {
	    console.log(["not redirecting"]);
	    return {cancel : false};
	}

	let url = url_item(request.url);
	let settings = storage.get(TYPE_PREFIX.PAGE, url);
	console.log("got", storage.get(TYPE_PREFIX.PAGE, url), "for", url);
	if (settings === undefined || !settings.allow)
	    return {cancel : false};

	second_target = (first_target || "") + (second_target || "")

	console.log(["redirecting", request.url,
		     (base_url + "#myext-allow" + second_target)]);

	return {
	    redirectUrl : (base_url + "#myext-allow" + second_target)
	};
    }

    async function start() {
	storage = await get_storage();

	chrome.webRequest.onBeforeRequest.addListener(
	    redirect,
	    {
		urls: ["<all_urls>"],
		types: ["main_frame", "sub_frame"]
	    },
	    ["blocking"]
	);
    }

    window.start_policy_smuggler = start;
})();