mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-29 12:35:13 +00:00
fix(api): redact error replies, lock segment url lookups, refuse unresolvable proxy hosts and recheck job proxies at start
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""The server spends its own proxy subscriptions only for a key that opts in."""
|
||||
|
||||
import asyncio
|
||||
from datetime import UTC
|
||||
from types import SimpleNamespace
|
||||
|
||||
@@ -33,6 +34,11 @@ def patch_server_region(monkeypatch, region):
|
||||
monkeypatch.setattr(ip_info, "get_ip_info", lambda session=None, cached=False: region and {"country": region})
|
||||
|
||||
|
||||
def answer(monkeypatch, address):
|
||||
"""Make every proxy host name resolve to ``address``."""
|
||||
monkeypatch.setattr(handlers.socket, "getaddrinfo", lambda host, port: [(2, 1, 6, "", (address, 0))])
|
||||
|
||||
|
||||
def patch_providers(monkeypatch, proxy):
|
||||
"""Give the server one provider that answers every country code with ``proxy``."""
|
||||
provider = SimpleNamespace(get_proxy=lambda query: proxy)
|
||||
@@ -78,6 +84,7 @@ def test_region_unknown_no_error(monkeypatch):
|
||||
|
||||
def test_direct_uri_passes_through(monkeypatch):
|
||||
patch_server_region(monkeypatch, "IN")
|
||||
answer(monkeypatch, "8.8.8.8")
|
||||
proxy, providers = handlers.resolve_handler_proxy(
|
||||
{"proxy": "socks5://user:pass@host:1080"}, "EXAMPLE", request("plainkey")
|
||||
)
|
||||
@@ -221,7 +228,8 @@ class TestDownloadGates:
|
||||
job_admission.enforce_download_gates({"service": "EXAMPLE", "proxy": "nordvpn:us"}, request("plainkey"))
|
||||
assert exc_info.value.error_code == APIErrorCode.INVALID_PROXY
|
||||
|
||||
def test_full_uri_passes_when_denied(self):
|
||||
def test_full_uri_passes_when_denied(self, monkeypatch):
|
||||
answer(monkeypatch, "8.8.8.8")
|
||||
job_admission.enforce_download_gates(
|
||||
{"service": "EXAMPLE", "proxy": "socks5://user:pass@host:1080"}, request("plainkey")
|
||||
)
|
||||
@@ -293,6 +301,12 @@ class TestDownloadEnforcement:
|
||||
created = created_params({"service": "EXAMPLE", "title_id": "t1"}, request("proxykey"))
|
||||
assert created["server_proxy"] is True
|
||||
|
||||
@pytest.mark.parametrize(("key", "stamp"), [("plainkey", False), ("proxykey", True), ("adminkey", True)])
|
||||
def test_the_local_proxy_stamp_records_the_key_at_submit(self, created_params, key, stamp):
|
||||
"""A key removed by a reload before the job starts reads as admin then, so the stamp keeps the submit view."""
|
||||
created = created_params({"service": "EXAMPLE", "title_id": "t1", "local_proxy": True}, request(key))
|
||||
assert created["local_proxy"] is stamp
|
||||
|
||||
@pytest.mark.parametrize(("params", "expected"), [({}, []), ({"server_proxy": True}, ["server"])])
|
||||
def test_perform_download_maps_stamp_to_dl_providers(self, monkeypatch, tmp_path, params, expected):
|
||||
"""An unstamped or denied job hands dl an empty provider list; a stamped one gets the server's list."""
|
||||
@@ -402,12 +416,66 @@ def test_a_client_region_that_is_no_region_code_gets_no_proxy(monkeypatch, clien
|
||||
|
||||
|
||||
def test_hostname_resolving_to_a_private_address_refused(monkeypatch):
|
||||
monkeypatch.setattr(handlers.socket, "getaddrinfo", lambda host, port: [(2, 1, 6, "", ("10.0.0.7", 0))])
|
||||
answer(monkeypatch, "10.0.0.7")
|
||||
with pytest.raises(APIError) as exc_info:
|
||||
handlers.resolve_handler_proxy({"proxy": "http://proxy.lan:3128"}, "EXAMPLE", request("plainkey"))
|
||||
assert exc_info.value.error_code == APIErrorCode.INVALID_PROXY
|
||||
|
||||
|
||||
def test_a_proxy_host_that_does_not_resolve_is_refused():
|
||||
"""A name that fails at check time can answer a local address when the client connects, so it is refused."""
|
||||
with pytest.raises(APIError) as exc_info:
|
||||
handlers.resolve_handler_proxy({"proxy": "http://unresolvable.invalid:3128"}, "EXAMPLE", request("plainkey"))
|
||||
assert exc_info.value.error_code == APIErrorCode.INVALID_PROXY
|
||||
assert exc_info.value.message == "Proxy host unresolvable.invalid does not resolve"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("stamp", "refused"), [(False, True), (True, False), (None, True)])
|
||||
@pytest.mark.parametrize("field", ["proxy", "proxy_download"])
|
||||
def test_a_job_checks_its_proxy_again_when_it_starts(monkeypatch, field, stamp, refused):
|
||||
"""A host that answered public at submit and answers private at start stops a job not stamped local_proxy."""
|
||||
params = {field: "http://rebind.example:3128", "local_proxy": stamp}
|
||||
answer(monkeypatch, "8.8.8.8")
|
||||
job_admission.check_job_start("EXAMPLE", params)
|
||||
job_admission.check_job_start("EXAMPLE", {**params, "no_proxy": True})
|
||||
|
||||
answer(monkeypatch, "127.0.0.1")
|
||||
job_admission.check_job_start("EXAMPLE", {**params, "no_proxy": True})
|
||||
if not refused:
|
||||
job_admission.check_job_start("EXAMPLE", params)
|
||||
return
|
||||
with pytest.raises(APIError) as exc_info:
|
||||
job_admission.check_job_start("EXAMPLE", params)
|
||||
assert exc_info.value.error_code == APIErrorCode.INVALID_PROXY
|
||||
assert exc_info.value.message == "Proxy host rebind.example is not a public address"
|
||||
|
||||
|
||||
def test_a_job_refused_at_start_never_runs_the_worker(monkeypatch):
|
||||
from datetime import datetime
|
||||
|
||||
from unshackle.core.api.download_manager import DownloadJob, DownloadQueueManager, JobStatus
|
||||
|
||||
async def never(job):
|
||||
raise AssertionError("the worker ran")
|
||||
|
||||
answer(monkeypatch, "10.0.0.7")
|
||||
manager = DownloadQueueManager()
|
||||
monkeypatch.setattr(manager, "run_download_async", never)
|
||||
job = DownloadJob(
|
||||
"job1",
|
||||
JobStatus.DOWNLOADING,
|
||||
datetime.now(),
|
||||
"EXAMPLE",
|
||||
"t1",
|
||||
{"proxy": "http://rebind.example:3128"},
|
||||
owner_key="plainkey",
|
||||
)
|
||||
with pytest.raises(APIError):
|
||||
asyncio.run(manager.execute_download(job))
|
||||
assert job.status is JobStatus.FAILED
|
||||
assert job.error_code == "INVALID_PROXY"
|
||||
|
||||
|
||||
def test_local_proxy_uri_ignored_under_no_proxy():
|
||||
handlers.resolve_handler_proxy({"proxy": "http://127.0.0.1:8080", "no_proxy": True}, "EXAMPLE", request("plainkey"))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user