summaryrefslogtreecommitdiff
path: root/tests/unit/components/test_hostblock.py
blob: 00a7a5f8fb0b94bd573b0f9b194e78b60b17c58f (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
#!/usr/bin/env python3

# Copyright 2015 Corentin Julé <corentinjule@gmail.com>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser.  If not, see <https://www.gnu.org/licenses/>.

import pathlib
import zipfile
import logging

import pytest

from PyQt5.QtCore import QUrl

from qutebrowser.components import hostblock
from qutebrowser.utils import urlmatch
from helpers import testutils


pytestmark = pytest.mark.usefixtures("qapp")

# TODO See ../utils/test_standarddirutils for OSError and caplog assertion

WHITELISTED_HOSTS = ("qutebrowser.org", "mediumhost.io", "http://*.edu")

BLOCKLIST_HOSTS = (
    "localhost",
    "mediumhost.io",
    "malware.badhost.org",
    "4-verybadhost.com",
    "ads.worsthostever.net",
)

CLEAN_HOSTS = ("goodhost.gov", "verygoodhost.com")

URLS_TO_CHECK = (
    "http://localhost",
    "http://mediumhost.io",
    "ftp://malware.badhost.org",
    "http://4-verybadhost.com",
    "http://ads.worsthostever.net",
    "http://goodhost.gov",
    "ftp://verygoodhost.com",
    "http://qutebrowser.org",
    "http://veryverygoodhost.edu",
)


@pytest.fixture
def host_blocker_factory(config_tmpdir, data_tmpdir, download_stub, config_stub):
    def factory():
        return hostblock.HostBlocker(config_dir=config_tmpdir, data_dir=data_tmpdir)

    return factory


def create_zipfile(directory, files, zipname="test"):
    """Return a path to a newly created zip file.

    Args:
        directory: path object where to create the zip file.
        files: list of pathlib.Paths (relative to directory) to each file to add.
        zipname: name to give to the zip file.
    """
    zipfile_path = (directory / zipname).with_suffix(".zip")
    with zipfile.ZipFile(zipfile_path, "w") as new_zipfile:
        for file_path in files:
            new_zipfile.write(directory / file_path, arcname=file_path.name)
            # Removes path from file name
    return pathlib.Path(zipname + ".zip")


def create_blocklist(
    directory, blocked_hosts=BLOCKLIST_HOSTS, name="hosts", line_format="one_per_line"
):
    """Return a path to a blocklist file.

    Args:
        directory: path object where to create the blocklist file
        blocked_hosts: an iterable of string hosts to add to the blocklist
        name: name to give to the blocklist file
        line_format: 'etc_hosts'  -->  /etc/hosts format
                    'one_per_line'  -->  one host per line format
                    'not_correct'  -->  Not a correct hosts file format.
    """
    blocklist_file = directory / name
    with blocklist_file.open("w", encoding="UTF-8") as blocklist:
        # ensure comments are ignored when processing blocklist
        blocklist.write("# Blocked Hosts List #\n\n")
        if line_format == "etc_hosts":  # /etc/hosts like format
            for host in blocked_hosts:
                blocklist.write("127.0.0.1  " + host + "\n")
        elif line_format == "one_per_line":
            for host in blocked_hosts:
                blocklist.write(host + "\n")
        elif line_format == "not_correct":
            for host in blocked_hosts:
                blocklist.write(host + " This is not a correct hosts file\n")
        else:
            raise ValueError("Incorrect line_format argument")
    return pathlib.Path(name)


def assert_urls(
    host_blocker,
    blocked=BLOCKLIST_HOSTS,
    whitelisted=WHITELISTED_HOSTS,
    urls_to_check=URLS_TO_CHECK,
):
    """Test if Urls to check are blocked or not by HostBlocker.

    Ensure URLs in 'blocked' and not in 'whitelisted' are blocked.
    All other URLs must not be blocked.

    localhost is an example of a special case that shouldn't be blocked.
    """
    whitelisted = list(whitelisted) + ["localhost"]
    for str_url in urls_to_check:
        url = QUrl(str_url)
        host = url.host()
        if host in blocked and host not in whitelisted:
            assert host_blocker._is_blocked(url)
        else:
            assert not host_blocker._is_blocked(url)


def blocklist_to_url(path):
    """Get an example.com-URL with the given filename as path."""
    assert not path.is_absolute(), path
    url = QUrl("http://example.com/")
    url.setPath("/" + str(path))
    assert url.isValid(), url.errorString()
    return url


def generic_blocklists(directory):
    """Return a generic list of files to be used in hosts-block-lists option.

    This list contains :
    - a remote zip file with 1 hosts file and 2 useless files
    - a remote zip file with only useless files
        (Should raise a FileNotFoundError)
    - a remote zip file with only one valid hosts file
    - a local text file with valid hosts
    - a remote text file without valid hosts format.
    """
    # remote zip file with 1 hosts file and 2 useless files
    file1 = create_blocklist(
        directory, blocked_hosts=CLEAN_HOSTS, name="README", line_format="not_correct"
    )
    file2 = create_blocklist(
        directory,
        blocked_hosts=BLOCKLIST_HOSTS[:3],
        name="hosts",
        line_format="etc_hosts",
    )
    file3 = create_blocklist(
        directory,
        blocked_hosts=CLEAN_HOSTS,
        name="false_positive",
        line_format="one_per_line",
    )
    files_to_zip = [file1, file2, file3]
    blocklist1 = blocklist_to_url(create_zipfile(directory, files_to_zip, "block1"))

    # remote zip file without file named hosts
    # (Should raise a FileNotFoundError)
    file1 = create_blocklist(
        directory, blocked_hosts=CLEAN_HOSTS, name="md5sum", line_format="etc_hosts"
    )
    file2 = create_blocklist(
        directory, blocked_hosts=CLEAN_HOSTS, name="README", line_format="not_correct"
    )
    file3 = create_blocklist(
        directory,
        blocked_hosts=CLEAN_HOSTS,
        name="false_positive",
        line_format="one_per_line",
    )
    files_to_zip = [file1, file2, file3]
    blocklist2 = blocklist_to_url(create_zipfile(directory, files_to_zip, "block2"))

    # remote zip file with only one valid hosts file inside
    file1 = create_blocklist(
        directory,
        blocked_hosts=[BLOCKLIST_HOSTS[3]],
        name="malwarelist",
        line_format="etc_hosts",
    )
    blocklist3 = blocklist_to_url(create_zipfile(directory, [file1], "block3"))

    # local text file with valid hosts
    blocklist4 = QUrl.fromLocalFile(
        str(
            directory
            / create_blocklist(
                directory,
                blocked_hosts=[BLOCKLIST_HOSTS[4]],
                name="mycustomblocklist",
                line_format="one_per_line",
            )
        )
    )
    assert blocklist4.isValid(), blocklist4.errorString()

    # remote text file without valid hosts format
    blocklist5 = blocklist_to_url(
        create_blocklist(
            directory,
            blocked_hosts=CLEAN_HOSTS,
            name="notcorrectlist",
            line_format="not_correct",
        )
    )

    return [
        blocklist1.toString(),
        blocklist2.toString(),
        blocklist3.toString(),
        blocklist4.toString(),
        blocklist5.toString(),
    ]


@pytest.mark.parametrize(
    "blocking_enabled, method",
    [
        # Assuming the adblock dependency is installed
        (True, "auto"),
        (True, "adblock"),
        (False, "auto"),
        (False, "adblock"),
        (False, "both"),
        (False, "hosts"),
    ],
)
def test_disabled_blocking_update(
    config_stub, tmp_path, caplog, host_blocker_factory, blocking_enabled, method
):
    """Ensure no URL is blocked when host blocking should be disabled."""
    if blocking_enabled and method == 'auto':
        pytest.importorskip('adblock')

    config_stub.val.content.blocking.hosts.lists = generic_blocklists(tmp_path)
    config_stub.val.content.blocking.enabled = blocking_enabled
    config_stub.val.content.blocking.method = method

    host_blocker = host_blocker_factory()
    downloads = host_blocker.adblock_update()
    while downloads._in_progress:
        current_download = downloads._in_progress[0]
        with caplog.at_level(logging.ERROR):
            current_download.successful = True
            current_download.finished.emit()
    host_blocker.read_hosts()
    for str_url in URLS_TO_CHECK:
        assert not host_blocker._is_blocked(QUrl(str_url))


def test_disabled_blocking_per_url(config_stub, host_blocker_factory):
    example_com = "https://www.example.com/"

    config_stub.val.content.blocking.method = "hosts"
    config_stub.val.content.blocking.hosts.lists = []
    pattern = urlmatch.UrlPattern(example_com)
    config_stub.set_obj("content.blocking.enabled", False, pattern=pattern)

    url = QUrl("https://blocked.example.com")

    host_blocker = host_blocker_factory()
    host_blocker._blocked_hosts.add(url.host())

    assert host_blocker._is_blocked(url)
    assert not host_blocker._is_blocked(url, first_party_url=QUrl(example_com))


def test_no_blocklist_update(config_stub, download_stub, host_blocker_factory):
    """Ensure no URL is blocked when no block list exists."""
    config_stub.val.content.blocking.hosts.lists = None
    config_stub.val.content.blocking.method = "hosts"
    config_stub.val.content.blocking.enabled = True

    host_blocker = host_blocker_factory()
    host_blocker.adblock_update()
    host_blocker.read_hosts()
    for dl in download_stub.downloads:
        dl.successful = True
    for str_url in URLS_TO_CHECK:
        assert not host_blocker._is_blocked(QUrl(str_url))


def test_successful_update(config_stub, tmp_path, caplog, host_blocker_factory):
    """Ensure hosts from host_blocking.lists are blocked after an update."""
    config_stub.val.content.blocking.hosts.lists = generic_blocklists(tmp_path)
    config_stub.val.content.blocking.method = "hosts"
    config_stub.val.content.blocking.enabled = True
    config_stub.val.content.blocking.whitelist = None

    host_blocker = host_blocker_factory()
    downloads = host_blocker.adblock_update()
    # Simulate download is finished
    while downloads._in_progress:
        current_download = downloads._in_progress[0]
        with caplog.at_level(logging.ERROR):
            current_download.successful = True
            current_download.finished.emit()
    host_blocker.read_hosts()
    assert_urls(host_blocker, whitelisted=[])


def test_parsing_multiple_hosts_on_line(config_stub, host_blocker_factory):
    """Ensure multiple hosts on a line get parsed correctly."""
    config_stub.val.content.blocking.method = "hosts"
    config_stub.val.content.blocking.enabled = True

    host_blocker = host_blocker_factory()
    bytes_host_line = " ".join(BLOCKLIST_HOSTS).encode("utf-8")
    parsed_hosts = host_blocker._read_hosts_line(bytes_host_line)
    host_blocker._blocked_hosts |= parsed_hosts
    assert_urls(host_blocker, whitelisted=[])


@pytest.mark.parametrize(
    "ip, host",
    [
        ("127.0.0.1", "localhost"),
        ("27.0.0.1", "localhost.localdomain"),
        ("27.0.0.1", "local"),
        ("55.255.255.255", "broadcasthost"),
        (":1", "localhost"),
        (":1", "ip6-localhost"),
        (":1", "ip6-loopback"),
        ("e80::1%lo0", "localhost"),
        ("f00::0", "ip6-localnet"),
        ("f00::0", "ip6-mcastprefix"),
        ("f02::1", "ip6-allnodes"),
        ("f02::2", "ip6-allrouters"),
        ("ff02::3", "ip6-allhosts"),
        (".0.0.0", "0.0.0.0"),
        ("127.0.1.1", "myhostname"),
        ("127.0.0.53", "myhostname"),
    ],
)
def test_whitelisted_lines(host_blocker_factory, ip, host):
    """Make sure we don't block hosts we don't want to."""
    host_blocker = host_blocker_factory()
    line = ("{} {}".format(ip, host)).encode("ascii")
    parsed_hosts = host_blocker._read_hosts_line(line)
    assert host not in parsed_hosts


def test_failed_dl_update(config_stub, tmp_path, caplog, host_blocker_factory):
    """One blocklist fails to download.

    Ensure hosts from this list are not blocked.
    """
    dl_fail_blocklist = blocklist_to_url(
        create_blocklist(
            tmp_path,
            blocked_hosts=CLEAN_HOSTS,
            name="download_will_fail",
            line_format="one_per_line",
        )
    )
    hosts_to_block = generic_blocklists(tmp_path) + [dl_fail_blocklist.toString()]
    config_stub.val.content.blocking.hosts.lists = hosts_to_block
    config_stub.val.content.blocking.enabled = True
    config_stub.val.content.blocking.method = "hosts"
    config_stub.val.content.blocking.whitelist = None

    host_blocker = host_blocker_factory()
    downloads = host_blocker.adblock_update()
    while downloads._in_progress:
        current_download = downloads._in_progress[0]
        # if current download is the file we want to fail, make it fail
        if current_download.name == dl_fail_blocklist.path():
            current_download.successful = False
        else:
            current_download.successful = True
        with caplog.at_level(logging.ERROR):
            current_download.finished.emit()
    host_blocker.read_hosts()
    assert_urls(host_blocker, whitelisted=[])


@pytest.mark.parametrize("location", ["content", "comment"])
def test_invalid_utf8(config_stub, tmp_path, caplog, host_blocker_factory, location):
    """Make sure invalid UTF-8 is handled correctly.

    See https://github.com/qutebrowser/qutebrowser/issues/2301
    """
    blocklist = tmp_path / "blocklist"
    if location == "comment":
        blocklist.write_bytes(b"# nbsp: \xa0\n")
    else:
        assert location == "content"
        blocklist.write_bytes(b"https://www.example.org/\xa0")
    with blocklist.open("a") as f:
        for url in BLOCKLIST_HOSTS:
            f.write(url + "\n")

    url = blocklist_to_url(pathlib.Path("blocklist"))
    config_stub.val.content.blocking.hosts.lists = [url.toString()]
    config_stub.val.content.blocking.enabled = True
    config_stub.val.content.blocking.method = "hosts"
    config_stub.val.content.blocking.whitelist = None

    host_blocker = host_blocker_factory()
    downloads = host_blocker.adblock_update()
    current_download = downloads._in_progress[0]

    if location == "content":
        with caplog.at_level(logging.ERROR):
            current_download.successful = True
            current_download.finished.emit()
        expected = r"Failed to decode: " r"b'https://www.example.org/\xa0localhost"
        assert caplog.messages[-2].startswith(expected)
    else:
        current_download.successful = True
        current_download.finished.emit()

    host_blocker.read_hosts()
    assert_urls(host_blocker, whitelisted=[])


def test_invalid_utf8_compiled(
    config_stub, config_tmpdir, data_tmpdir, monkeypatch, caplog, host_blocker_factory
):
    """Make sure invalid UTF-8 in the compiled file is handled."""
    config_stub.val.content.blocking.hosts.lists = []

    # Make sure the HostBlocker doesn't delete blocked-hosts in __init__
    monkeypatch.setattr(hostblock.HostBlocker, "update_files", lambda _self: None)

    (config_tmpdir / "blocked-hosts").write_binary(b"https://www.example.org/\xa0")
    (data_tmpdir / "blocked-hosts").ensure()

    host_blocker = host_blocker_factory()
    with caplog.at_level(logging.ERROR):
        host_blocker.read_hosts()
    assert caplog.messages[-1] == "Failed to read host blocklist!"


def test_blocking_with_whitelist(config_stub, data_tmpdir, host_blocker_factory):
    """Ensure hosts in content.blocking.whitelist are never blocked."""
    # Simulate adblock_update has already been run
    # by creating a file named blocked-hosts,
    # Exclude localhost from it as localhost is never blocked via list
    filtered_blocked_hosts = BLOCKLIST_HOSTS[1:]
    blocklist = create_blocklist(
        data_tmpdir,
        blocked_hosts=filtered_blocked_hosts,
        name="blocked-hosts",
        line_format="one_per_line",
    )
    config_stub.val.content.blocking.hosts.lists = [str(blocklist)]
    config_stub.val.content.blocking.enabled = True
    config_stub.val.content.blocking.method = "hosts"
    config_stub.val.content.blocking.whitelist = list(WHITELISTED_HOSTS)

    host_blocker = host_blocker_factory()
    host_blocker.read_hosts()
    assert_urls(host_blocker)


def test_config_change_initial(config_stub, tmp_path, host_blocker_factory):
    """Test emptying host_blocking.lists with existing blocked_hosts.

    - A blocklist is present in host_blocking.lists and blocked_hosts is
      populated
    - User quits qutebrowser, empties host_blocking.lists from his config
    - User restarts qutebrowser, does adblock-update
    """
    create_blocklist(
        tmp_path,
        blocked_hosts=BLOCKLIST_HOSTS,
        name="blocked-hosts",
        line_format="one_per_line",
    )
    config_stub.val.content.blocking.hosts.lists = None
    config_stub.val.content.blocking.enabled = True
    config_stub.val.content.blocking.method = "hosts"
    config_stub.val.content.blocking.whitelist = None

    host_blocker = host_blocker_factory()
    host_blocker.read_hosts()
    for str_url in URLS_TO_CHECK:
        assert not host_blocker._is_blocked(QUrl(str_url))


def test_config_change(config_stub, tmp_path, host_blocker_factory):
    """Ensure blocked-hosts resets if host-block-list is changed to None."""
    filtered_blocked_hosts = BLOCKLIST_HOSTS[1:]  # Exclude localhost
    blocklist = blocklist_to_url(
        create_blocklist(
            tmp_path,
            blocked_hosts=filtered_blocked_hosts,
            name="blocked-hosts",
            line_format="one_per_line",
        )
    )
    config_stub.val.content.blocking.hosts.lists = [blocklist.toString()]
    config_stub.val.content.blocking.enabled = True
    config_stub.val.content.blocking.method = "hosts"
    config_stub.val.content.blocking.whitelist = None

    host_blocker = host_blocker_factory()
    host_blocker.read_hosts()
    config_stub.val.content.blocking.hosts.lists = None
    host_blocker.read_hosts()
    for str_url in URLS_TO_CHECK:
        assert not host_blocker._is_blocked(QUrl(str_url))


def test_add_directory(config_stub, tmp_path, host_blocker_factory):
    """Ensure adblocker can import all files in a directory."""
    blocklist_hosts2 = []
    for i in BLOCKLIST_HOSTS[1:]:
        blocklist_hosts2.append("1" + i)

    create_blocklist(
        tmp_path,
        blocked_hosts=BLOCKLIST_HOSTS,
        name="blocked-hosts",
        line_format="one_per_line",
    )
    create_blocklist(
        tmp_path,
        blocked_hosts=blocklist_hosts2,
        name="blocked-hosts2",
        line_format="one_per_line",
    )

    config_stub.val.content.blocking.hosts.lists = [str(tmp_path)]
    config_stub.val.content.blocking.enabled = True
    config_stub.val.content.blocking.method = "hosts"
    host_blocker = host_blocker_factory()
    host_blocker.adblock_update()
    assert len(host_blocker._blocked_hosts) == len(blocklist_hosts2) * 2


def test_adblock_benchmark(data_tmpdir, benchmark, host_blocker_factory):
    blocked_hosts = data_tmpdir / "blocked-hosts"
    blocked_hosts.write_text("\n".join(testutils.blocked_hosts()), encoding="utf-8")

    url = QUrl("https://www.example.org/")
    blocker = host_blocker_factory()
    blocker.read_hosts()
    assert blocker._blocked_hosts

    benchmark(lambda: blocker._is_blocked(url))


def test_subdomain_blocking(config_stub, host_blocker_factory):
    config_stub.val.content.blocking.method = "hosts"
    config_stub.val.content.blocking.hosts.lists = None
    host_blocker = host_blocker_factory()
    host_blocker._blocked_hosts.add("example.com")
    assert host_blocker._is_blocked(QUrl("https://subdomain.example.com"))