Project

General

Profile

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

haketilo / test / haketilo_test / unit / test_policy_enforcing.py @ 6ca07019

1
# SPDX-License-Identifier: CC0-1.0
2

    
3
"""
4
Haketilo unit tests - enforcing script blocking policy from content script
5
"""
6

    
7
# This file is part of Haketilo
8
#
9
# Copyright (C) 2022 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 pytest
21
import json
22
import urllib.parse
23
from selenium.webdriver.support.ui import WebDriverWait
24

    
25
from ..script_loader import load_script
26
from .utils import are_scripts_allowed
27

    
28
# For simplicity, we'll use one nonce in all test cases.
29
nonce = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
30

    
31
allow_policy = {'allow': True}
32
block_policy = {
33
    'allow': False,
34
    'csp': f"prefetch-src 'none'; script-src-attr 'none'; script-src 'none'; script-src-elem 'none'; frame-src http://* https://*;"
35
}
36
payload_policy = {
37
    'mapping': 'somemapping',
38
    'payload': {'identifier': 'someresource'},
39
    'csp': f"prefetch-src 'none'; script-src-attr 'none'; script-src 'nonce-{nonce}'; script-src-elem 'nonce-{nonce}';"
40
}
41

    
42
def content_script():
43
    return load_script('content/policy_enforcing.js') + \
44
        content_script_appended_code
45

    
46
content_script_appended_code = ''';{
47
const smuggled_what_to_do = /^[^#]*#?(.*)$/.exec(document.URL)[1];
48
const what_to_do = smuggled_what_to_do === "" ? {policy: {allow: true}} :
49
                   JSON.parse(decodeURIComponent(smuggled_what_to_do));
50

    
51
if (what_to_do.csp_off) {
52
    const orig_DOMParser = window.DOMParser;
53
    window.DOMParser = function() {
54
        const parser = new orig_DOMParser();
55
        this.parseFromString = () => parser.parseFromString('', 'text/html');
56
    }
57
}
58

    
59
enforce_blocking(what_to_do.policy);
60
}'''
61

    
62
def get(driver, page, what_to_do):
63
    driver.get(page + '#' + urllib.parse.quote(json.dumps(what_to_do)))
64
    driver.execute_script('window.before_reload = true; location.reload();')
65
    done = lambda _: not driver.execute_script('return window.before_reload;')
66
    WebDriverWait(driver, 10).until(done)
67

    
68
@pytest.mark.ext_data({'content_script': content_script})
69
@pytest.mark.usefixtures('webextension')
70
# Under Mozilla we use several mechanisms of script blocking. Some serve as
71
# fallbacks in case others break. CSP one of those mechanisms. Here we run the
72
# test once with CSP blocking on and once without it. This allows us to verify
73
# that the CSP-less blocking approaches by themselves also work. We don't do the
74
# reverse (CSP on and other mechanisms off) because CSP rules added through
75
# <meta> injection are not reliable enough - they do not always take effect
76
# immediately and there's nothing we can do to fix it.
77
@pytest.mark.parametrize('csp_off_setting', [{}, {'csp_off': True}])
78
def test_policy_enforcing_html(driver, execute_in_page, csp_off_setting):
79
    """
80
    A test case of sanitizing <script>s and intrinsic JavaScript in HTML pages.
81
    """
82
    def click_all():
83
        for i in range(1, 3):
84
            driver.find_element_by_id(f'clickme{i}').click()
85

    
86
    def assert_properly_blocked():
87
        click_all()
88

    
89
        assert set(driver.execute_script('return window.__run || [];')) == set()
90
        assert bool(csp_off_setting) == are_scripts_allowed(driver)
91

    
92
        for attr in ('onclick', 'href', 'src', 'data'):
93
            elem = driver.find_element_by_css_selector(f'[blocked-{attr}]')
94

    
95
            assert 'blocked' in elem.get_attribute(attr)
96
            assert '__run = [...(' in elem.get_attribute(f'blocked-{attr}')
97

    
98
        but1 = driver.find_element_by_id('clickme1')
99
        assert but1.get_attribute('blocked-blocked-onclick') == \
100
            "some useful data"
101

    
102
    # First, see if scripts run when not blocked.
103
    get(driver, 'https://gotmyowndoma.in/scripts_to_block_1.html', {
104
        'policy': allow_policy,
105
        **csp_off_setting
106
    })
107

    
108
    click_all()
109

    
110
    assert set(driver.execute_script('return window.__run || [];')) == \
111
        {'inline', 'on', 'href', 'src', 'data'}
112
    assert are_scripts_allowed(driver)
113

    
114
    # Now, verify scripts don't run when blocked.
115
    get(driver, 'https://gotmyowndoma.in/scripts_to_block_1.html', {
116
        'policy': block_policy,
117
        **csp_off_setting
118
    })
119

    
120
    assert_properly_blocked()
121

    
122
    # Now, verify only scripts with nonce can run when payload is injected.
123
    get(driver, 'https://gotmyowndoma.in/scripts_to_block_1.html', {
124
        'policy': payload_policy,
125
        **csp_off_setting
126
    })
127

    
128
    assert_properly_blocked()
129
    assert are_scripts_allowed(driver, nonce)
130

    
131
# Test function analogous to that for HTML page.
132
@pytest.mark.ext_data({'content_script': content_script})
133
@pytest.mark.usefixtures('webextension')
134
@pytest.mark.parametrize('csp_off_setting', [{}, {'csp_off': True}])
135
def test_policy_enforcing_xml(driver, execute_in_page, csp_off_setting):
136
    """
137
    A test case of sanitizing <script>s and intrinsic JavaScript in XML
138
    documents.
139
    """
140
    def click_all():
141
        for name in ('idaret', 'nowamak', 'mango', 'annoying'):
142
            elem = driver.find_element_by_id(f'{name}_circle')
143
            try:
144
                elem.click()
145
            except:
146
                pass
147

    
148
    def assert_properly_blocked():
149
        click_all()
150

    
151
        assert set(driver.execute_script('return window.__run || [];')) == set()
152
        assert bool(csp_off_setting) == are_scripts_allowed(driver)
153

    
154
    # First, see if scripts run when not blocked.
155
    get(driver, 'https://gotmyowndoma.in/scripts_to_block_2.xml', {
156
        'policy': allow_policy,
157
        **csp_off_setting
158
    })
159

    
160
    click_all()
161

    
162
    assert set(driver.execute_script('return window.__run || [];')) == \
163
        {'grape', 'raspberry', 'idaret', 'melon'}
164
    assert are_scripts_allowed(driver)
165

    
166
    # Now, verify scripts don't run when blocked.
167
    get(driver, 'https://gotmyowndoma.in/scripts_to_block_2.xml', {
168
        'policy': block_policy,
169
        **csp_off_setting
170
    })
171

    
172
    assert_properly_blocked()
173

    
174
    # Now, verify only scripts with nonce can run when payload is injected.
175
    get(driver, 'https://gotmyowndoma.in/scripts_to_block_2.xml', {
176
        'policy': payload_policy,
177
        **csp_off_setting
178
    })
179

    
180
    assert_properly_blocked()
181
    assert are_scripts_allowed(driver, nonce)
(18-18/25)