mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +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: {
|
||||
|
||||
Reference in New Issue
Block a user