File size: 2,772 Bytes
1d697a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest

import fetch


@pytest.fixture(autouse=True)
def _public_dns(monkeypatch):
    # Resolve every allowed host to a public address so tests need no network.
    monkeypatch.setattr(fetch.socket, "getaddrinfo",
                        lambda host, *a, **k: [(None, None, None, None, ("142.250.1.1", 0))])


@pytest.mark.parametrize("url", [
    "https://www.youtube.com/watch?v=abc",
    "https://youtu.be/abc",
    "https://m.youtube.com/watch?v=abc",
    "https://vimeo.com/12345",
    "https://www.facebook.com/x/videos/1",
    "https://fb.watch/abc/",
    "https://www.tiktok.com/@u/video/1",
    "https://x.com/u/status/1",
    "https://www.instagram.com/reel/abc/",
])
def test_allowed(url):
    assert fetch.check_url(url) is None


@pytest.mark.parametrize("url", [
    "ftp://youtube.com/x",
    "file:///etc/passwd",
    "https://example.com/video.mp4",
    "https://youtube.com.evil.net/x",
    "https://evilyoutube.com/x",
    "http://127.0.0.1/x",
    "http://[::1]/x",
    "http://169.254.169.254/latest/meta-data",
    "https://user:pw@youtube.com/x",
    "https://youtube.com:8080/x",
    "",
    "not a url",
])
def test_refused(url):
    assert fetch.check_url(url) is not None


def test_private_resolution_refused(monkeypatch):
    monkeypatch.setattr(fetch.socket, "getaddrinfo",
                        lambda host, *a, **k: [(None, None, None, None, ("10.0.0.5", 0))])
    assert fetch.check_url("https://www.youtube.com/watch?v=abc") is not None


def test_unresolvable_refused(monkeypatch):
    def boom(*a, **k):
        raise OSError("nxdomain")
    monkeypatch.setattr(fetch.socket, "getaddrinfo", boom)
    assert "can't be reached" in fetch.check_url("https://vimeo.com/1")


def test_generic_extractor_not_allowed():
    assert "generic" not in fetch.ALLOWED_EXTRACTORS or "generic:unsupported" in fetch.ALLOWED_EXTRACTORS
    assert not any(e == "generic" for e in fetch.ALLOWED_EXTRACTORS)


def test_cookie_filter(monkeypatch, tmp_path):
    raw = ("# Netscape HTTP Cookie File\n"
           ".youtube.com\tTRUE\t/\tTRUE\t0\tSID\tabc\n"
           ".bank.example\tTRUE\t/\tTRUE\t0\tSESSION\tsecret\n"
           "accounts.google.com\tFALSE\t/\tTRUE\t0\tX\ty\n")
    monkeypatch.setenv("YT_DLP_COOKIES", raw)
    monkeypatch.delenv("YT_DLP_COOKIES_B64", raising=False)
    monkeypatch.setattr(fetch, "_cookie_path", None)
    p = fetch._cookiefile()
    assert p
    text = open(p, encoding="utf-8").read()
    assert "youtube.com" in text and "accounts.google.com" in text
    assert "bank.example" not in text


def test_bad_token_short_circuits(monkeypatch):
    monkeypatch.setattr(fetch, "verify_usage_token", lambda t, s=None: False)
    assert fetch.fetch_video("https://youtu.be/x", "nope") == (None, "Not authorized.")