gabriel / musehub public
test_doc_inf_scheme.py python
136 lines 6.6 KB
Raw
sha256:c2ee54efa85fcbdeea4ffc84a2dd1b8fcd4623463b22e35765df3013a26834aa merge: #276 public URLs + muse#181 docs onto dev (#313 present) Opus 5.5 minor ⚠ breaking 1 day ago
1 """DOC_INF_04 (URL scheme) — musehub#242 W2.4.
2
3 Every /muse page is rendered as a visitor on ``https://musehub.ai`` receives it: through the
4 proxy-header handling uvicorn applies in production, with the trusted-proxy setting read from the
5 image's ``entrypoint.sh``, and the request arriving from the Docker bridge gateway, as nginx's
6 connection does. No page may then contain a non-loopback ``http://`` URL: every absolute URL comes
7 from the canonical public base (``musehub.core.public_urls``, musehub#276), never from the request
8 scheme. ``TestCheckerDetects`` proves each rule fires and each exemption holds.
9
10 Out of scope here: live API ``url`` fields. Those endpoints need a database; they build their URLs
11 through the same helper, which ``tests/test_public_urls.py`` pins and guards.
12 """
13
14 from __future__ import annotations
15
16 from pathlib import Path
17
18 import pytest
19
20 from tests.docs_contract import scheme_check as sc
21 from tests.docs_contract.pages import (
22 BRIDGE_GATEWAY_PEER,
23 LOOPBACK_PEER,
24 PAGES,
25 PUBLIC_HOST,
26 render_behind_proxy,
27 )
28
29 _REPO = Path(__file__).resolve().parents[2]
30 _INSTALL = "/install.sh"
31
32
33 @pytest.fixture(scope="module")
34 def trusted_proxies() -> str:
35 values = sc.production_trusted_proxies(
36 (_REPO / "entrypoint.sh").read_text(), (_REPO / "Dockerfile").read_text()
37 )
38 assert values and len(set(values)) == 1, f"entrypoint.sh uvicorn commands disagree on trusted proxies: {values}"
39 return values[0]
40
41
42 @pytest.fixture(scope="module")
43 def served_over_https(trusted_proxies: str) -> dict[str, str]:
44 return render_behind_proxy([*PAGES, _INSTALL], trusted_proxies, BRIDGE_GATEWAY_PEER)
45
46
47 # ── Live: production topology ───────────────────────────────────────────────
48
49 @pytest.mark.parametrize("path", PAGES)
50 def test_page_served_over_https_has_no_http_urls(path: str, served_over_https: dict[str, str]) -> None:
51 assert sc.check_page(path, served_over_https[path]) == []
52
53
54 def test_install_script_served_over_https_downloads_over_https(served_over_https: dict[str, str]) -> None:
55 # The install script embeds the public base too, so it downloads the sdist over https.
56 script = served_over_https[_INSTALL]
57 assert f"https://{PUBLIC_HOST}" in script
58 assert sc.check_text(script) == []
59
60
61 def test_pages_have_no_hard_coded_http_urls() -> None:
62 # With the proxy trusted, request-derived URLs come out https; anything left is written into a template.
63 pages = render_behind_proxy(PAGES, sc.UVICORN_DEFAULT_TRUSTED, LOOPBACK_PEER)
64 assert [p for path, html in pages.items() for p in sc.check_page(path, html)] == []
65
66
67 # ── The checker detects real problems and exempts legitimate syntax ─────────
68
69
70 class TestCheckerDetects:
71 def test_https_relative_and_loopback_urls_are_clean(self) -> None:
72 html = (
73 '<a href="https://musehub.ai/muse">x</a><a href="/muse/wire#push">x</a><a href="#top">x</a>'
74 "<pre>muse remote add local http://localhost:1337/gabriel/muse\n"
75 "curl http://127.0.0.1:1337/healthz\ncurl http://[::1]:1337/mcp\nopen http://app.localhost/</pre>"
76 )
77 assert sc.check_page("p", html) == []
78
79 def test_scheme_mention_without_host_is_not_a_url(self) -> None:
80 assert sc.check_page("p", "<p>You used <code>http://</code> instead of <code>https://</code>.</p>") == []
81
82 def test_xml_namespace_is_exempt_but_a_link_to_the_same_url_is_not(self) -> None:
83 html = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"></svg>'
84 assert sc.check_page("p", html) == []
85 assert sc.check_page("p", '<a href="http://www.w3.org/2000/svg">spec</a>') == [
86 "p: <a href>: non-loopback http:// URL 'http://www.w3.org/2000/svg'"
87 ]
88
89 def test_http_in_attribute_and_text_is_located(self) -> None:
90 html = (
91 '<link rel="canonical" href="http://musehub.ai/muse/wire">'
92 "<pre><code>curl -fsSL HTTP://MuseHub.ai/install.sh | sh</code></pre>"
93 )
94 assert sc.check_page("/muse/wire", html) == [
95 "/muse/wire: <link href>: non-loopback http:// URL 'http://musehub.ai/muse/wire'",
96 "/muse/wire: <code> text: non-loopback http:// URL 'HTTP://MuseHub.ai/install.sh'",
97 ]
98
99 def test_percent_encoded_url_in_query_is_checked(self) -> None:
100 html = '<link href="https://musehub.ai/musehub/oembed?url=http%3A//musehub.ai/muse&amp;format=json">'
101 assert sc.check_page("p", html) == [
102 "p: <link href>: non-loopback http:// URL 'http://musehub.ai/muse' "
103 "(inside the query of 'https://musehub.ai/musehub/oembed?url=http%3A//musehub.ai/muse&format=json')"
104 ]
105
106 def test_entities_are_decoded_and_sentence_punctuation_stripped(self) -> None:
107 assert sc.check_text("see http://musehub.ai/api?a=1&b=2.") == [
108 "non-loopback http:// URL 'http://musehub.ai/api?a=1&b=2'"
109 ]
110 assert sc.check_page("p", "<p>(http://musehub.ai/x)</p>") == ["p: <p> text: non-loopback http:// URL 'http://musehub.ai/x'"]
111
112 def test_malformed_url_is_reported_not_skipped(self) -> None:
113 problems = sc.check_text("curl http://[::1/mcp")
114 assert len(problems) == 1 and problems[0].startswith("malformed URL 'http://[::1/mcp'")
115
116 def test_json_url_fields(self) -> None:
117 payload = {
118 "url": "http://musehub.ai/gabriel/muse/issues/1",
119 "author": {"avatarUrl": "https://musehub.ai/a.png", "html_url": "http://musehub.ai/gabriel"},
120 "links": [{"href": "http://localhost:1337/x"}, {"href": "http://musehub.ai/y"}],
121 "body": "prose mentioning http://musehub.ai is not a URL field",
122 }
123 assert sc.check_json_urls("GET /api/x", payload) == [
124 "GET /api/x: $.url: non-loopback http:// URL 'http://musehub.ai/gabriel/muse/issues/1'",
125 "GET /api/x: $.author.html_url: non-loopback http:// URL 'http://musehub.ai/gabriel'",
126 "GET /api/x: $.links[1].href: non-loopback http:// URL 'http://musehub.ai/y'",
127 ]
128
129 def test_trusted_proxies_are_read_from_the_entrypoint(self) -> None:
130 entry = (
131 'if [ -f /tls/c ]; then\n exec uvicorn musehub.main:app \\\n --proxy-headers \\\n'
132 ' --forwarded-allow-ips "*"\nelse\n exec uvicorn musehub.main:app --proxy-headers\nfi\n'
133 )
134 assert sc.production_trusted_proxies(entry) == ["*", "127.0.0.1"]
135 assert sc.production_trusted_proxies(entry, "ENV FORWARDED_ALLOW_IPS=172.16.0.0/12\n") == ["*", "172.16.0.0/12"]
136 assert sc.production_trusted_proxies("exec uvicorn app --forwarded-allow-ips=10.0.0.1") == ["10.0.0.1"]
File History 5 commits
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
sha256:f7413c1abec6d6a8e5bdabcd4cc7ad5fcd059ce6a6f50f566e2b0118a0a8b578 tests: make tests/docs_contract/ database-free by directory… Opus 5.5 10 days ago
sha256:eb10be5b0151404c767863448333d0cf0ad1b63219b5113abb902cb0132ae67e Merge dev (#242 DOC_INF_04) into task/243-docs-landing (#243) Opus 5.5 10 days ago
sha256:e8b5a50b0a352faa913ea7d5e72e615527564b52466009c56171d1d54f15b7ec test(docs): check that /muse pages served over HTTPS hand o… Opus 5.5 10 days ago