Skip to content

Server Security Guards ​

Overview ​

Two Nitro server utility files hold the request guards added by the security audit (PR #144). Both are auto-imported in server/ routes.

FileExportsUsed by
server/utils/publicUrlGuard.ts (new, commit 36e8cd9)hostOf, isAllowedAssetHost, isBlockedAddress, isPublicHttpsUrl, assertAllowedProxyUrlPDF Proxy, TXT Proxy
server/utils/auth.ts (extended, commit 36e8cd9)verifyBackendToken, requireVerifiedAuth, assertSameOrigin (new) next to the older getAuthToken, isAuthenticated, getUserData, requireAuthPDF and TXT proxies, Set Session, Clear Session

The backend side of token checks (signed sign-in tokens, re-authentication) is documented in Notion: Backend API — Sign-in Tokens and Re-authentication (Nov 2026).

publicUrlGuard.ts ​

assertAllowedProxyUrl(url) ​

Throws unless the URL is a public HTTPS address on an allowed asset host. Both errors are createError({ statusCode: 400, statusMessage: 'Bad Request', message }).

OrderCheckmessage on failure
1isAllowedAssetHost(hostOf(url))Host not allowed
2await isPublicHttpsUrl(url)URL is not an allowed public HTTPS address

Allowed asset hosts (isAllowedAssetHost) ​

The hostname is lowercased and must match one of two patterns. Anything else (the API host, the portal host, other CDNs) is refused.

NamePatternMatchesDoes not match
S3_HOST^(?:[a-z0-9.-]+\.)?s3[.-][a-z0-9-]*\.?amazonaws\.com$bucket.s3.us-east-1.amazonaws.com, bucket.s3-us-west-2.amazonaws.com, s3.amazonaws.combucket.s3.dualstack.us-east-1.amazonaws.com, amazonaws.com.example.net
CLOUDFRONT_HOST^[a-z0-9-]+\.cloudfront\.net$d123example.cloudfront.neta.b.cloudfront.net, cloudfront.net

The check is by host shape only: any S3 bucket or CloudFront distribution passes this step, not only Collage's own. hostOf returns '' for a URL that does not parse, which then fails the host check.

Public HTTPS check (isPublicHttpsUrl) ​

Rules in order; the first failure returns false:

  1. The value parses with new URL().
  2. protocol is https: (plain http: is refused).
  3. No username or password in the URL.
  4. If the hostname is an IP literal, it must not be blocked (isBlockedAddress).
  5. Otherwise the hostname is resolved with dns.lookup(host, { all: true }). The lookup must succeed, return at least one address, and every returned address must be unblocked. A DNS error returns false.

The DNS answer is checked once; the proxies then call fetch on the same URL, which resolves the name again. Redirects are not followed (redirect: 'manual', a 3xx becomes 502), so an allowed host cannot hand the request on to another address.

Blocked addresses (isBlockedAddress) ​

isIP(address) decides the family. A value that is neither IPv4 nor IPv6 is treated as blocked.

IPv4 ranges (BLOCKED_V4, compared with a bit mask):

RangeCovers
0.0.0.0/8"this network"
10.0.0.0/8private
100.64.0.0/10carrier-grade NAT
127.0.0.0/8loopback
169.254.0.0/16link-local, including cloud metadata addresses
172.16.0.0/12private
192.0.0.0/24IETF protocol assignments
192.168.0.0/16private
198.18.0.0/15benchmarking
224.0.0.0/4multicast
240.0.0.0/4reserved, including broadcast

An IPv4 string that does not split into four integer octets 0..255 is blocked.

IPv6 rules (isBlockedV6, lowercased, zone id after % dropped):

RuleBlocked
IPv4-mapped ::ffff:a.b.c.dwhen the embedded IPv4 is blocked
:: and ::1unspecified and loopback
starts with fc or fd followed by two hex digits and :unique local fc00::/7
starts with fe8, fe9, fea or feb followed by a hex digit and :link-local fe80::/10
starts with 2001:db8:documentation range

All other IPv6 addresses are allowed.

auth.ts ​

verifyBackendToken(token) ​

Asks the backend whether a token is live.

  • Request: GET <runtimeConfig.public.apiBaseUrl>user with Authorization: Bearer <token> and Accept: application/json.
  • Returns response.data.user, else response.user, else null.
  • Returns null when apiBaseUrl is empty or the request throws (any non-2xx, network error). It never throws.

requireVerifiedAuth(event) ​

Reads the auth_token cookie (getAuthToken) and verifies it.

CaseResult
no cookiethrows 401 Authentication required
verifyBackendToken returns nullthrows 401 Invalid authentication token
validreturns the backend user object

The older requireAuth(event) (cookie present, no backend call) is still exported; on origin/development nothing in server/ calls it.

assertSameOrigin(event) ​

A cross-site request check for the session routes. Every failure throws 403 Cross-site request rejected.

Rules in order:

  1. If a sec-fetch-site header is present and is not same-origin, reject. (same-site, cross-site and none are all rejected.)
  2. If there is no origin header, allow. Server-to-server calls and older browsers that send neither header pass.
  3. If origin does not parse as a URL, reject.
  4. Compare the origin's host (lowercased, includes the port) with the request host header and with the first value of x-forwarded-host. If it equals neither, reject.
RequestResult
sec-fetch-site: same-origin, origin: https://portal.example.com, host: portal.example.comallowed
sec-fetch-site: cross-site403
no sec-fetch-site, origin: https://other.example.net, host: portal.example.com403
origin: https://portal.example.com, host: 10.0.0.5:3000, x-forwarded-host: portal.example.com, edgeallowed (forwarded host matches)
no sec-fetch-site, no originallowed

Callers: POST /api/auth/set-session and POST /api/auth/clear-session, before any body is read.