Files
uconsole_asw_game/tests/test_commander_insecure_context.py
T
db9979andClaude Opus 5.5 4dd23bc242 Web client: CIC redesign with ES modules and bundled fonts
- Remote Crew browser client rebuilt as a dark combat-information-centre layout
  for large desktop monitors: slim status bar (stations, mission, clocks, UTC,
  link state), pulsing alert band, the instrument as centrepiece and
  collapsible docks (contacts, station, contact detail, log drawer; keys , . L).
  Four columns from 2400 px, fluid type up to 4K; guide, lookout and contact
  library open as overlays (Esc closes).
- app.js split into native ES modules under data/commander/js; the transport
  (net/*) no longer touches the DOM and talks to the views through synchronous
  topics (core/events.js, app/bindings.js); redraws share one frame scheduler.
- style.css replaced by a token-based design system in data/commander/css.
- Inter 4.1 and JetBrains Mono 2.304 bundled unmodified (OFL-1.1) with
  provenance in THIRD_PARTY_NOTICES.md; CSP allows font-src 'self'.
- Server serves the css/js/fonts trees from one asset manifest
  (src/commander/assets.py); packaging, capture tool, help, manual and docs
  updated; tests share tests/commander_web.py and import modules directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 12:02:01 +02:00

65 lines
2.7 KiB
Python

"""The LAN host serves plain HTTP, where crypto.randomUUID does not exist.
Every request-ID generator in the browser clients must fall back to
crypto.getRandomValues instead of throwing ("crypto.randomUUID is not a
function" at pairing/login).
"""
import html
import json
import re
import shutil
import subprocess
from pathlib import Path
import pytest
from commander_web import client_js
ASSETS = Path("data/commander")
def _function_source(text: str, name: str) -> str:
start = re.search(rf"^\s*function {name}\(\) {{", text, re.M).start()
depth = 0
for index in range(text.index("{", start), len(text)):
depth += {"{": 1, "}": -1}.get(text[index], 0)
if depth == 0:
return text[start:index + 1]
raise AssertionError(name)
def test_request_ids_never_call_random_uuid_unguarded():
for path in sorted(ASSETS.rglob("*.js")):
text = path.read_text(encoding="utf-8")
for match in re.finditer(r"crypto\.randomUUID\(\)", text):
line = text[text.rfind("\n", 0, match.start()) + 1:text.find("\n", match.end())]
assert 'typeof crypto.randomUUID === "function"' in line, (path, line)
def test_request_id_fallback_works_without_random_uuid(tmp_path):
chromium = shutil.which("chromium") or shutil.which("chromium-browser")
if chromium is None:
pytest.skip("Chromium is not installed")
admin = _function_source((ASSETS / "admin.js").read_text(encoding="utf-8"), "requestId")
app = _function_source(client_js(), "secureId")
script = "\n".join([
# Emulate a non-secure context: the method is absent.
"Object.defineProperty(Crypto.prototype, 'randomUUID', {value: undefined});",
admin, app,
"const pattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;",
"const ids = [requestId(), requestId(), secureId(), secureId()];",
"document.documentElement.dataset.result = JSON.stringify({",
" valid: ids.every((id) => pattern.test(id)), unique: new Set(ids).size});",
])
page = tmp_path / "ids.html"
page.write_text(f"<!doctype html><html><body><script>{script}</script></body></html>",
encoding="utf-8")
result = subprocess.run(
[chromium, "--headless", "--no-sandbox", "--disable-gpu", "--no-first-run",
f"--user-data-dir={tmp_path / 'profile'}", "--virtual-time-budget=5000",
"--dump-dom", page.as_uri()], capture_output=True, text=True, timeout=60)
match = re.search(r'data-result="([^"]*)"', result.stdout)
assert match, result.stdout[-2000:] + result.stderr[-2000:]
assert json.loads(html.unescape(match.group(1))) == {"valid": True, "unique": 4}