Commit Graph
3 Commits
Author SHA1 Message Date
pastilhasandClaude Opus 5 ab2336516f dev-proxy: a stylesheet's url() is a URL
Two bugs, one line apart, both silent.

The `url(/…)` rule only matched an UNQUOTED target, so `url("/fonts/x.woff2")` — the form every
generator emits — went through untouched. And it only ran on HTML, so a linked stylesheet got the
narrow `/_bun/` rule and nothing else.

Together that meant a site's own @font-face rules kept pointing at `/fonts/…`, which inside the frame
is the platform's root. Every face 404'd and the preview rendered in a system fallback: nothing looked
broken, it just looked like a different site than the one being previewed.

JavaScript keeps the narrow rule — an absolute path in JS may not be a URL. In CSS it always is.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-31 06:42:14 +01:00
pastilhasandClaude Opus 5 fccabfcea3 the preview authorises itself before it mounts the frame
401 on every asset, white pane. The server was never wrong — replaying the exact
browser sequence with a minted owner token gave HTML 200, `set-cookie` present, and
the sub-resource authorised. So the fault was in how the frame got its credential,
and both mechanisms it relied on were guesses.

Putting the token on the iframe URL guesses which key the browser stored it under.
Letting the frame mount and hoping guesses that a cookie from some earlier load is
still there. When either guess misses, the HTML renders, every sub-resource 401s,
and the pane is white — which is the shape that makes this hard to read, because
the document loading looks like success.

So neither is used now. The panel asks `/api/dev-proxy/session` with the ordinary
API client — which already knows how to authenticate, because it sends the header on
every other call it makes — and mounts the frame only once that returns. The iframe
URL carries nothing. `iframeSrc` is deleted rather than left as a fallback: a second
path that runs when the first fails is how you get a bug that only appears
sometimes.

`Authorization` rather than a query parameter, so the token never reaches an
address, a log or a Referer. Re-run on every reload, so a preview left open
overnight recovers by itself instead of showing a wall of 401s.

── Also fixed: a lie attached to every binary asset ──

The non-rewritable branch returned the upstream's `content-encoding` and
`content-length` unchanged, but `fetch` had already decompressed the body — so the
browser was told to inflate plain bytes, with a length that no longer matched. The
rewrite branch had always deleted both. Images, fonts and wasm were affected;
nothing had hit it yet because the test app serves none.

Verified: `/session` 200 and sets the cookie with a token, 401 without; the frame
200 with the cookie, 401 with nothing.

`[open]` A request that escapes the prefix hits the SPA catch-all and returns
`200 text/html` instead of 404, so a `<script>` gets HTML, the MIME check refuses it
silently, and nothing appears as an error. That masks this entire class of bug and
is worth closing separately.

tsgo clean, frontend builds, 890 pass / 7 fail unchanged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-16 13:33:22 +00:00
pastilhasandClaude Opus 5 7d64b0a0d6 paste localhost:3000 into the preview and see the server's port
No proxy host, no certificate, no port opened. `/api/dev-proxy/5173/…` forwards to
`http://127.0.0.1:5173/…` over the platform's own origin, and the panel maps what
you type onto it.

── Why a proxy at all, when the ask was "just point at localhost" ──

Neither direct route works from a browser on https://<platform>.
`http://localhost:5173` resolves on the VIEWER's machine, not the server's, and
`http://<server>:5173` is mixed content inside an https page and is blocked before
it is even attempted. Both fail looking exactly like the dev server being down. The
bytes have to arrive over an origin the page already trusts, which is the platform.

A dev server can therefore stay bound to 127.0.0.1. The workflow this replaces —
one NPM proxy host per app — needs it on 0.0.0.0 plus a firewall rule for the
docker bridge, so this is both less setup and less exposure.

── What came back from `188b45c1^`, and what did not ──

The 46-line HTML/asset rewrite, which is the hard-won part: absolute paths in the
document, plus an injected script that intercepts fetch/XHR/WebSocket for the URLs
built at runtime. Its JWT injection is deleted — those apps called the PLATFORM's
API; a dev server proxied here talks to itself, and handing its process the owner's
bearer token would be giving credentials to whatever is on that port.

The other 314 lines are not back: spawning, port discovery, idle timers, log
collection, a proxyId registry. The port is the id, and the background-job button
already starts dev servers.

── HMR is bridged, because "see it" without "follow it" is half the feature ──

The injected script rewrites the dev server's socket under the prefix; the relay
dials `ws://127.0.0.1:<port>` and forwards frames both ways. Same shape as the vault
relay including the non-obvious part — state registered synchronously and frames
queued until the upstream opens, because Bun requires the upgrade to be synchronous
and an HMR client speaks immediately. An upstream close closes the client, which is
what makes a dev-server restart reconnect by itself.

── Access, which was not in the ask and is not optional ──

This route reaches any loopback port on the machine, on a host that answers from the
public internet. An iframe cannot carry a bearer token — it is a navigation — so the
panel puts it in the URL once, the proxy verifies it is the OWNER through
`resolveAuthToken`, and issues a short-lived cookie every sub-resource then carries.
Anonymous and non-owner requests get 401, verified. Owner-only rather than a
permission: reading an arbitrary loopback port is the `execution` power the registry
says can never be granted.

Both exemptions are registered with reasons, and the boot check earned its keep —
it refused to start until the socket provider was claimed too.

── Verified against a real loopback-only server ──

Confirmed unreachable from outside, then through the proxy: index served and
rewritten, `/_bun/` chunk resolved, JSON passed through untouched, 401 anonymous,
401 with a bad token, "nothing is listening" on a dead port. Websocket: refused
without the cookie, target parsed, frames relayed in both directions.

One bug found by testing rather than reading: the upstream path was derived by
slicing a hardcoded prefix, so every asset request returned the index page. It is
found from the `/<port>` segment now.

A test asserting `http://localhost:5173` is kept verbatim is now inverted, with the
reason written beside it — that was correct when the panel only framed real
hostnames.

tsgo clean, frontend builds, 890 pass / 7 fail unchanged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-16 12:59:20 +00:00