Appearance
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.
| File | Exports | Used by |
|---|---|---|
server/utils/publicUrlGuard.ts (new, commit 36e8cd9) | hostOf, isAllowedAssetHost, isBlockedAddress, isPublicHttpsUrl, assertAllowedProxyUrl | PDF Proxy, TXT Proxy |
server/utils/auth.ts (extended, commit 36e8cd9) | verifyBackendToken, requireVerifiedAuth, assertSameOrigin (new) next to the older getAuthToken, isAuthenticated, getUserData, requireAuth | PDF 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 }).
| Order | Check | message on failure |
|---|---|---|
| 1 | isAllowedAssetHost(hostOf(url)) | Host not allowed |
| 2 | await 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.
| Name | Pattern | Matches | Does 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.com | bucket.s3.dualstack.us-east-1.amazonaws.com, amazonaws.com.example.net |
CLOUDFRONT_HOST | ^[a-z0-9-]+\.cloudfront\.net$ | d123example.cloudfront.net | a.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:
- The value parses with
new URL(). protocolishttps:(plainhttp:is refused).- No
usernameorpasswordin the URL. - If the hostname is an IP literal, it must not be blocked (
isBlockedAddress). - 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 returnsfalse.
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):
| Range | Covers |
|---|---|
0.0.0.0/8 | "this network" |
10.0.0.0/8 | private |
100.64.0.0/10 | carrier-grade NAT |
127.0.0.0/8 | loopback |
169.254.0.0/16 | link-local, including cloud metadata addresses |
172.16.0.0/12 | private |
192.0.0.0/24 | IETF protocol assignments |
192.168.0.0/16 | private |
198.18.0.0/15 | benchmarking |
224.0.0.0/4 | multicast |
240.0.0.0/4 | reserved, 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):
| Rule | Blocked |
|---|---|
IPv4-mapped ::ffff:a.b.c.d | when the embedded IPv4 is blocked |
:: and ::1 | unspecified 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>userwithAuthorization: Bearer <token>andAccept: application/json. - Returns
response.data.user, elseresponse.user, elsenull. - Returns
nullwhenapiBaseUrlis empty or the request throws (any non-2xx, network error). It never throws.
requireVerifiedAuth(event) ​
Reads the auth_token cookie (getAuthToken) and verifies it.
| Case | Result |
|---|---|
| no cookie | throws 401 Authentication required |
verifyBackendToken returns null | throws 401 Invalid authentication token |
| valid | returns 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:
- If a
sec-fetch-siteheader is present and is notsame-origin, reject. (same-site,cross-siteandnoneare all rejected.) - If there is no
originheader, allow. Server-to-server calls and older browsers that send neither header pass. - If
origindoes not parse as a URL, reject. - Compare the origin's
host(lowercased, includes the port) with the requesthostheader and with the first value ofx-forwarded-host. If it equals neither, reject.
| Request | Result |
|---|---|
sec-fetch-site: same-origin, origin: https://portal.example.com, host: portal.example.com | allowed |
sec-fetch-site: cross-site | 403 |
no sec-fetch-site, origin: https://other.example.net, host: portal.example.com | 403 |
origin: https://portal.example.com, host: 10.0.0.5:3000, x-forwarded-host: portal.example.com, edge | allowed (forwarded host matches) |
no sec-fetch-site, no origin | allowed |
Callers: POST /api/auth/set-session and POST /api/auth/clear-session, before any body is read.