gabriel / musehub public
test_doc_inf_scheme.py python
145 lines 7.2 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. The known failure is
7 strict-xfailed against #242 (the fix is tracked separately), so the fix flips it to XPASS → failure
8 until the marker is removed. ``TestCheckerDetects`` proves each rule fires and each exemption holds.
9
10 Out of scope here: live API ``url`` fields. Every endpoint that builds one from the request needs a
11 database, so they are checked by ``check_json_urls`` on fixtures now and live under the W4 QA stack.
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 # Known finding (2026-11-06, musehub dev 0e4f9453): request-derived absolute URLs (site_base_url(),
50 # the oEmbed link's request.url) render as http:// on every page.
51 _HTTP_ON_HTTPS = pytest.mark.xfail(
52 strict=True,
53 raises=AssertionError, # only the finding's own assert; a setup error stays an error (musehub#273)
54 reason="musehub#242: request-derived absolute URLs ignore the forwarded https scheme (fix tracked separately)",
55 )
56
57
58 @pytest.mark.parametrize("path", [pytest.param(p, marks=_HTTP_ON_HTTPS) for p in PAGES])
59 def test_page_served_over_https_has_no_http_urls(path: str, served_over_https: dict[str, str]) -> None:
60 assert sc.check_page(path, served_over_https[path]) == []
61
62
63 def test_install_script_served_over_https_downloads_over_https(served_over_https: dict[str, str]) -> None:
64 # Positive control: install.py's _base_url honours X-Forwarded-Proto itself, so the harness must see https.
65 script = served_over_https[_INSTALL]
66 assert f"https://{PUBLIC_HOST}" in script
67 assert sc.check_text(script) == []
68
69
70 def test_pages_have_no_hard_coded_http_urls() -> None:
71 # With the proxy trusted, request-derived URLs come out https; anything left is written into a template.
72 pages = render_behind_proxy(PAGES, sc.UVICORN_DEFAULT_TRUSTED, LOOPBACK_PEER)
73 assert [p for path, html in pages.items() for p in sc.check_page(path, html)] == []
74
75
76 # ── The checker detects real problems and exempts legitimate syntax ─────────
77
78
79 class TestCheckerDetects:
80 def test_https_relative_and_loopback_urls_are_clean(self) -> None:
81 html = (
82 '<a href="https://musehub.ai/muse">x</a><a href="/muse/wire#push">x</a><a href="#top">x</a>'
83 "<pre>muse remote add local http://localhost:1337/gabriel/muse\n"
84 "curl http://127.0.0.1:1337/healthz\ncurl http://[::1]:1337/mcp\nopen http://app.localhost/</pre>"
85 )
86 assert sc.check_page("p", html) == []
87
88 def test_scheme_mention_without_host_is_not_a_url(self) -> None:
89 assert sc.check_page("p", "<p>You used <code>http://</code> instead of <code>https://</code>.</p>") == []
90
91 def test_xml_namespace_is_exempt_but_a_link_to_the_same_url_is_not(self) -> None:
92 html = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"></svg>'
93 assert sc.check_page("p", html) == []
94 assert sc.check_page("p", '<a href="http://www.w3.org/2000/svg">spec</a>') == [
95 "p: <a href>: non-loopback http:// URL 'http://www.w3.org/2000/svg'"
96 ]
97
98 def test_http_in_attribute_and_text_is_located(self) -> None:
99 html = (
100 '<link rel="canonical" href="http://musehub.ai/muse/wire">'
101 "<pre><code>curl -fsSL HTTP://MuseHub.ai/install.sh | sh</code></pre>"
102 )
103 assert sc.check_page("/muse/wire", html) == [
104 "/muse/wire: <link href>: non-loopback http:// URL 'http://musehub.ai/muse/wire'",
105 "/muse/wire: <code> text: non-loopback http:// URL 'HTTP://MuseHub.ai/install.sh'",
106 ]
107
108 def test_percent_encoded_url_in_query_is_checked(self) -> None:
109 html = '<link href="https://musehub.ai/musehub/oembed?url=http%3A//musehub.ai/muse&amp;format=json">'
110 assert sc.check_page("p", html) == [
111 "p: <link href>: non-loopback http:// URL 'http://musehub.ai/muse' "
112 "(inside the query of 'https://musehub.ai/musehub/oembed?url=http%3A//musehub.ai/muse&format=json')"
113 ]
114
115 def test_entities_are_decoded_and_sentence_punctuation_stripped(self) -> None:
116 assert sc.check_text("see http://musehub.ai/api?a=1&b=2.") == [
117 "non-loopback http:// URL 'http://musehub.ai/api?a=1&b=2'"
118 ]
119 assert sc.check_page("p", "<p>(http://musehub.ai/x)</p>") == ["p: <p> text: non-loopback http:// URL 'http://musehub.ai/x'"]
120
121 def test_malformed_url_is_reported_not_skipped(self) -> None:
122 problems = sc.check_text("curl http://[::1/mcp")
123 assert len(problems) == 1 and problems[0].startswith("malformed URL 'http://[::1/mcp'")
124
125 def test_json_url_fields(self) -> None:
126 payload = {
127 "url": "http://musehub.ai/gabriel/muse/issues/1",
128 "author": {"avatarUrl": "https://musehub.ai/a.png", "html_url": "http://musehub.ai/gabriel"},
129 "links": [{"href": "http://localhost:1337/x"}, {"href": "http://musehub.ai/y"}],
130 "body": "prose mentioning http://musehub.ai is not a URL field",
131 }
132 assert sc.check_json_urls("GET /api/x", payload) == [
133 "GET /api/x: $.url: non-loopback http:// URL 'http://musehub.ai/gabriel/muse/issues/1'",
134 "GET /api/x: $.author.html_url: non-loopback http:// URL 'http://musehub.ai/gabriel'",
135 "GET /api/x: $.links[1].href: non-loopback http:// URL 'http://musehub.ai/y'",
136 ]
137
138 def test_trusted_proxies_are_read_from_the_entrypoint(self) -> None:
139 entry = (
140 'if [ -f /tls/c ]; then\n exec uvicorn musehub.main:app \\\n --proxy-headers \\\n'
141 ' --forwarded-allow-ips "*"\nelse\n exec uvicorn musehub.main:app --proxy-headers\nfi\n'
142 )
143 assert sc.production_trusted_proxies(entry) == ["*", "127.0.0.1"]
144 assert sc.production_trusted_proxies(entry, "ENV FORWARDED_ALLOW_IPS=172.16.0.0/12\n") == ["*", "172.16.0.0/12"]
145 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