test_security.py
python
sha256:e1629e5f328982dbf6e535d3f253993c78ff225dfee4ebb0e0a15fc1db69046b
merge: rc25 — docs and docs-contract tests match Muse batch…
Opus 5.5
minor
⚠ breaking
2 days ago
| 1 | """DOC_SEC — /muse/security contract (musehub#260, QA contract v2). |
| 2 | |
| 3 | Every claim on the page was checked on 2026-11-09 against musehub ``dev`` 259e6968 and muse 0.2.1rc20 (muse ``dev`` 5becda10) |
| 4 | in a disposable sandbox (isolated HOME, no identity, no hub; loopback mock servers only, a throwaway in-memory key). The guard |
| 5 | cards' "applied at" lists, the trust-boundary map, the threat rows and the size-cap table come from ``docs_security_data``; these |
| 6 | tests re-derive each from the code the page documents (an AST scan of the installed ``muse`` package, the constants, and the |
| 7 | behaviour itself), so a guard added, moved or removed on the engine side fails here (DOC_SEC_04). Known product defects stay |
| 8 | documented as contract plus warning (#241 defect-preservation rule): each is pinned by a strict xfail that flips when its ticket |
| 9 | is fixed, so the warning on the page must be removed in the same change. |
| 10 | |
| 11 | DOC_SEC_01 (commands parse), _02 (anchors and section labels) and _03 (URL scheme) are the #242 checkers. DOC_SEC_04 is |
| 12 | ``TestCallSites``; _05 is ``TestCounts``; _06 is ``TestGuardBehaviour`` and ``TestTransport``; _07 is ``TestWording``. No database is used. |
| 13 | """ |
| 14 | |
| 15 | from __future__ import annotations |
| 16 | |
| 17 | import ast |
| 18 | import html as html_lib |
| 19 | import http.server |
| 20 | import os |
| 21 | import re |
| 22 | import subprocess |
| 23 | import sys |
| 24 | import threading |
| 25 | from pathlib import Path |
| 26 | from typing import Any |
| 27 | |
| 28 | import muse |
| 29 | import pytest |
| 30 | from muse.core import snapshot as snap |
| 31 | from muse.core import transport as tr |
| 32 | from muse.core import validation as val |
| 33 | from muse.core.identity import _save_all, hostname_from_url |
| 34 | from muse.core.xml_safe import SafeET |
| 35 | |
| 36 | from musehub.api.routes.musehub import docs_security_data as data |
| 37 | from musehub.api.routes.musehub.ui_docs import _PHASES |
| 38 | from tests.docs_contract import registry_check as rc |
| 39 | |
| 40 | pytestmark = pytest.mark.no_db |
| 41 | |
| 42 | PAGE = rc.phase_path("security") |
| 43 | MUSE_ISSUES = "https://staging.musehub.ai/gabriel/muse/issues/" |
| 44 | _C_HELPERS = "215" # SysEx cap, provenance U+202E, branch C1, helpers without call sites |
| 45 | |
| 46 | _PKG = Path(muse.__file__).resolve().parent |
| 47 | |
| 48 | |
| 49 | @pytest.fixture(scope="module") |
| 50 | def html(rendered: dict[str, str]) -> str: |
| 51 | return html_lib.unescape(rendered[PAGE]) |
| 52 | |
| 53 | |
| 54 | @pytest.fixture(scope="module") |
| 55 | def text(html: str) -> str: |
| 56 | return " ".join(re.sub(r"<[^>]+>", "", html).split()) |
| 57 | |
| 58 | |
| 59 | # ── AST scan of the installed package: the same logic that produced ``SITES`` ──────────────────────────────── |
| 60 | |
| 61 | |
| 62 | def _scan(names: set[str]) -> dict[str, list[tuple[str, str]]]: |
| 63 | found: dict[str, list[tuple[str, str]]] = {n: [] for n in names} |
| 64 | |
| 65 | class Visitor(ast.NodeVisitor): |
| 66 | def __init__(self, rel: str) -> None: |
| 67 | self.rel, self.stack = rel, [] |
| 68 | |
| 69 | def _scope(self, node: Any) -> None: |
| 70 | self.stack.append(node.name) |
| 71 | self.generic_visit(node) |
| 72 | self.stack.pop() |
| 73 | |
| 74 | visit_FunctionDef = visit_AsyncFunctionDef = visit_ClassDef = _scope # noqa: N815 |
| 75 | |
| 76 | def visit_Call(self, node: ast.Call) -> None: |
| 77 | f = node.func |
| 78 | name = f.id if isinstance(f, ast.Name) else f.attr if isinstance(f, ast.Attribute) else None |
| 79 | base = f.value.id if isinstance(f, ast.Attribute) and isinstance(f.value, ast.Name) else None |
| 80 | hit = name if name in names else base if base in names else None |
| 81 | if hit: |
| 82 | found[hit].append((self.rel, ".".join(self.stack) or "<module>")) |
| 83 | self.generic_visit(node) |
| 84 | |
| 85 | for path in sorted(_PKG.rglob("*.py")): |
| 86 | try: |
| 87 | tree = ast.parse(path.read_text(encoding="utf-8")) |
| 88 | except (SyntaxError, UnicodeDecodeError): |
| 89 | continue |
| 90 | Visitor(path.relative_to(_PKG).as_posix()).visit(tree) |
| 91 | return found |
| 92 | |
| 93 | |
| 94 | @pytest.fixture(scope="module") |
| 95 | def scan() -> dict[str, list[tuple[str, str]]]: |
| 96 | return _scan(set(data.SITES) | {"sanitize_display", "clamp_int", "make_transport", "walk_workdir", "compute_snapshot_id"}) |
| 97 | |
| 98 | |
| 99 | def _commands(sites: list[tuple[str, str]]) -> tuple[str, ...]: |
| 100 | stems = {Path(m).stem for m, _ in sites if m.startswith("cli/commands/")} |
| 101 | return tuple(sorted(s for s in stems if not s.startswith("_"))) |
| 102 | |
| 103 | |
| 104 | def _core(sites: list[tuple[str, str]]) -> tuple[str, ...]: |
| 105 | return tuple(sorted({m for m, _ in sites if not m.startswith("cli/commands/")})) |
| 106 | |
| 107 | |
| 108 | # ── DOC_SEC_04: "applied at" lists are the real call graph ─────────────────────────────────────────────────── |
| 109 | |
| 110 | |
| 111 | class TestCallSites: |
| 112 | @pytest.mark.parametrize("guard", list(data.SITES)) |
| 113 | def test_pinned_sites_equal_the_ast_scan(self, scan: dict[str, list[tuple[str, str]]], guard: str) -> None: |
| 114 | sites = scan[guard] |
| 115 | pinned = data.SITES[guard] |
| 116 | assert len(sites) == pinned.sites, guard |
| 117 | assert len({m for m, _ in sites}) == pinned.modules, guard |
| 118 | assert _commands(sites) == pinned.commands, guard |
| 119 | assert _core(sites) == pinned.core, guard |
| 120 | |
| 121 | def test_the_two_widest_guards_stay_above_their_floors_and_name_real_commands(self, scan: dict[str, list[tuple[str, str]]]) -> None: |
| 122 | disp = {Path(m).stem for m, _ in scan["sanitize_display"] if m.startswith("cli/commands/")} |
| 123 | assert len(disp) >= data.FLOORS["sanitize_display"] and set(data.DISPLAY_COMMANDS) <= disp |
| 124 | clamp = {Path(m).stem for m, _ in scan["clamp_int"] if m.startswith("cli/commands/")} |
| 125 | assert len({m for m, _ in scan["clamp_int"]}) >= data.FLOORS["clamp_int"] and set(data.CLAMP_COMMANDS) <= clamp |
| 126 | |
| 127 | def test_the_transport_commands_are_exactly_the_make_transport_callers(self, scan: dict[str, list[tuple[str, str]]]) -> None: |
| 128 | assert set(_commands(scan["make_transport"])) == set(data.TRANSPORT_COMMANDS) |
| 129 | |
| 130 | def test_command_modules_that_are_not_top_level_commands_are_written_as_module_names(self, html: str) -> None: |
| 131 | for name in data.NOT_TOP_LEVEL: |
| 132 | assert f"<code>{name}</code>" in html or name not in {c for s in data.SITES.values() for c in s.commands} |
| 133 | assert f"muse {name.replace('_', '-')}" not in html, name |
| 134 | assert data.NOT_TOP_LEVEL <= {c for s in data.SITES.values() for c in s.commands} | set(data.CLAMP_COMMANDS) |
| 135 | assert "docs_cmd" in data.NOT_TOP_LEVEL |
| 136 | |
| 137 | def test_helpers_with_no_call_sites_are_the_ones_the_page_marks(self, scan: dict[str, list[tuple[str, str]]]) -> None: |
| 138 | zero = {g for g, s in data.SITES.items() if s.sites == 0} |
| 139 | assert zero == {"clamp_natural", "finite_float", "sanitize_token", "SafeET"} |
| 140 | assert all(not scan[g] for g in zero) |
| 141 | |
| 142 | def test_the_commit_snapshot_walker_has_the_documented_callers(self, scan: dict[str, list[tuple[str, str]]]) -> None: |
| 143 | callers = {f for _, f in scan["walk_workdir"]} |
| 144 | assert {"build_snapshot_manifest", "run", "verify_workdir_integrity"} <= callers |
| 145 | assert {f for _, f in scan["compute_snapshot_id"]} == {"_verify_snapshot_id"} |
| 146 | |
| 147 | |
| 148 | # ── DOC_SEC_05: counts, anchors and the map ───────────────────────────────────────────────────────────────── |
| 149 | |
| 150 | |
| 151 | class TestCounts: |
| 152 | def test_the_guard_count_is_the_number_of_cards_everywhere(self, html: str, text: str) -> None: |
| 153 | cards = re.findall(r'<div class="sec-guard sec-guard--[a-z-]+" id="([a-z-]+)"', html) |
| 154 | assert cards == [g.anchor for g in data.GUARDS] and len(cards) == data.GUARD_COUNT |
| 155 | assert f"The {data.GUARD_COUNT} guards" in text |
| 156 | assert f"{data.GUARD_COUNT} guards are described" in text |
| 157 | |
| 158 | def test_no_fourteen_and_the_registry_card_has_no_number(self, text: str) -> None: |
| 159 | assert "Fourteen" not in text and "fourteen" not in text |
| 160 | desc = {slug: d for slug, _n, _t, d in _PHASES}["security"] |
| 161 | assert not re.search(r"\b(\w+teen|\d+) (layered )?guards\b", desc, re.I) |
| 162 | |
| 163 | def test_every_id_is_unique_and_every_in_page_link_resolves(self, html: str) -> None: |
| 164 | article = html[html.index("<article") :] # the page's own ids; the shared layout and icon sprite are not this page's |
| 165 | ids = re.findall(r'\sid="([^"]+)"', article) |
| 166 | assert len(ids) == len(set(ids)) |
| 167 | assert 'id="size-caps"' in html and 'id="size-caps-table"' in html |
| 168 | sidebar = html[: html.index("<article")] |
| 169 | for target in re.findall(r'href="#([^"]+)"', html): |
| 170 | assert target in ids or f'id="{target}"' in sidebar, target |
| 171 | |
| 172 | def test_threat_rows_link_cards_and_cover_the_same_14_threats(self, html: str) -> None: |
| 173 | anchors = {g.anchor for g in data.GUARDS} |
| 174 | assert len(data.THREATS) == 14 and all(t.anchor in anchors for t in data.THREATS) |
| 175 | assert html.count('<tr data-status="') == 14 |
| 176 | by_status = {s: sum(1 for t in data.THREATS if t.status == s) for s in ("applied", "partial", "helper-only")} |
| 177 | assert by_status == {"applied": 8, "partial": 4, "helper-only": 2} |
| 178 | |
| 179 | def test_the_map_rows_link_cards_and_dashed_rows_are_the_zero_call_site_helpers(self, html: str) -> None: |
| 180 | anchors = {g.anchor for g in data.GUARDS} |
| 181 | assert all(r.anchor in anchors for r in data.MAP_ROWS) |
| 182 | assert [r.guard for r in data.MAP_ROWS if r.status == "helper-only"] == ["SafeET.parse", "finite_float"] |
| 183 | assert html.count('class="tbmap__row tbmap__row--') == len(data.MAP_ROWS) |
| 184 | assert html.count("<strong>not applied</strong>") == 2 |
| 185 | |
| 186 | def test_the_figure_is_a_labelled_docs_figure(self, html: str) -> None: |
| 187 | assert 'id="trust-boundary-map"' in html and 'aria-labelledby="trust-boundary-map-title"' in html |
| 188 | |
| 189 | def test_section_label_and_pager_come_from_the_registry(self, html: str) -> None: |
| 190 | nums = {slug: num for slug, num, _t, _d in _PHASES} |
| 191 | titles = {slug: t for slug, _n, t, _d in _PHASES} |
| 192 | assert f'<span class="devdocs-phase-num">PHASE {nums["security"]}</span>' in html |
| 193 | assert f"{nums['crdt']} — {titles['crdt']}" in html |
| 194 | |
| 195 | |
| 196 | # ── DOC_SEC_06: guards behave as the cards say ────────────────────────────────────────────────────────────── |
| 197 | |
| 198 | |
| 199 | def _accepts(fn: Any, value: str) -> bool: |
| 200 | try: |
| 201 | fn(value) |
| 202 | except (ValueError, TypeError): |
| 203 | return False |
| 204 | return True |
| 205 | |
| 206 | |
| 207 | class TestGuardBehaviour: |
| 208 | @pytest.mark.parametrize( |
| 209 | "name", |
| 210 | ["a..b", "x.lock", "feat/x.lock", "@", "a@{1}", "a b", "a\tb", "a\x1b[31mb", "/a", "a/", "a//b", ".a", "a.", "a~1", "a^", "a:b", "a?", "a*", "a[", "a\\b", "", "x" * 256, "..", "feat/./x", "a\x00b", "a\nb", "a\x7fb"], |
| 211 | ) |
| 212 | def test_branch_names_the_card_says_are_rejected(self, name: str) -> None: |
| 213 | assert not _accepts(val.validate_branch_name, name) |
| 214 | |
| 215 | @pytest.mark.parametrize("name", ["main", "feat/x", "feat/a.b", "a-b_c.d", "x" * 255, "a{b}", "feat/.hidden"]) |
| 216 | def test_branch_names_the_card_says_are_accepted(self, name: str) -> None: |
| 217 | assert _accepts(val.validate_branch_name, name) |
| 218 | |
| 219 | @pytest.mark.parametrize("name", ["ok\u009b31mx", "a\x85b", "rloevil"]) |
| 220 | def test_the_limits_line_is_true_c1_and_bidi_override_pass_validation(self, name: str) -> None: |
| 221 | assert _accepts(val.validate_branch_name, name) |
| 222 | |
| 223 | def test_object_ids_are_exactly_sha256_prefixed_lowercase_hex(self) -> None: |
| 224 | ok = "sha256:" + "a" * 64 |
| 225 | assert _accepts(val.validate_object_id, ok) and _accepts(val.validate_ref_id, ok) |
| 226 | for bad in ("a" * 64, "sha256:" + "A" * 64, "sha256:" + "a" * 63, "sha256:" + "a" * 65, "../../etc/passwd", "sha256:../../" + "a" * 51, ok + "\r\n", ok + " ", " " + ok, "SHA256:" + "a" * 64, ""): |
| 227 | assert not _accepts(val.validate_object_id, bad), bad |
| 228 | |
| 229 | def test_sanitize_display_strips_c0_del_c1_keeps_tab_newline_and_leaves_bidi(self) -> None: |
| 230 | out = val.sanitize_display("a\x1b]0;t\x07b\x9b0mc\td\ne\x7f\x00fg") |
| 231 | assert out == "a]0;tb0mc\td\nefg" |
| 232 | |
| 233 | def test_sanitize_provenance_also_strips_tab_and_newline(self) -> None: |
| 234 | assert val.sanitize_provenance("a\tb\nc\x1bd\x9be") == "abcde" |
| 235 | |
| 236 | def test_glob_prefix_strips_the_six_metacharacters_and_only_two_characters_pick_the_shard(self) -> None: |
| 237 | assert val.sanitize_glob_prefix("a*b?c[d]e{f}g") == "abcdefg" |
| 238 | assert val.sanitize_glob_prefix("**/*") == "/" |
| 239 | src = (_PKG / "core" / "commits.py").read_text(encoding="utf-8") |
| 240 | assert 'glob(f"sha256/{safe_prefix[:2]}/*")' in src |
| 241 | |
| 242 | def test_contain_path_rejects_traversal_absolute_empty_and_symlink_escape(self, tmp_path: Path) -> None: |
| 243 | base, outside = tmp_path / "b", tmp_path / "o" |
| 244 | (base / "ok").mkdir(parents=True) |
| 245 | outside.mkdir() |
| 246 | os.symlink(outside, base / "evil") |
| 247 | assert val.contain_path(base, "ok/a.txt") == (base / "ok" / "a.txt").resolve() |
| 248 | for bad in ("../x", "ok/../../x", "/etc/passwd", "evil/f", ""): |
| 249 | with pytest.raises(ValueError): |
| 250 | val.contain_path(base, bad) |
| 251 | |
| 252 | def test_numeric_guards(self) -> None: |
| 253 | with pytest.raises(ValueError): |
| 254 | val.clamp_int(5, 1, 3, "n") |
| 255 | assert val.clamp_int(2, 1, 3, "n") == 2 |
| 256 | assert val.finite_float(float("nan"), 0.0) == 0.0 and val.finite_float(float("inf"), 1.0) == 1.0 and val.finite_float(1.5, 0.0) == 1.5 |
| 257 | |
| 258 | def test_snapshot_ids_use_a_nul_separator_and_the_walker_skips_what_the_card_says(self, tmp_path: Path) -> None: |
| 259 | oid = "sha256:" + "a" * 64 |
| 260 | assert b"\x00" in snap.snapshot_identity_bytes({"a": oid, "b": oid}) |
| 261 | assert snap.compute_snapshot_id({"a|b": "sha256:" + "1" * 64}) != snap.compute_snapshot_id({"a": "sha256:" + "1" * 64}) |
| 262 | w, outside = tmp_path / "w", tmp_path / "o" |
| 263 | w.mkdir() |
| 264 | outside.mkdir() |
| 265 | for name in ("a.txt", ".hidden", ".cursorrules"): |
| 266 | (w / name).write_text("x") |
| 267 | (w / "d").mkdir() |
| 268 | (w / "d" / ".dotfile").write_text("x") |
| 269 | os.symlink(outside, w / "linkdir") |
| 270 | os.symlink(w / "a.txt", w / "linkfile") |
| 271 | os.mkfifo(w / "fifo") |
| 272 | (w / ".muse").mkdir() |
| 273 | (w / ".muse" / "x").write_text("x") |
| 274 | (w / "node_modules").mkdir() |
| 275 | (w / "node_modules" / "m.js").write_text("x") |
| 276 | assert sorted(snap.walk_workdir(w)) == [".cursorrules", ".hidden", "a.txt", "d/.dotfile"] |
| 277 | |
| 278 | def test_safeet_blocks_entity_expansion_and_external_entities_but_parses_plain_xml(self, tmp_path: Path) -> None: |
| 279 | p = tmp_path / "x.xml" |
| 280 | p.write_text('<?xml version="1.0"?><!DOCTYPE r [<!ENTITY a "aaaa"><!ENTITY b "&a;&a;&a;&a;">]><r>&b;</r>') |
| 281 | with pytest.raises(ValueError) as bomb: |
| 282 | SafeET.parse(p) |
| 283 | assert type(bomb.value).__name__ == "EntitiesForbidden" and not isinstance(bomb.value, SafeET.ParseError) |
| 284 | p.write_text('<?xml version="1.0"?><!DOCTYPE r [<!ENTITY x SYSTEM "file:///etc/hostname">]><r>&x;</r>') |
| 285 | with pytest.raises(ValueError, match="EntitiesForbidden"): |
| 286 | SafeET.parse(p) |
| 287 | p.write_text("<r><a>1</a></r>") |
| 288 | assert SafeET.parse(p).getroot().tag == "r" |
| 289 | |
| 290 | def test_identity_store_modes_symlink_refusal_and_url_normalisation(self, tmp_path: Path) -> None: |
| 291 | target = tmp_path / ".muse" / "identity.toml" |
| 292 | _save_all({}, target) |
| 293 | assert oct(target.parent.stat().st_mode & 0o777) == "0o700" and oct(target.stat().st_mode & 0o777) == "0o600" |
| 294 | target.unlink() |
| 295 | victim = tmp_path / "victim.txt" |
| 296 | victim.write_text("keep") |
| 297 | os.symlink(victim, target) |
| 298 | with pytest.raises(OSError): |
| 299 | _save_all({}, target) |
| 300 | assert victim.read_text() == "keep" |
| 301 | assert {hostname_from_url(u) for u in ("https://admin:secret@host/", "host", "HTTPS://Host/")} == {"host"} |
| 302 | assert hostname_from_url("https://host:8443/x") == "host:8443" |
| 303 | src = (_PKG / "core" / "identity.py").read_text(encoding="utf-8") |
| 304 | assert "_identity_write_lock" in src and "fcntl.flock" in src and "os.fchmod" in src and "os.replace" in src |
| 305 | assert "MSign ***" not in src |
| 306 | |
| 307 | def test_no_token_masking_exists_anywhere(self) -> None: |
| 308 | assert not any("MSign ***" in p.read_text(encoding="utf-8") for p in _PKG.rglob("*.py")) |
| 309 | |
| 310 | def test_there_is_no_local_file_transport_and_file_remotes_are_rejected(self, tmp_path: Path) -> None: |
| 311 | assert not hasattr(tr, "LocalFileTransport") and isinstance(tr.make_transport("file:///tmp/x"), tr.HttpTransport) |
| 312 | env = {k: v for k, v in os.environ.items() if k != "MUSE_ENABLE_MIDI"} |
| 313 | env.update(PYTHONPATH=os.pathsep.join(p for p in sys.path if p), HOME=str(tmp_path), XDG_CONFIG_HOME=str(tmp_path / ".config")) |
| 314 | work = tmp_path / "repo" |
| 315 | work.mkdir() |
| 316 | code = "import sys; from muse.cli.app import main; sys.argv = ['muse'] + sys.argv[1:]; sys.exit(main())" |
| 317 | run = lambda *a: subprocess.run([sys.executable, "-P", "-c", code, *a], cwd=work, env=env, capture_output=True, text=True, timeout=60) # noqa: E731 |
| 318 | assert run("init").returncode == 0 |
| 319 | proc = run("remote", "add", "f", "file:///tmp/nonexistent") |
| 320 | assert proc.returncode != 0 and "not allowed" in (proc.stdout + proc.stderr) |
| 321 | |
| 322 | def test_sysex_cap_is_defined_once_read_nowhere_and_documented_as_not_applied(self) -> None: |
| 323 | used = [p.name for p in _PKG.rglob("*.py") if p.name != "validation.py" and "MAX_SYSEX_BYTES" in p.read_text(encoding="utf-8")] |
| 324 | assert used == [] |
| 325 | cap = next(c for c in data.CAPS if c.constant == "MAX_SYSEX_BYTES") |
| 326 | assert cap.works is False and cap.where.startswith("nowhere") |
| 327 | |
| 328 | def test_size_cap_constants_equal_the_table(self, html: str) -> None: |
| 329 | assert val.MAX_FILE_BYTES == val.MAX_OBJECT_WRITE_BYTES == 256 * 1024 * 1024 and val.MAX_RESPONSE_BYTES == 64 * 1024 * 1024 |
| 330 | assert val.MAX_SYSEX_BYTES == 65_536 and val.MAX_AST_BYTES == 2 * 1024 * 1024 and val.MAX_PACK_OBJECTS == 100_000 |
| 331 | values = {c.constant: c.value for c in data.CAPS} |
| 332 | assert values == {"MAX_FILE_BYTES": "256 MiB", "MAX_OBJECT_WRITE_BYTES": "256 MiB", "MAX_RESPONSE_BYTES": "64 MiB", "MAX_PACK_OBJECTS": "100,000 items", "MAX_AST_BYTES": "2 MiB", "MAX_SYSEX_BYTES": "64 KiB"} |
| 333 | assert [c.constant for c in data.CAPS if not c.works] == ["MAX_SYSEX_BYTES"] |
| 334 | assert html.count('data-works="no"') == 1 |
| 335 | |
| 336 | |
| 337 | # ── DOC_SEC_06: the HttpTransport claims, on loopback mock servers (no network, no credentials, a throwaway key) ── |
| 338 | |
| 339 | |
| 340 | class _Handler(http.server.BaseHTTPRequestHandler): |
| 341 | hits: list[tuple[int, str]] = [] |
| 342 | target = 0 |
| 343 | |
| 344 | def log_message(self, *_a: Any) -> None: |
| 345 | pass |
| 346 | |
| 347 | def do_GET(self) -> None: # noqa: N802 |
| 348 | _Handler.hits.append((self.server.server_address[1], self.path)) |
| 349 | if self.path.startswith("/redir"): |
| 350 | self.send_response(302) |
| 351 | self.send_header("Location", f"http://127.0.0.1:{_Handler.target}/landed") |
| 352 | self.end_headers() |
| 353 | elif self.path.startswith("/big"): |
| 354 | self.send_response(200) |
| 355 | self.send_header("Content-Length", str(65 * 1024 * 1024)) |
| 356 | self.end_headers() |
| 357 | try: |
| 358 | for _ in range(65): |
| 359 | self.wfile.write(b"\0" * (1 << 20)) |
| 360 | except OSError: |
| 361 | pass |
| 362 | else: |
| 363 | self.send_response(200) |
| 364 | self.end_headers() |
| 365 | self.wfile.write(b"{}") |
| 366 | |
| 367 | |
| 368 | @pytest.fixture(scope="module") |
| 369 | def servers() -> Any: |
| 370 | a = http.server.ThreadingHTTPServer(("127.0.0.1", 0), _Handler) |
| 371 | b = http.server.ThreadingHTTPServer(("127.0.0.1", 0), _Handler) |
| 372 | for s in (a, b): |
| 373 | threading.Thread(target=s.serve_forever, daemon=True).start() |
| 374 | _Handler.target = b.server_address[1] |
| 375 | yield a.server_address[1], b.server_address[1] |
| 376 | a.shutdown() |
| 377 | b.shutdown() |
| 378 | |
| 379 | |
| 380 | @pytest.fixture(scope="module") |
| 381 | def ident() -> tr.SigningIdentity: |
| 382 | from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey |
| 383 | |
| 384 | return tr.SigningIdentity("qa-probe", Ed25519PrivateKey.generate()) |
| 385 | |
| 386 | |
| 387 | class TestTransport: |
| 388 | def test_a_redirect_is_refused_and_the_target_is_never_contacted(self, servers: Any, ident: tr.SigningIdentity) -> None: |
| 389 | pa, pb = servers |
| 390 | _Handler.hits.clear() |
| 391 | t = tr.HttpTransport() |
| 392 | with pytest.raises(tr.TransportError): |
| 393 | t._execute(t._build_request("GET", f"http://127.0.0.1:{pa}/redir", ident)) # noqa: SLF001 |
| 394 | assert not [h for h in _Handler.hits if h[0] == pb] |
| 395 | |
| 396 | def test_an_api_response_over_64_mib_is_refused(self, servers: Any) -> None: |
| 397 | pa, _ = servers |
| 398 | t = tr.HttpTransport() |
| 399 | with pytest.raises(tr.TransportError, match="64 MiB cap"): |
| 400 | t._execute(t._build_request("GET", f"http://127.0.0.1:{pa}/big", None)) # noqa: SLF001 |
| 401 | |
| 402 | def test_https_is_required_only_for_signed_requests_to_non_loopback_hosts(self, servers: Any, ident: tr.SigningIdentity) -> None: |
| 403 | pa, _ = servers |
| 404 | t = tr.HttpTransport() |
| 405 | with pytest.raises(tr.TransportError, match="non-HTTPS"): |
| 406 | t._build_request("GET", "http://hub.example.invalid/x", ident) # noqa: SLF001 |
| 407 | assert t._build_request("GET", f"http://127.0.0.1:{pa}/x", ident).url # noqa: SLF001 - loopback http is allowed |
| 408 | assert t._build_request("GET", "http://hub.example.invalid/x", None).url # noqa: SLF001 - unsigned requests are not checked |
| 409 | |
| 410 | def test_a_presigned_pack_download_is_not_subject_to_the_64_mib_cap(self, servers: Any) -> None: |
| 411 | pa, _ = servers |
| 412 | assert len(tr._urllib_do("GET", f"http://127.0.0.1:{pa}/big", {}, follow_redirects=True, max_bytes=None)) == 65 * 1024 * 1024 # noqa: SLF001 |
| 413 | |
| 414 | def test_there_is_no_content_type_guard(self) -> None: |
| 415 | src = (_PKG / "core" / "transport.py").read_text(encoding="utf-8") |
| 416 | assert "content-type guard" not in src.lower() and "first byte" not in src.lower() |
| 417 | |
| 418 | |
| 419 | # Known defects, pinned: each flips when its ticket is fixed (strict), and the warnings on the page must go with it. |
| 420 | def _xfail(what: str) -> Any: |
| 421 | return pytest.mark.xfail(strict=True, raises=AssertionError, reason=f"gabriel/muse#{_C_HELPERS}: {what}") |
| 422 | |
| 423 | |
| 424 | def test_sysex_payload_is_serialised_in_full_or_as_its_digest() -> None: |
| 425 | """Since muse#212 the MIDI merge serialises every field: a short SysEx payload in full, a long one as its SHA-256.""" |
| 426 | import hashlib |
| 427 | |
| 428 | import mido |
| 429 | from muse.plugins.midi import midi_merge as mm |
| 430 | |
| 431 | assert mm._msg_to_dict(mido.Message("sysex", data=[1, 2, 3]))["data"] == [1, 2, 3] # noqa: SLF001 |
| 432 | big = [1] * 70_000 |
| 433 | assert mm._msg_to_dict(mido.Message("sysex", data=big))["data"] == "sha256:" + hashlib.sha256(bytes(big)).hexdigest() # noqa: SLF001 |
| 434 | |
| 435 | |
| 436 | def test_sysex_payload_is_part_of_the_event_hash() -> None: |
| 437 | import mido |
| 438 | from muse.plugins.midi import midi_merge as mm |
| 439 | |
| 440 | a, b = mido.Message("sysex", data=[1, 2, 3]), mido.Message("sysex", data=[9, 9, 9]) |
| 441 | assert mm._hash_events([(0, a)]) != mm._hash_events([(0, b)]) # noqa: SLF001 |
| 442 | |
| 443 | |
| 444 | @_xfail("sanitize_provenance does not remove U+202E although its docstring says it prevents RTL-override spoofing") |
| 445 | def test_provenance_strips_the_bidirectional_override() -> None: |
| 446 | assert "" not in val.sanitize_provenance("ab") |
| 447 | |
| 448 | |
| 449 | @_xfail("validate_branch_name accepts C1 controls") |
| 450 | def test_branch_names_reject_c1_controls() -> None: |
| 451 | assert not _accepts(val.validate_branch_name, "ok\u009b31mx") |
| 452 | |
| 453 | |
| 454 | @_xfail("validate_object_id and its siblings accept one trailing newline ($ matches before a final LF)") |
| 455 | def test_object_id_validation_rejects_a_trailing_newline() -> None: |
| 456 | assert not _accepts(val.validate_object_id, "sha256:" + "a" * 64 + "\n") |
| 457 | |
| 458 | |
| 459 | @_xfail("finite_float has no call sites: no shipped command applies it") |
| 460 | def test_finite_float_has_a_call_site(scan: dict[str, list[tuple[str, str]]]) -> None: |
| 461 | assert scan["finite_float"] |
| 462 | |
| 463 | |
| 464 | # ── DOC_SEC_07: wording the audit corrected ───────────────────────────────────────────────────────────────── |
| 465 | |
| 466 | |
| 467 | class TestWording: |
| 468 | def test_the_old_claims_are_gone(self, html: str) -> None: |
| 469 | for stale in ( |
| 470 | "exactly 64 lowercase hex", |
| 471 | "hex-only", |
| 472 | "typer.echo", |
| 473 | "midi_parser", |
| 474 | "content-type guard", |
| 475 | "Seven layered controls", |
| 476 | "token masking in logs", |
| 477 | "skips symlinks and", |
| 478 | "rejected before the bytes are buffered", |
| 479 | "all fourteen guards", |
| 480 | "every site where it is applied", |
| 481 | "defusedxml.ElementTree.parse()</code> behind a typed", |
| 482 | "MusicXML import", |
| 483 | ): |
| 484 | assert stale not in html, stale |
| 485 | |
| 486 | def test_removed_guards_are_said_to_be_removed_and_ticketed_defects_link_their_ticket(self, html: str, text: str) -> None: |
| 487 | assert "Removed from this page:" in text and "Local file transport" in text and "MSign ***" in text |
| 488 | assert html.count(f'{MUSE_ISSUES}{_C_HELPERS}"') >= 5 |
| 489 | assert "No call sites" in text and "Not applied in rc20" in text |
| 490 | |
| 491 | def test_the_default_exclusion_wording_is_the_owner_approved_conservative_one(self, text: str) -> None: |
| 492 | assert "These exclusions are not a substitute for reviewing the contents of a commit" in text |
| 493 | assert "Dotfiles and dot-directories are tracked unless excluded" in text |
| 494 | |
| 495 | def test_scope_names_the_server_pages_instead_of_claiming_server_guarantees(self, html: str, text: str) -> None: |
| 496 | assert "What it does not cover" in text and "MuseHub server" in text |
| 497 | for slug in ("identity", "wire", "api"): |
| 498 | assert f'href="/muse/{slug}"' in html |
| 499 | assert "Request code outside HttpTransport is not described by this card" in text |
| 500 | assert "they should not be assumed to cover every Muse CLI request" in text |
| 501 | |
| 502 | def test_the_http_card_scopes_every_control_to_httptransport(self, text: str) -> None: |
| 503 | assert "Controls of the HttpTransport class" in text |
| 504 | assert "where it installs _NoRedirectHandler, a 3xx response is refused" in text |
| 505 | assert "an unsigned request is not subject to this check" in text |
| 506 | assert "a longer body raises TransportError" in text |
| 507 | assert "make_transport()" in text |
File History
4 commits
sha256:e1629e5f328982dbf6e535d3f253993c78ff225dfee4ebb0e0a15fc1db69046b
merge: rc25 — docs and docs-contract tests match Muse batch…
Opus 5.5
minor
⚠
2 days ago
sha256:7d5e578cc640e757aa78f7d17acb4f6e33322c5414f4278f01ef000ef01a5ff7
docs(security): guard call sites and SysEx serialisation af…
Opus 5.5
patch
2 days ago
sha256:099dace872ac6456f79fb8b27e07b483dc140dee63a34dac9f54f8be53ff7a4c
docs(security): #260 scope the HTTP redirect-refusal statem…
Opus 5.5
patch
7 days ago
sha256:7513f245748d6667481e0ecef54e261ad11d4a664ba7128527060b240f4707bc
docs(security): #260 Security Reference QA contract v2 — co…
Sonnet 5.5
minor
⚠
7 days ago