test_public_urls.py
python
sha256:f01bf034eb409e79373775083966fb6d1ecaefb4ac3b2e7e6d8beaeb555259df
fix(rest): one honest rate-limit contract (#309); the pagin…
Opus 5.5
minor
⚠ breaking
1 day ago
| 1 | """Absolute public URLs come from one canonical base, never from the request scheme (musehub#276). |
| 2 | |
| 3 | PUB_01–04 test the helper as a pure function of settings and the Host value. PUB_30 guards the |
| 4 | codebase: request-derived absolute URLs (``request.base_url``, ``str(request.url)``, the request |
| 5 | scheme) may appear only in the helper, or at an allow-listed site with a reason. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import re |
| 11 | from pathlib import Path |
| 12 | from unittest.mock import patch |
| 13 | |
| 14 | import pytest |
| 15 | from starlette.requests import Request |
| 16 | |
| 17 | from musehub.core.public_urls import public_base_url, public_request_url, public_url_for |
| 18 | |
| 19 | pytestmark = pytest.mark.no_db |
| 20 | |
| 21 | ROOT = Path(__file__).resolve().parents[1] |
| 22 | |
| 23 | |
| 24 | def _request(url: str, host: str | None = None) -> Request: |
| 25 | scheme, rest = url.split("://", 1) |
| 26 | netloc, _, path_qs = rest.partition("/") |
| 27 | path, _, query = ("/" + path_qs).partition("?") |
| 28 | headers = [(b"host", (host or netloc).encode())] |
| 29 | return Request({"type": "http", "scheme": scheme, "method": "GET", "path": path, "query_string": query.encode(), |
| 30 | "headers": headers, "server": (netloc.split(":")[0], 80)}) |
| 31 | |
| 32 | |
| 33 | @pytest.fixture(autouse=True) |
| 34 | def _settings(): |
| 35 | with patch("musehub.core.public_urls.settings") as s: |
| 36 | s.public_url = "https://musehub.ai/" |
| 37 | s.allowed_hosts = ["musehub.ai", "staging.musehub.ai", "localhost:1337"] |
| 38 | yield s |
| 39 | |
| 40 | |
| 41 | def test_pub01_allow_listed_host_seen_as_http_is_https() -> None: |
| 42 | assert public_base_url(_request("http://musehub.ai/x")) == "https://musehub.ai" |
| 43 | assert public_base_url(_request("http://staging.musehub.ai/x")) == "https://staging.musehub.ai" |
| 44 | assert public_base_url(_request("http://localhost:1337/x")) == "https://localhost:1337" |
| 45 | assert public_base_url(_request("http://MuseHub.AI/x")) == "https://musehub.ai" |
| 46 | |
| 47 | |
| 48 | def test_pub02_unknown_or_spoofed_host_is_never_reflected() -> None: |
| 49 | assert public_base_url(_request("http://evil.example/x")) == "https://musehub.ai" |
| 50 | assert public_base_url(_request("http://musehub.ai/x", host="musehub.ai.evil.example")) == "https://musehub.ai" |
| 51 | assert public_base_url(_request("http://test/x")) == "https://musehub.ai" |
| 52 | |
| 53 | |
| 54 | def test_pub03_no_request_is_the_public_url() -> None: |
| 55 | assert public_base_url(None) == "https://musehub.ai" |
| 56 | |
| 57 | |
| 58 | def test_pub04_slashes_are_neither_doubled_nor_dropped() -> None: |
| 59 | req = _request("http://staging.musehub.ai/x") |
| 60 | assert public_url_for(req, "/gabriel/muse") == "https://staging.musehub.ai/gabriel/muse" |
| 61 | assert public_url_for(req, "gabriel/muse") == "https://staging.musehub.ai/gabriel/muse" |
| 62 | assert public_url_for(None, "") == "https://musehub.ai" |
| 63 | assert public_url_for(None, "/") == "https://musehub.ai" |
| 64 | assert public_request_url(_request("http://musehub.ai/gabriel/muse?ref=main&x=1")) == \ |
| 65 | "https://musehub.ai/gabriel/muse?ref=main&x=1" |
| 66 | assert public_request_url(_request("http://musehub.ai/gabriel/muse")) == "https://musehub.ai/gabriel/muse" |
| 67 | |
| 68 | |
| 69 | # ── PUB_30: regression guard ────────────────────────────────────────────────── |
| 70 | |
| 71 | _REQUEST_DERIVED = re.compile(r"request\.base_url|str\(request\.url\)|request\.url\.scheme|\{\{\s*request\.url\s*[|}]") |
| 72 | |
| 73 | # Sites that may keep a request-derived URL, with the reason. Everything else goes through musehub.core.public_urls. |
| 74 | _ALLOWED = { |
| 75 | } |
| 76 | |
| 77 | |
| 78 | def test_pub30_no_request_derived_absolute_urls_outside_the_helper() -> None: |
| 79 | offenders = [] |
| 80 | for path in sorted([*(ROOT / "musehub").rglob("*.py"), *(ROOT / "musehub" / "templates").rglob("*.html")]): |
| 81 | rel = str(path.relative_to(ROOT)) |
| 82 | if rel in _ALLOWED or rel == "musehub/core/public_urls.py": |
| 83 | continue |
| 84 | for n, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1): |
| 85 | if _REQUEST_DERIVED.search(line) and not line.lstrip().startswith(("#", '"', "``")): |
| 86 | offenders.append(f"{rel}:{n}: {line.strip()}") |
| 87 | assert offenders == [] |
File History
3 commits
sha256:f01bf034eb409e79373775083966fb6d1ecaefb4ac3b2e7e6d8beaeb555259df
fix(rest): one honest rate-limit contract (#309); the pagin…
Opus 5.5
minor
⚠
1 day ago
sha256:c2ee54efa85fcbdeea4ffc84a2dd1b8fcd4623463b22e35765df3013a26834aa
merge: #276 public URLs + muse#181 docs onto dev (#313 present)
Opus 5.5
minor
⚠
1 day ago
sha256:c777acd5605d4a83bbf9d2903bff0b0d419325b7bc49317700e4037b56594e8e
fix(web): absolute public URLs come from the canonical publ…
Opus 5.5
minor
⚠
1 day ago