mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
fix(cors): the instance's own address no longer trips ALLOWED_ORIGINS
With ALLOWED_ORIGINS set, every request carrying an Origin that was not on the list was failed with an unhandled error, which the exception filter answered with a 500. A browser sends Origin on every POST and WebSocket upgrade, including same-origin ones, so opening TREK under any address the list did not name (the LAN IP, a second hostname, http instead of https) broke the login with "Internal Server Error" while another browser that used the listed address worked. A request whose Origin is the host it was sent to is now treated as same-origin and always passes, for REST and the /ws upgrade alike. A foreign origin is still refused, now with a 403 "Not allowed by CORS" and a warning in the log naming the origin to add. Closes #2543
This commit is contained in:
@@ -5,6 +5,7 @@ import helmet from 'helmet';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import { readEnv, type AppEnv } from '../app-config';
|
||||
import { logDebug, logWarn, logError } from '../nest/audit/audit-log.logger';
|
||||
import { isSameHostOrigin } from '../nest/common/same-origin';
|
||||
|
||||
/**
|
||||
* Field names redacted from request-log query/body dumps (case-insensitive —
|
||||
@@ -162,17 +163,28 @@ export function applyGlobalMiddleware(
|
||||
|
||||
const allowedOrigins = http.corsOrigins;
|
||||
|
||||
let corsOrigin: cors.CorsOptions['origin'];
|
||||
if (allowedOrigins) {
|
||||
corsOrigin = (origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => {
|
||||
if (!origin || allowedOrigins.includes(origin)) callback(null, true);
|
||||
else callback(new Error('Not allowed by CORS'));
|
||||
};
|
||||
} else if (isProduction) {
|
||||
corsOrigin = false;
|
||||
} else {
|
||||
corsOrigin = true;
|
||||
}
|
||||
// With ALLOWED_ORIGINS set, a request from anywhere else is refused outright.
|
||||
// Two things keep that from hitting the instance's own pages (#2543): a request
|
||||
// whose Origin is the host it was sent to is same-origin and always passes, and
|
||||
// a refusal is a 403 naming the cause instead of an unhandled error, which the
|
||||
// exception filter turned into a bare 500 on the login screen.
|
||||
const warnedOrigins = new Set<string>();
|
||||
const corsOptions: cors.CorsOptions | cors.CorsOptionsDelegate<Request> = allowedOrigins
|
||||
? (req: Request, callback: (err: Error | null, options?: cors.CorsOptions) => void) => {
|
||||
const origin = req.headers.origin;
|
||||
if (!origin || allowedOrigins.includes(origin) || isSameHostOrigin(origin, req.headers.host)) {
|
||||
callback(null, { origin: true, credentials: true });
|
||||
return;
|
||||
}
|
||||
if (!warnedOrigins.has(origin) && warnedOrigins.size < 50) {
|
||||
warnedOrigins.add(origin);
|
||||
logWarn(
|
||||
`CORS: refused origin ${origin} for host ${req.headers.host ?? '(none)'}; add it to ALLOWED_ORIGINS if it is yours`,
|
||||
);
|
||||
}
|
||||
callback(Object.assign(new Error('Not allowed by CORS'), { statusCode: 403 }));
|
||||
}
|
||||
: { origin: isProduction ? false : true, credentials: true };
|
||||
|
||||
const shouldForceHttps = http.forceHttps;
|
||||
// HSTS is worth enabling any time we're serving production traffic,
|
||||
@@ -225,7 +237,7 @@ export function applyGlobalMiddleware(
|
||||
}
|
||||
},
|
||||
);
|
||||
app.use(cors({ origin: corsOrigin, credentials: true }));
|
||||
app.use(cors(corsOptions));
|
||||
app.use(helmet({
|
||||
contentSecurityPolicy: {
|
||||
directives: {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Whether a browser `Origin` names the very host the request was sent to.
|
||||
*
|
||||
* `ALLOWED_ORIGINS` exists to let OTHER origins in. A request whose Origin is the
|
||||
* instance's own host is not cross-origin at all, yet a browser still attaches the
|
||||
* header to every POST and to every WebSocket upgrade. Checking only the list
|
||||
* turned an ordinary login into a refusal as soon as the list and the address in
|
||||
* the address bar differed in any way (another hostname, the LAN IP, http against
|
||||
* https), which is #2543.
|
||||
*
|
||||
* The comparison is against the `Host` header only. The browser sets it from the
|
||||
* URL it was asked to load, and a page on another site cannot choose it, so a
|
||||
* foreign Origin never matches. `X-Forwarded-Host` is deliberately not consulted:
|
||||
* that one is client-controlled unless a proxy overwrites it.
|
||||
*
|
||||
* The scheme is compared through the host normalisation only (a default port on
|
||||
* the Host header is dropped for the Origin's own scheme), not for equality: TLS
|
||||
* usually ends at a reverse proxy, so the server itself cannot tell which scheme
|
||||
* the page was loaded over.
|
||||
*/
|
||||
export function isSameHostOrigin(origin: string | undefined, hostHeader: string | undefined): boolean {
|
||||
if (!origin || !hostHeader) return false;
|
||||
const host = hostHeader.trim();
|
||||
// A Host is a hostname with an optional port, nothing that a URL parser would
|
||||
// read as userinfo, a path or a query.
|
||||
if (!host || /[\s/\\@?#]/.test(host)) return false;
|
||||
let originUrl: URL;
|
||||
try {
|
||||
originUrl = new URL(origin);
|
||||
} catch {
|
||||
return false; // `null` and anything else that is not a URL
|
||||
}
|
||||
if (originUrl.protocol !== 'http:' && originUrl.protocol !== 'https:') return false;
|
||||
try {
|
||||
const requestHost = new URL(`${originUrl.protocol}//${host}`).host;
|
||||
return requestHost !== '' && requestHost === originUrl.host;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
import { WsAdapter } from '@nestjs/platform-ws';
|
||||
import type { INestApplicationContext } from '@nestjs/common';
|
||||
import type { Server as HttpServer } from 'node:http';
|
||||
import type { IncomingMessage, Server as HttpServer } from 'node:http';
|
||||
import type { MessageMappingProperties } from '@nestjs/websockets';
|
||||
import { WebSocketServer } from 'ws';
|
||||
import type { Observable } from 'rxjs';
|
||||
import { readEnv } from '../../app-config';
|
||||
import { setServer, type TrekWebSocket } from './ws-state';
|
||||
import { logError } from '../audit/audit-log.logger';
|
||||
import { isSameHostOrigin } from '../common/same-origin';
|
||||
|
||||
// Per-connection message rate limiting. It lives in the adapter, not in the
|
||||
// gateway's handlers, because the original counted EVERY inbound frame before
|
||||
@@ -122,10 +123,12 @@ export class TrekWsAdapter extends WsAdapter {
|
||||
...(allowedOrigins
|
||||
? {
|
||||
verifyClient: (
|
||||
{ origin }: { origin: string },
|
||||
{ origin, req }: { origin: string; req: IncomingMessage },
|
||||
cb: (ok: boolean, code?: number, msg?: string) => void,
|
||||
) => {
|
||||
if (!origin || allowedOrigins.includes(origin)) cb(true);
|
||||
// Same rule as the REST CORS check (#2543): the instance's own host
|
||||
// is never cross-origin, whatever ALLOWED_ORIGINS lists.
|
||||
if (!origin || allowedOrigins.includes(origin) || isSameHostOrigin(origin, req?.headers.host)) cb(true);
|
||||
else cb(false, 403, 'Origin not allowed');
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user