http/origin.ts

Request source verification middleware for API protection.

Verifies requests carry an Origin the server allows, or none. CSRF protection is SameSite: strict on session cookies (see auth/session_middleware.ts) plus the CORS simple-request refusal (http/simple_request.ts) — SameSite alone admits same-site pages, which include every other port on the same host. This module provides origin allowlisting for locally-running services — preventing untrusted websites from making requests as the user browses the web — and is the only gate on the WebSocket upgrade, which is never preflighted.

The allowlist grammar is shared with the Rust spine entry for entry: parse_allowed_origins accepts and refuses the same entries, naming each refusal with the same AllowedOriginsErrorKind string, and an accepted list matches the same origins. The shared conformance table (testing/cross_backend/origin_conformance.json) pins both.

view source

Declarations
#

12 declarations

ALLOWED_ORIGINS_ERROR_MESSAGES
#

http/origin.ts view source

Readonly<Record<"empty_entry" | "not_printable_ascii" | "missing_scheme" | "unsupported_scheme" | "has_path" | "has_userinfo" | "missing_host" | "invalid_host_char" | "unbracketed_ipv6" | ... 7 more ... | "no_origins", string>>

type Readonly<Record<AllowedOriginsErrorKind, string>>

import {ALLOWED_ORIGINS_ERROR_MESSAGES} from '@fuzdev/fuz_app/http/origin.js';

The operator-facing reason for each refusal kind.

AllowedOriginsError
#

http/origin.ts view source

AllowedOriginsError import type {AllowedOriginsError} from '@fuzdev/fuz_app/http/origin.js';

One reason parse_allowed_origins refused a value.

kind

type AllowedOriginsErrorKind

readonly

index?

The refused entry's 1-based position; absent for no_origins.

type number

readonly

entry?

The refused entry as given, trimmed; absent for no_origins.

type string

readonly

AllowedOriginsErrorKind
#

http/origin.ts view source

value + type

"empty_entry" | "not_printable_ascii" | "missing_scheme" | "unsupported_scheme" | "has_path" | "has_userinfo" | "missing_host" | "invalid_host_char" | "unbracketed_ipv6" | ... 7 more ... | "no_origins"

type "empty_entry" | "not_printable_ascii" | "missing_scheme" | "unsupported_scheme" | "has_path" | "has_userinfo" | "missing_host" | "invalid_host_char" | "unbracketed_ipv6" | "partial_wildcard" | "non_canonical_ipv4" | "unclosed_ipv6" | "invalid_ipv6" | "invalid_after_ipv6" | "invalid_port" | "default_port" | "no_origins"

import {AllowedOriginsErrorKind} from '@fuzdev/fuz_app/http/origin.js';

Why an allowlist value was refused: no_origins (a blank value) or an entry's kind. A one_of whose def is the names.

format_allowed_origins_error
#

http/origin.ts view source

(error: AllowedOriginsError): string import {format_allowed_origins_error} from '@fuzdev/fuz_app/http/origin.js';

Render one refusal for a boot error — the entry in double quotes with every character but visible ASCII and the space escaped as \uXXXX (and " and \ backslash-escaped), so an env value can't put a control, bidi, or other non-printing character in a log, nor close the quotes it is shown in.

error

one of parse_allowed_origins's errors

returns

string

the reason as one line

is_browser_context
#

http/origin.ts view source

(c: Context<any, any, {}>): boolean import {is_browser_context} from '@fuzdev/fuz_app/http/origin.js';

True when the request looks like it originated from a browser context — it carries an Origin or Referer header. Browsers attach these automatically on the state-changing surface; loopback / CLI clients don't. Uses !== undefined so an empty-string header still counts as browser context. Checks Referer too (not just Origin) because some browser requests send only Referer. Twin of the Rust spine's bearer_auth::is_browser_context.

The bearer (auth/bearer_auth.ts) and daemon-token (auth/daemon_token_middleware.ts) middleware both silently discard their credential when this is true: a stolen bearer token can't be replayed from a browser, and the loopback daemon token never legitimately carries an Origin. Distinct from verify_request_source — that gates browser requests against an allowlist; this only detects browser context.

c

type Context<any, any, {}>

returns

boolean

origin_pattern_of
#

http/origin.ts view source

(regexp: RegExp): OriginPattern | undefined import {origin_pattern_of} from '@fuzdev/fuz_app/http/origin.js';

The pattern regexp was parsed from, when parse_allowed_origins (of any fuz_app copy) built it.

regexp

a matcher from an allowlist

type RegExp

returns

OriginPattern | undefined

its OriginPattern, or undefined for a matcher built another way

OriginPattern
#

http/origin.ts view source

OriginPattern import type {OriginPattern} from '@fuzdev/fuz_app/http/origin.js';

One validated allowlist entry, as parse_allowed_origins parsed it — the twin of the Rust fuz_http::OriginPattern.

entry

The entry as written (trimmed).

type string

readonly

regexp

The matcher should_allow_origin tests.

type RegExp

readonly

host

The host as parsed, lowercased: .-joined labels (a * label as *), or a bracketed IPv6 literal.

type string

readonly

host_labels

The host's labels, lowercased; null for a bracketed IPv6 host, which is exact.

type readonly string[] | null

readonly

port

No port, :* (any port, or none), or one exact port.

type "absent" | "any" | "exact"

readonly

OriginPatternErrorKind
#

http/origin.ts view source

value + type

"empty_entry" | "not_printable_ascii" | "missing_scheme" | "unsupported_scheme" | "has_path" | "has_userinfo" | "missing_host" | "invalid_host_char" | "unbracketed_ipv6" | ... 6 more ... | "default_port"

type "empty_entry" | "not_printable_ascii" | "missing_scheme" | "unsupported_scheme" | "has_path" | "has_userinfo" | "missing_host" | "invalid_host_char" | "unbracketed_ipv6" | "partial_wildcard" | "non_canonical_ipv4" | "unclosed_ipv6" | "invalid_ipv6" | "invalid_after_ipv6" | "invalid_port" | "default_port"

import {OriginPatternErrorKind} from '@fuzdev/fuz_app/http/origin.js';

Why one allowlist entry was refused, a @fuzdev/fuz_schema one_of whose def is the names. The wire names both spines emit — the Rust spine's OriginPatternError::kind returns the same strings.

parse_allowed_origins
#

http/origin.ts view source

(raw: string): Result<{ origins: RegExp[]; patterns: OriginPattern[]; }, { errors: AllowedOriginsError[]; }> import {parse_allowed_origins} from '@fuzdev/fuz_app/http/origin.js';

Parses an allowlist value (conventionally the FUZ_ALLOWED_ORIGINS env var) into regex matchers for request source verification. Origin allowlisting for locally-running services — not the CSRF layer (that's SameSite: strict on session cookies plus http/simple_request.ts).

Entries are comma-separated and trimmed of ASCII whitespace. Each is written the way a browser writes an Origin — scheme://host[:port] — where a whole host label or the port may be *:

  • Exact origins: https://api.fuz.dev, http://localhost:3000
  • Wildcard labels: https://*.fuz.dev (each * is exactly one label, in any position — api.fuz.dev matches, fuz.dev and a.b.fuz.dev don't)
  • Wildcard ports: http://localhost:* (any port or none)
  • IPv6 addresses: http://[::1]:3000, https://[2001:db8::1] (exact)
  • Combined: https://*.fuz.dev:*

Refused, nothing normalized: anything but printable ASCII (punycode an international hostname), a scheme other than http or https (null, file://, chrome-extension://), a path, query, fragment or trailing slash, userinfo, a character no hostname holds, a partial wildcard, an IPv4 address not written as four canonical decimal numbers (127.1), an IPv6 address not in the form a browser sends, a port outside 1..=65535 or with leading zeros, the scheme's default port, and an empty entry. Every refused entry is reported, not just the first.

raw

the comma-separated allowlist value

type string

returns

Result

the matchers and the patterns they were parsed from, or every refusal — no_origins when the value is blank

quote_ascii
#

http/origin.ts view source

(value: string): string import {quote_ascii} from '@fuzdev/fuz_app/http/origin.js';

value in double quotes, ASCII-only: " and \ backslash-escaped, every other UTF-16 code unit outside \x20-\x7e as \uXXXX — for a client- or env-written value shown in a log line. Twin of the Rust fuz_sys::text::quoted.

value

the text to quote

type string

returns

string

the quoted text

should_allow_origin
#

http/origin.ts view source

(origin: string, allowed_patterns: readonly RegExp[]): boolean import {should_allow_origin} from '@fuzdev/fuz_app/http/origin.js';

Tests if an Origin value matches any of the allowed patterns. Pattern matching is case-insensitive (web hosts and schemes are). A value that isn't visible ASCII matches nothing — a browser never sends one.

origin

type string

allowed_patterns

type readonly RegExp[]

returns

boolean

verify_request_source
#

http/origin.ts view source

(allowed_patterns: readonly RegExp[], carve_outs?: readonly MiddlewareCarveOut[]): Handler import {verify_request_source} from '@fuzdev/fuz_app/http/origin.js';

Middleware that verifies the request source against an allowlist.

Origin allowlisting (not the CSRF layer — that's SameSite: strict cookies plus http/simple_request.ts):

  • Checks the Origin header (if present) against the allowlist
  • Allows requests without an Origin header (direct access, curl, etc.)
  • Refuses an Origin that isn't visible ASCII, and a duplicated one — the runtimes join a repeated header into one value (a, b), which carries a space and so matches nothing

Origin-only by design — Fetch spec mandates Origin on every unsafe method (POST / PUT / DELETE / PATCH) regardless of Referrer-Policy, so every real browser request on the state-changing surface carries it. Non-browser clients (curl, server-to-server, CLI) don't ship auto- attached session cookies, so CSRF isn't the relevant threat there — auth (bearer / daemon token) is the actual control. A Referer fallback would only widen the accepted-shape envelope without closing a real CSRF hole. The Rust spine's origin_layer is the twin.

create_app_server mounts it on every path (ORIGIN_MIDDLEWARE_PATH); a request whose path a carve_outs entry matches passes unchecked — the named, explicit exception, each with its reason, as the Rust stack's origin_carve_out is.

allowed_patterns

compiled regex patterns from parse_allowed_origins

type readonly RegExp[]

carve_outs?

paths the check skips, matched like a middleware path (middleware_applies)

type readonly MiddlewareCarveOut[]
optional default []

returns

Handler

Depends on
#

Imported by
#