1
|
# SPDX-License-Identifier: CC0-1.0
|
2
|
|
3
|
"""
|
4
|
Haketilo unit tests - modifying requests using webRequest API
|
5
|
"""
|
6
|
|
7
|
# This file is part of Haketilo
|
8
|
#
|
9
|
# Copyright (C) 2021, Wojtek Kosior <koszko@koszko.org>
|
10
|
#
|
11
|
# This program is free software: you can redistribute it and/or modify
|
12
|
# it under the terms of the CC0 1.0 Universal License as published by
|
13
|
# the Creative Commons Corporation.
|
14
|
#
|
15
|
# This program is distributed in the hope that it will be useful,
|
16
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
# CC0 1.0 Universal License for more details.
|
19
|
|
20
|
import re
|
21
|
from hashlib import sha256
|
22
|
import pytest
|
23
|
|
24
|
from ..script_loader import load_script
|
25
|
from .utils import are_scripts_allowed
|
26
|
|
27
|
def webrequest_js():
|
28
|
return (load_script('background/webrequest.js',
|
29
|
'#IMPORT common/patterns_query_tree.js AS pqt') +
|
30
|
''';
|
31
|
// Mock pattern tree.
|
32
|
tree = pqt.make();
|
33
|
// Mock default allow.
|
34
|
default_allow = {name: "default_allow", value: true};
|
35
|
|
36
|
// Rule to block scripts.
|
37
|
pqt.register(tree, "https://site.with.scripts.block.ed/***",
|
38
|
"~allow", 0);
|
39
|
|
40
|
// Rule to allow scripts, but overridden by payload assignment.
|
41
|
pqt.register(tree, "https://site.with.paylo.ad/***", "~allow", 1);
|
42
|
pqt.register(tree, "https://site.with.paylo.ad/***",
|
43
|
"somemapping", {identifier: "someresource"});
|
44
|
|
45
|
// Mock stream_filter.
|
46
|
stream_filter.apply = (details, headers, policy) => headers;
|
47
|
|
48
|
// Mock secret and start webrequest operations.
|
49
|
start("somesecret");
|
50
|
''')
|
51
|
|
52
|
@pytest.mark.ext_data({'background_script': webrequest_js})
|
53
|
@pytest.mark.usefixtures('webextension')
|
54
|
def test_on_headers_received(driver, execute_in_page):
|
55
|
for attempt in range(10):
|
56
|
driver.get('https://site.with.scripts.block.ed/')
|
57
|
|
58
|
if not are_scripts_allowed(driver):
|
59
|
break
|
60
|
assert attempt != 9
|
61
|
|
62
|
driver.get('https://site.with.scripts.allow.ed/')
|
63
|
assert are_scripts_allowed(driver)
|
64
|
|
65
|
driver.get('https://site.with.paylo.ad/')
|
66
|
assert not are_scripts_allowed(driver)
|
67
|
source = 'somemapping:someresource:https://site.with.paylo.ad/index.html:somesecret'
|
68
|
assert are_scripts_allowed(driver, sha256(source.encode()).digest().hex())
|