fix: add early API key validation

This commit is contained in:
Sha Zhou 2025-05-16 14:58:17 +08:00
parent 3d6e6f73ea
commit f1ef6c5cd0

View File

@ -74,6 +74,16 @@ export const jinaAiMiddleware = (req: Request, res: Response, next: NextFunction
next();
return;
}
// Early API key validation - reject immediately if no valid auth header
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
corsMiddleware(req, res, () => {
res.status(401).json({ error: 'Unauthorized: API key required' });
});
return;
}
asyncLocalContext.run(async () => {
const googleTraceId = req.get('x-cloud-trace-context')?.split('/')?.[0];
const ctx = asyncLocalContext.ctx;