Repository navigation
fix: render and capture file servers answer only their own loopback host - #7853
Conversation
…vers The CLI static project server and the producer and engine file servers bind 127.0.0.1, but answered any Host header. They now refuse a request whose Host is not 127.0.0.1, localhost or [::1] at their own port (or the in-process origin the CLI hands out when listen is refused), and their responses carry frame-ancestors 'self' and X-Frame-Options: SAMEORIGIN so another site cannot frame the served page.
Edit accuracy: accurate 267 (base branch 267), smooth 95 of thoseThe gate passes. Quarantined, measured but not gated (8)
Unstable (3)
|
jrusso1020
left a comment
There was a problem hiding this comment.
Approve. Reviewed at 2a7f35b2429b9f2af8fad11ccc937b2834aaed9c.
- Host rule: all three capture file servers now answer only
127.0.0.1,localhostor[::1]on the port they actually bound. - Framing: every response says no other origin may frame it, so a foreign parent never gets the runtime's editing port.
- In practice: every real caller still reaches the server by the exact host it was handed, and same-origin subframes keep working.
Nothing blocking.
Need: yes · Size: ok · Reuse: one loopbackOnly.ts helper shared by all three servers; studio-server's isLoopbackHost can't be imported from engine and has a different, any-port meaning for play/present · Simpler: already minimal · Structure: fine (checked studio-server request-identity and #7852's per-server copies; no split)
Risk: low. The only callers are local Chrome navigating to the URL the server returned, which passes the check by construction.
Overlap with #7852
#7852 and this PR fix the same two gaps on the same three servers, in different ways.
| #7852 | this PR | |
|---|---|---|
| Host | any loopback name, any port (studio-server isLoopbackHost in CLI/producer, a separate parser in engine, plus this machine's addresses on a wildcard bind) |
the three loopback names, own bound port only, one shared helper |
| Framing | a <meta> mark that tells the runtime not to offer the editing port; CLI also sends CSP: sandbox to cross-site loads |
frame-ancestors 'self' + X-Frame-Options: SAMEORIGIN, enforced by the browser |
Engine on 0.0.0.0 |
guarded; service-DNS callers get 403 | unguarded (see nit 1) |
- They conflict.
git merge-treeof both heads stops onpackages/cli/src/utils/staticProjectServer.tsandpackages/engine/src/services/fileServer.ts. Landing both would also leave two Host rules and two framing defences on each server. - I'd land this one and close #7852, or cut #7852 down to whatever it still adds. This one is stricter on Host (own port, no missing-Host fallback), uses one helper instead of two, and its framing defence doesn't depend on the runtime. It also avoids the service-DNS break on the exported engine server.
- Does this one miss anything #7852 covers? I don't think so. The editing agent only answers
window.parent(editingAgent.ts:143:if (parent === window || event.source !== parent) return;), and a top-level page is its own parent. So blocking foreign parents closes it, and #7852's mark adds nothing once that's in place.
Production path
- Producer:
createFileServerbinds127.0.0.1and returnshttp://localhost:<boundPort>.listeningPortis set from the sameboundPortbefore the promise resolves, including aftersettleOnBrowserSafePortre-settles. So the URL handed out always passes the own-port check. - Callers: probe, render, distributed chunk, frame-cost and reference-background stages each start their own server and navigate Chrome to its
url. - Public-only capture proxy:
browserProxyArgs(proxy.url, pageServerHost)bypasses exactlynew URL(pageServerUrl).host. Chrome connects directly, with that host asHost. - hyperframes-internal:
git grep -e createFileServer -e serveStaticProjectHtml -e fileServer.urlon internalmainreturns 0 hits. Its producer server builds on the OSS render handlers, so this reaches prod renders on the next producer bump, with the same localhost URL. - CLI in-process fallback:
answerInProcessnow setsHostto thehyperframes-N.localhostname it handed out.listeningPortstays-1, so that one name is all it accepts.
Probes and tests
Real Chrome (headless shell 155, my own puppeteer harness, removed afterwards):
| server | top-level load | same-origin /sub.html iframe |
framed by a page on another 127.0.0.1 port |
|---|---|---|---|
CLI serveStaticProjectHtml |
200, frame-ancestors 'self' |
loads (SUBPAGE) |
chrome-error://chromewebdata/ |
producer createFileServer |
200 on http://localhost:<port>/index.html |
loads (SUBPAGE) |
chrome-error://chromewebdata/ |
At head:
- engine
fileServer.host+fileServer: 8/8 (vitest). - producer
fileServer.host,file-race,listenError: 36/36 (vitest). ProducerfileServer.test.ts: 65/65 (bun test). - CLI
staticProjectServer+.host: 56 pass. - Two CLI browser tests gave no result here.
range.chromiumis env-gated and skipped.charset.browsertimed out starting Chrome while this box sat at load 21, before it reached the server. The Chrome probe above covers that path.
Reverts and mutants:
- All three server sources put back to the merge base: engine host 2/2 fail, producer host 4/8 fail, CLI host 4/7 fail.
answerInProcesswithout the forcedHost: the in-process test fails. Node then sendsHost: localhost, which has no port and is refused.- The Host check removed from
answerOwnLoopbackHostOnly, framing headers kept: producer host 3/8 fail.
Nits (non-blocking)
- An engine server bound to a non-loopback address also loses the framing headers.
answerOwnLoopbackHostOnlysets both, soboundToLoopbackOnly === falseskips framing as well as the Host rule.- Choosing to expose the server is a reason to skip the Host rule, not to let another site frame the page and take the editing port.
- Fix: register a second small middleware that only sets
SAME_ORIGIN_FRAMING_HEADERS, unconditionally, and keep the Host middleware behind the bind check. - No caller in this repo binds the engine server elsewhere today, so this only affects outside users of the export.
[::1]:<port>is accepted, but the producer and CLI servers only bind127.0.0.1. Harmless, since nothing can reach them on::1. Say so in the helper's comment, or take the bound address as a parameter.- Release note: an engine caller on
127.0.0.1that reached the server as127.0.0.2or by a custom loopback alias now gets 403.
— Rames
* chore: release v0.9.54 * chore: release v0.9.54 notes include heygen-com#7827, heygen-com#7825, heygen-com#7845, heygen-com#7848 and heygen-com#7849 * chore: release v0.9.54 notes include heygen-com#7853 * chore: release v0.9.54 notes include heygen-com#7851 and heygen-com#7855
What
The file servers that feed the capture browser now answer only their own loopback address, and they tell the browser that no other site may frame their pages.
serveStaticProjectHtmlin the CLI serves the project forsnapshot,check,validate,layout,compare,grade-compareandmotion-shot.createFileServerin the producer serves the project duringhyperframes render, Studio renders and Desktop exports.createFileServerin the engine is the smaller public version of the same server.Why
Each server binds
127.0.0.1, but none of them checked theHostheader. A website can point its own DNS name at127.0.0.1and then read these servers as if they were its own origin. That would let it fetch the project's page and files while a render or check is running. The served page also carries the HyperFrames runtime, which offers an editing channel to its parent frame. A page from another site could frame the server's page and use that channel.A browser cannot forge the
Hostheader, so checking it closes the first gap. Refusing cross-site framing closes the second.Related work
Refs #7792, which added the Host rule for play and present.
How
isOwnLoopbackHost, accepts only127.0.0.1:<port>,localhost:<port>and[::1]:<port>for the port the server actually bound.answerOwnLoopbackHostOnly. It refuses any other Host with a 403 before a route opens a file. It also addsContent-Security-Policy: frame-ancestors 'self'andX-Frame-Options: SAMEORIGINto every response.httphandler. When a sandbox refuseslisten(), the server answers in-process at ahyperframes-N.localhostorigin. That path now sets its own Host on each request, and the server accepts that one name too.http://localhost:<port>orhttp://127.0.0.1:<port>), including Docker, Lambda, Cloud Run and distributed chunk workers, where Chrome runs next to the server. The public-only capture proxy sends the page server's host directly, so its Host is unchanged. No caller reaches these servers by another name.The play, present and preview servers are not changed. They listen on every interface on purpose and keep their own wider Host rule.
Test plan
Unit tests added/updated
Manual testing performed
Documentation updated (if applicable)
Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug
New tests for each server: a foreign Host gets 403 for
/,/index.htmland an asset; the server's own port answers on127.0.0.1,localhostand[::1]; the HTML response carries both framing headers. These tests fail without the change.Existing CLI server tests pass, including the in-process path and the Chromium range, charset and finding-inspector tests.
Existing producer file server tests pass, including
scriptFailureAttribution.test.ts, which drives Chrome againstcreateFileServer.Ran
hyperframes render,snapshot,checkandvalidatefrom source on a sample project. All completed.tsc --noEmitpasses for engine, producer and cli.