1616// server's heavy render dependencies (@hyperframes/producer, engine, …).
1717// ---------------------------------------------------------------------------
1818
19- import { hostname , networkInterfaces } from "node:os " ;
19+ import { identityAllowed , isLoopbackHost } from "@hyperframes/studio-server/request-identity " ;
2020import { readConfig , readConfigFresh } from "../telemetry/config.js" ;
2121import {
2222 resetTelemetryPostureCache ,
@@ -25,6 +25,12 @@ import {
2525import { canaryDecisionsForStudio , type CliCanaryDecision } from "../telemetry/canary.js" ;
2626import { detectAgentRuntime } from "../telemetry/agent_runtime.js" ;
2727
28+ // The host-identity half of this module moved to
29+ // @hyperframes /studio-server/request-identity, where the studio API's
30+ // write guard shares the same definition. Re-exported so this module's
31+ // existing importers keep one source for it.
32+ export { identityAllowed , isLoopbackHost } ;
33+
2834/**
2935 * The CLI's anonymous distinct id to hand to Studio, or null when CLI telemetry
3036 * is disabled or no id is available. Fail-silent — telemetry must never break
@@ -58,33 +64,6 @@ function resolveCliBucketSeed(): string | null {
5864 }
5965}
6066
61- /**
62- * Is this request's `Host` a loopback name the studio server could have been
63- * reached on directly?
64- *
65- * A bare `[::1]`/`localhost`/dotted-quad check rather than a full parse: the
66- * port is irrelevant (any port on loopback is us), and anything exotic enough
67- * to miss here should be refused rather than guessed at.
68- *
69- * Scope, stated precisely: this is a **DNS-rebinding mitigation for browsers**,
70- * not access control. A browser sets `Host` from the URL it was given, so a
71- * page that rebinds its own hostname to 127.0.0.1 arrives carrying that
72- * hostname and is refused. A non-browser client sets `Host` to whatever it
73- * likes, so this stops nothing there — but on a loopback-bound server such a
74- * client is already local, and on a LAN-bound one it can read the project
75- * files through the unauthenticated studio API anyway. See `identityAllowed`.
76- */
77- export function isLoopbackHost ( host : string | undefined ) : boolean {
78- if ( ! host ) return false ;
79- // Strip the port. IPv6 literals are bracketed (`[::1]:1234`), so take the
80- // bracketed part when present and only split on ":" otherwise.
81- const bracketed = / ^ \[ ( [ ^ \] ] + ) \] / . exec ( host ) ;
82- const hostname = ( bracketed ? bracketed [ 1 ] : host . split ( ":" ) [ 0 ] ) ?. toLowerCase ( ) ?? "" ;
83- if ( hostname === "localhost" || hostname === "::1" || hostname === "0:0:0:0:0:0:0:1" ) return true ;
84- // 127.0.0.0/8 — the whole loopback block, not just 127.0.0.1.
85- return / ^ 1 2 7 \. \d { 1 , 3 } \. \d { 1 , 3 } \. \d { 1 , 3 } $ / . test ( hostname ) ;
86- }
87-
8867// JSON.stringify does not escape "<" or "/". Escaping both means no
8968// "</script>" (or "</…") sequence can form in the emitted value, so it can
9069// never terminate the inline <script> or open a new tag. (The values are
@@ -197,96 +176,6 @@ export function buildStudioHeadScripts(
197176 return `${ buildCliIdentityScript ( options ) } ${ envScript } ` ;
198177}
199178
200- /**
201- * The `<head>` scripts for a request, given its `Host`.
202- *
203- * The Host split lives here rather than in the route so it is testable
204- * without a Studio bundle on disk — the route's own test can only reach the
205- * injection branch when `packages/studio/dist` happens to be built, which is
206- * true locally and false in the CI test lane.
207- */
208- /** Hostname of a `Host` header, port and IPv6 brackets removed. */
209- function hostnameOf ( host : string | undefined ) : string {
210- if ( ! host ) return "" ;
211- const bracketed = / ^ \[ ( [ ^ \] ] + ) \] / . exec ( host ) ;
212- return ( bracketed ? bracketed [ 1 ] : host . split ( ":" ) [ 0 ] ) ?. toLowerCase ( ) ?? "" ;
213- }
214-
215- /**
216- * Names this machine legitimately answers to on a wildcard bind: every
217- * interface address, plus its own hostname and the `.local` mDNS form people
218- * actually type. Fail closed — a throw yields an empty set, which denies.
219- */
220- function localNames ( ) : Set < string > {
221- const names = new Set < string > ( ) ;
222- try {
223- for ( const entries of Object . values ( networkInterfaces ( ) ) ) {
224- for ( const entry of entries ?? [ ] ) names . add ( entry . address . toLowerCase ( ) ) ;
225- }
226- const self = hostname ( ) . toLowerCase ( ) ;
227- if ( self !== "" ) {
228- names . add ( self ) ;
229- // `my-box` is reachable as `my-box.local`, and a `my-box.lan.example`
230- // FQDN is reachable by its short form. Register both directions.
231- names . add ( `${ self } .local` ) ;
232- const short = self . split ( "." ) [ 0 ] ;
233- if ( short !== undefined && short !== "" ) {
234- names . add ( short ) ;
235- names . add ( `${ short } .local` ) ;
236- }
237- }
238- } catch {
239- /* fail closed */
240- }
241- return names ;
242- }
243-
244- /**
245- * Wildcard binds answer on every local name; a specific bind on itself only.
246- *
247- * This is the part that was missing: the previous rule accepted ANY Host once
248- * the env var was set, so a rebinding page could name itself `evil.example.com`
249- * and still be handed the CLI identity.
250- */
251- function hostMatchesBind ( host : string | undefined , bind : string ) : boolean {
252- const requested = hostnameOf ( host ) ;
253- if ( requested === "" ) return false ;
254- const bound = bind . toLowerCase ( ) ;
255- if ( requested === bound ) return true ;
256- return bound === "0.0.0.0" || bound === "::" || bound === "*"
257- ? localNames ( ) . has ( requested )
258- : false ;
259- }
260-
261- /**
262- * May this request receive the CLI's identity (distinct id + bucket seed)?
263- *
264- * The server binds loopback by DEFAULT and exposes the LAN only when an
265- * operator sets `HYPERFRAMES_PREVIEW_HOST` (portUtils.ts, F-001). A loopback
266- * `Host` is always fine: whatever reached us came via loopback, and the only
267- * interesting attacker is a rebinding browser page — which the Host check
268- * catches, because a browser cannot forge `Host`.
269- *
270- * For anything else the bind decides:
271- *
272- * - **Unset, or bound to loopback.** No LAN exposure was requested, so the
273- * Host check is a live rebinding mitigation and a non-loopback Host is
274- * refused. Previously ANY non-empty value disabled the check, so even
275- * `HYPERFRAMES_PREVIEW_HOST=127.0.0.1` — which exposes nothing — turned the
276- * loopback service from hostile-Host refusal into accept-everything.
277- * - **Bound to a LAN address.** The operator opted into exposure, so identity
278- * has to reach the LAN name the user browses or the CLI-to-Studio stitch
279- * breaks and Studio mints a second person for the same human. But it is
280- * still checked: the Host must name an address this machine actually
281- * answers on, not any attacker-chosen name a rebinding page supplies.
282- */
283- export function identityAllowed ( host : string | undefined ) : boolean {
284- if ( isLoopbackHost ( host ) ) return true ;
285- const bind = ( process . env [ "HYPERFRAMES_PREVIEW_HOST" ] ?? "" ) . trim ( ) ;
286- if ( bind === "" || isLoopbackHost ( bind ) ) return false ;
287- return hostMatchesBind ( host , bind ) ;
288- }
289-
290179/**
291180 * Re-read the persisted telemetry preference for this request.
292181 *
@@ -309,6 +198,14 @@ export function refreshTelemetryPosture(): void {
309198 }
310199}
311200
201+ /**
202+ * The `<head>` scripts for a request, given its `Host`.
203+ *
204+ * The Host split lives here rather than in the route so it is testable
205+ * without a Studio bundle on disk — the route's own test can only reach the
206+ * injection branch when `packages/studio/dist` happens to be built, which is
207+ * true locally and false in the CI test lane.
208+ */
312209export function buildStudioHeadScriptsForHost ( envScript : string , host : string | undefined ) : string {
313210 refreshTelemetryPosture ( ) ;
314211 return buildStudioHeadScripts ( envScript , { includeIdentity : identityAllowed ( host ) } ) ;
0 commit comments