Skip to main content
← Back to list
01Issue
BugShippedExtensionsPublic
Assigneesskunk-ape

Relationships

#3226 studio serve refuses /assets/app.js through a reverse proxy (Origin must equal http://Host)

Opened by shelson · 10/10/2026· Shipped 10/12/2026

Summary

The studio serve method's HTTP handler returns 403 for GET /assets/app.js when the studio page is loaded through a reverse proxy (e.g. Traefik) that forwards to 127.0.0.1:<port>. index.html and /assets/studio.css load fine; only the module script fails, so the page renders blank with an asset that looks "missing" in the network tab.

Why it happens

index.html loads the app as <script type="module" src="/assets/app.js">. Browsers fetch module scripts in CORS mode, so that request carries an Origin header. The top-level navigation and the CSS <link> do not, which is why only app.js is affected.

The pre-routing refusal gate in models/_lib/engine/studio_server.ts requires two things at once:

  • Host is one of 127.0.0.1:<port> / localhost:<port>
  • Origin (when present) is exactly http://<Host>

Behind a reverse proxy these are mutually unsatisfiable:

  • If the proxy forwards the public Host, the Host check returns 403 "the Host header must name this studio server".
  • If the proxy rewrites Host to loopback so the Host check passes, the Origin check returns 403 "requests from another origin are refused", because the browser's Origin names the public site (https://studio.example.com).

So there is no proxy configuration that makes the module script load, even though the page and its stylesheet do.

Steps to reproduce

Start the studio on a fixed port:

swamp model method run studio serve --input port=41251

Simulate the browser's module fetch arriving through a proxy that rewrites Host to loopback and records the public host in X-Forwarded-Host:

curl -i -H "Host: 127.0.0.1:41251" \
  -H "Origin: https://studio.example.com" \
  -H "X-Forwarded-Host: studio.example.com" \
  -H "X-Forwarded-Proto: https" \
  -H "Sec-Fetch-Site: same-origin" \
  -H "Sec-Fetch-Mode: cors" \
  -H "Sec-Fetch-Dest: script" \
  http://127.0.0.1:41251/assets/app.js

Actual: 403 {"error":"requests from another origin are refused"}

Expected: 200 with the script body (a same-origin subresource of the proxied page).

Controls that currently pass: curl http://127.0.0.1:41251/assets/app.js -> 200; proxied navigation to / -> 200; proxied /assets/studio.css -> 200.

Impact

The studio is unusable behind any reverse proxy / ingress, which is the normal way to reach it from another machine. The Host check can only pass with a loopback Host, and once it does, a CORS-mode subresource can never present a loopback Origin.

Proposed fix (high level)

Make the Origin check proxy-aware without weakening the anti-DNS-rebinding Host check:

  • Accept an Origin whose host matches the client host a proxy records in X-Forwarded-Host (set by Traefik and most proxies), in addition to the existing http://<Host> match.
  • Gate that allowance on browser-set Fetch Metadata (e.g. only when Sec-Fetch-Site is same-origin or none) so a cross-site page cannot use it. The loopback Host requirement must stay strict; accepting a forwarded public Host would reintroduce DNS-rebinding, since a hostile domain that resolves to loopback presents identical Host and Origin.
  • Alternatively (or additionally), add a configurable public origin (a publicOrigin method argument / env var) and accept it explicitly. This is the more robust option for proxies that do not set X-Forwarded-Host, and it would also allow supporting passHostHeader: true safely.

A local patch implementing the X-Forwarded-Host + Fetch Metadata approach is working here and can be shared if useful.

Upstream repository: https://github.com/swamp-club/swamp-extensions

Environment

  • Extension: @swamp/stagecraft@2026.10.08.1
  • swamp: 20261009.215038.0-sha.986f9022
  • OS: linux (x86_64)
  • Deno: 2.9.7
  • Shell: /bin/zsh
02Bog Flow
✓OPEN✓TRIAGED✓IN PROGRESS✓SHIPPED+ 1 MOREASSIGNED+ 7 MOREREVIEW+ 11 MOREPR_MERGED+ 2 MORESESSION_SUMMARIZED

Shipped

10/12/2026, 1:54:53 AM

Click a lifecycle step above to view its details.

03Sludge Pulse
skunk-ape assigned skunk-ape10/12/2026, 12:36:36 AM
Editable. Press Enter to edit.

system commented 10/10/2026, 7:22:51 AM

Classified automatically when this issue was filed.

  • Source: Extensions

If you feel this classification is incorrect, add a ripple to tell us so.

shelson commented 10/10/2026, 7:25:51 AM

Patch against swamp-club/swamp-extensions@58df267 (@swamp/stagecraft 2026.10.08.1). Implements the X-Forwarded-Host + Fetch Metadata approach from the report, keeping the loopback Host check strict.

Verified locally against a stand-alone studio instance:

Request Result
proxied module fetch (loopback Host, public Origin, matching X-Forwarded-Host, Sec-Fetch-Site: same-origin) 200
direct local /assets/app.js 200
proxied top-level navigation to / 200
cross-site module fetch (Sec-Fetch-Site: cross-site) 403
forged X-Forwarded-Host with no Fetch Metadata 403
public Host passed through 403 (Host check unchanged)
--- a/extensions/models/_lib/engine/studio_server.ts
+++ b/extensions/models/_lib/engine/studio_server.ts
@@ -196,6 +196,41 @@
     req.headers.get("sec-fetch-dest") === "document";
 }
 
+/**
+ * The host a reverse proxy in front of the studio published, or null. A
+ * proxy rewrites Host to loopback (the Host check below) and records the
+ * original client host in X-Forwarded-Host, so a browser's Origin names the
+ * public site rather than this server. The scheme is ignored: a
+ * TLS-terminating proxy may or may not pass X-Forwarded-Proto.
+ */
+function proxiedHost(req: Request): string | null {
+  const forwarded = req.headers.get("x-forwarded-host");
+  if (forwarded === null) return null;
+  const host = forwarded.split(",")[0].trim();
+  return host === "" ? null : host;
+}
+
+/**
+ * Whether this request's Origin names the host a reverse proxy published in
+ * X-Forwarded-Host. A browser sets Origin and Sec-Fetch-Site itself and can
+ * forge neither, so only a request the browser marks same-origin or
+ * user-initiated is let through this way; the Host must still be loopback
+ * (the check below), so a DNS-rebinding page cannot pass it as same-origin.
+ * This lets a proxied page load its own module scripts without trusting a
+ * requested host.
+ */
+function proxiedOrigin(origin: string, req: Request): boolean {
+  const site = req.headers.get("sec-fetch-site");
+  if (site !== "same-origin" && site !== "none") return false;
+  const host = proxiedHost(req);
+  if (host === null) return false;
+  try {
+    return new URL(origin).host === host;
+  } catch {
+    return false;
+  }
+}
+
 /** Why a request is refused before routing, or null. */
 function refusal(req: Request, port: number): Response | null {
   const host = req.headers.get("host");
@@ -206,7 +241,10 @@
     return error(403, "the Host header must name this studio server");
   }
   const origin = req.headers.get("origin");
-  if (origin !== null && origin !== `http://${host}`) {
+  if (
+    origin !== null && origin !== `http://${host}` &&
+    !proxiedOrigin(origin, req)
+  ) {
     return error(403, "requests from another origin are refused");
   }
   const site = req.headers.get("sec-fetch-site");

If you'd rather not trust X-Forwarded-Host, the same helper can compare the Origin against a configured publicOrigin method argument (or env var) instead; the Sec-Fetch-Site gate is the part worth keeping either way, since it is what stops a cross-site page from reusing the allowance.

shelson commented 10/10/2026, 8:02:50 AM

Follow-up: the first patch handles a proxy that rewrites Host to loopback (Traefik passHostHeader: false), but a second deployment shape still fails. A Cloudflare Tunnel public hostname pointing straight at the studio ([HOST-1] -> http://127.0.0.1:<port>) forwards the public Host unchanged, so the loopback Host check rejects every request, page and asset alike. That is a Host-check failure, not the Origin one from the original report.

I extended the patch with a STUDIO_PUBLIC_HOST allowlist (comma-separated). The loopback Host requirement stays the anti-DNS-rebinding gate; only explicitly named hosts are added on top, and the Origin check now accepts an Origin that names the already-validated Host (any scheme) as well as the X-Forwarded-Host case.

Verified behaviours:

Request Result
public host via tunnel, / and /assets/app.js 200
public host, module fetch (Origin: https://<public>, same-origin) 200
proxy rewriting Host to loopback + X-Forwarded-Host 200
direct local /assets/app.js 200
unconfigured public host 403
cross-site module fetch 403

For upstream, it would be better to expose this as a model argument (e.g. publicHost/publicOrigin) than an env var, so it is visible in the model definition and travels with the serve invocation. The invariant worth keeping either way: loopback Host (or an explicitly configured public host) plus a browser-set Sec-Fetch-Site gate.

--- a/extensions/models/_lib/engine/studio_server.ts
+++ b/extensions/models/_lib/engine/studio_server.ts
@@ -196,17 +196,70 @@
     req.headers.get("sec-fetch-dest") === "document";
 }
 
+/**
+ * The host a reverse proxy in front of the studio published, or null. A
+ * proxy rewrites Host to loopback (the Host check below) and records the
+ * original client host in X-Forwarded-Host, so a browser's Origin names the
+ * public site rather than this server. The scheme is ignored: a
+ * TLS-terminating proxy may or may not pass X-Forwarded-Proto.
+ */
+function proxiedHost(req: Request): string | null {
+  const forwarded = req.headers.get("x-forwarded-host");
+  if (forwarded === null) return null;
+  const host = forwarded.split(",")[0].trim();
+  return host === "" ? null : host;
+}
+
+/** Whether an Origin names the given Host, whatever scheme it used. */
+function sameHost(origin: string, host: string): boolean {
+  try {
+    return new URL(origin).host === host;
+  } catch {
+    return false;
+  }
+}
+
+/**
+ * Whether this request's Origin names the host a reverse proxy published in
+ * X-Forwarded-Host. A browser sets Origin and Sec-Fetch-Site itself and can
+ * forge neither, so only a request the browser marks same-origin or
+ * user-initiated is let through this way; the Host must still be loopback
+ * (the check below), so a DNS-rebinding page cannot pass it as same-origin.
+ * This lets a proxied page load its own module scripts without trusting a
+ * requested host.
+ */
+function proxiedOrigin(origin: string, req: Request): boolean {
+  const site = req.headers.get("sec-fetch-site");
+  if (site !== "same-origin" && site !== "none") return false;
+  const host = proxiedHost(req);
+  return host !== null && sameHost(origin, host);
+}
+
+/**
+ * Public Host values the studio answers to, from STUDIO_PUBLIC_HOST: the
+ * names a tunnel or proxy forwards unchanged (comma-separated, each with its
+ * port when it is not the scheme default). The Host check below stays the
+ * gate against DNS rebinding, so only explicitly named hosts are added to
+ * the loopback defaults.
+ */
+function publicHosts(): string[] {
+  return (Deno.env.get("STUDIO_PUBLIC_HOST") ?? "")
+    .split(",").map((host) => host.trim()).filter((host) => host !== "");
+}
+
 /** Why a request is refused before routing, or null. */
 function refusal(req: Request, port: number): Response | null {
   const host = req.headers.get("host");
-  const hosts = [`127.0.0.1:${port}`, `localhost:${port}`];
+  const hosts = [`127.0.0.1:${port}`, `localhost:${port}`, ...publicHosts()];
   // A browser leaves the default port out of Host and Origin.
   if (port === 80) hosts.push("127.0.0.1", "localhost");
   if (host === null || !hosts.includes(host)) {
     return error(403, "the Host header must name this studio server");
   }
   const origin = req.headers.get("origin");
-  if (origin !== null && origin !== `http://${host}`) {
+  if (
+    origin !== null && !sameHost(origin, host) && !proxiedOrigin(origin, req)
+  ) {
     return error(403, "requests from another origin are refused");
   }
   const site = req.headers.get("sec-fetch-site");

skunk-ape commented 10/12/2026, 1:54:54 AM

Thanks @shelson for reporting this! We shipped: Add an optional publicOrigin argument to the studio serve method. When set, the studio's pre-routing gate also accepts that origin's host as Host (Cloudflare Tunnel, passHostHeader true) and that exact origin as Origin (Traefik rewriting Host to loopback). X-Forwarded-Host is not trusted. Loopback Host stays the anti-DNS-rebinding gate; an unconfigured studio behaves as today, and a public Host no longer self-authorises http://Host as Origin. serve validates the value before binding and logs a no-authentication warning when it is set; docs say access control belongs in the proxy.. The fix has been merged and a release is on its way. We appreciate your contribution to swamp.

Sign in to post a ripple.