Files
zeekay 19fdf55ceb feat(auth): one key scheme, minted and read in one place
A key now opens with what it is allowed to touch. `pk-` is publishable and
safe in a browser; `sk-` is secret and never leaves a server. The five marks
it replaces all began `ph` -- the upstream's initials -- and they were the
last of its brand a customer read back to us, in every SDK snippet. No word
rule could reach them, because `ph` is not a word.

The marks were also not telling the truth. The constant said `phc_` while
every live project token said `hi_`, so the middleware that routes on the
constant matched no real token at all. Two spellings existed and one was
fiction. Minting now happens in exactly one function and reading in exactly
one, so a second spelling has nowhere to live.

`phx_` (personal) and `phs_` (project secret) both become `sk-`. The mark
now says only that a value is secret; which store holds it is settled by
asking. That was already how personal keys worked -- keys minted before the
marks carry none, so the store always decided -- and both secret resolvers
already returned None on a miss and fell through, so one shared extractor
replaces the two that could drift apart.

OAuth's access and refresh tokens stay distinguishable as `at-` and `rt-`,
because the parser tells them apart before either is looked up, and the
domain-verification token becomes `vt-` rather than keeping a sixth mint of
its own.

`bin/debrand --check` now refuses the marks as a shape, so a seventh cannot
be coined. Two rules that were about to rename things that are not ours to
spell are bounded: `Lemon` no longer eats `LemonSqueezy`, and the `hog_flow`
rules no longer reach a Postgres table, a Datastore table or a route.

Keys are stored hashed, so a mark cannot be rewritten in place: the three
live project tokens have to be reminted.
2026-08-08 19:42:44 -07:00

84 lines
2.5 KiB
Python

"""End-to-end integration test: spin up a local fixture page with a stub
Insights snippet, run check-loading against it, and assert the snippet was
detected. Skipped automatically when Playwright isn't installed.
"""
from __future__ import annotations
import asyncio
import threading
import http.server
import socketserver
import pytest
playwright = pytest.importorskip(
"playwright.async_api",
reason="Playwright not installed; run `playwright install chromium` first.",
)
import cli # noqa: E402
FIXTURE_HTML = """<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>traffic-sim fixture</title>
<script id="insights-init">
// Stub insights object so the detector reports loaded=true without
// hitting the network during tests.
window.insights = {
__loaded: true,
config: {token: 'pk-test_fixture', api_host: 'https://example.com'},
get_distinct_id: function() { return 'test-distinct-id'; },
};
</script>
</head>
<body>
<h1>Hello from the fixture</h1>
</body>
</html>
"""
@pytest.fixture
def fixture_server(tmp_path):
"""Serve FIXTURE_HTML on a localhost port. Yields the URL."""
fixture_dir = tmp_path / "site"
fixture_dir.mkdir()
(fixture_dir / "index.html").write_text(FIXTURE_HTML)
handler = lambda *a, **kw: http.server.SimpleHTTPRequestHandler(*a, directory=str(fixture_dir), **kw)
httpd = socketserver.TCPServer(("127.0.0.1", 0), handler)
port = httpd.server_address[1]
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
thread.start()
try:
yield f"http://127.0.0.1:{port}/"
finally:
httpd.shutdown()
httpd.server_close()
@pytest.mark.integration
def test_check_loading_detects_stubbed_insights(fixture_server):
"""check-loading against a fixture page reports insights as loaded."""
pages = asyncio.run(
cli.run_check_loading(
urls=[fixture_server],
headless=True,
verbose=False,
cloud=False,
timeout_ms=30_000,
run_id="itest",
insights_domains=cli.DEFAULT_INSIGHTS_DOMAINS,
)
)
assert len(pages) == 1
page_data = next(iter(pages.values()))
assert page_data.get("error") is None
assert page_data["runtime_state"]["loaded"] is True
# The stub uses #insights-init script tag, which we treat as head_snippet.
assert page_data["load_method"] == "head_snippet"
assert page_data["init_config"]["api_key"] == "pk-test_fixture"