summaryrefslogtreecommitdiff
path: root/background/policy_smuggler.js
blob: 180dcb76b03495abeb18a1da0d56e4729afc3153 (about) (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
65
66
67
/**
* 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;
    const gen_unique = window.gen_unique;

    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];

	let url = url_item(request.url);
	let unique = gen_unique(url);

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

	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 + unique + second_target)]);

	return {
	    redirectUrl : (base_url + unique + 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;
})();