mirror of
https://dev.azure.com/tstanciu94/PhantomMind/_git/Bitip
synced 2025-10-13 01:52:19 +03:00
refactor: enhance frontend API key validation by checking same-origin requests
This commit is contained in:
parent
ae34658fba
commit
06a1b84f03
@ -33,11 +33,23 @@ export const apiKeyAuth = (
|
|||||||
const origin = req.headers.origin || req.headers.referer;
|
const origin = req.headers.origin || req.headers.referer;
|
||||||
const allowedOrigins = config.frontendAllowedOrigins;
|
const allowedOrigins = config.frontendAllowedOrigins;
|
||||||
|
|
||||||
// Check if origin is present and matches allowed origins
|
// If no origin/referer header, check if it's a same-origin request by checking Host header
|
||||||
if (!origin) {
|
if (!origin) {
|
||||||
|
const host = req.headers.host;
|
||||||
|
const protocol = req.protocol || 'http';
|
||||||
|
const requestOrigin = `${protocol}://${host}`;
|
||||||
|
|
||||||
|
// Check if the request comes from an allowed origin based on Host header
|
||||||
|
const isSameOriginAllowed = allowedOrigins.some(allowed =>
|
||||||
|
requestOrigin.startsWith(allowed)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!isSameOriginAllowed) {
|
||||||
logger.warn('Frontend API key used without origin/referer header', {
|
logger.warn('Frontend API key used without origin/referer header', {
|
||||||
ip: req.ip,
|
ip: req.ip,
|
||||||
path: req.path,
|
path: req.path,
|
||||||
|
host: host,
|
||||||
|
requestOrigin: requestOrigin,
|
||||||
userAgent: req.headers['user-agent'],
|
userAgent: req.headers['user-agent'],
|
||||||
});
|
});
|
||||||
res.status(403).json({
|
res.status(403).json({
|
||||||
@ -47,6 +59,12 @@ export const apiKeyAuth = (
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Same-origin request allowed
|
||||||
|
req.apiKeyType = 'frontend';
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const isOriginAllowed = allowedOrigins.some(allowed =>
|
const isOriginAllowed = allowedOrigins.some(allowed =>
|
||||||
origin.startsWith(allowed)
|
origin.startsWith(allowed)
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user