core/api: normalize bearer authorization

This commit is contained in:
jj 2024-10-20 10:00:00 +00:00
parent 0e52e1f8b0
commit d55dddea2e
No known key found for this signature in database

View file

@ -158,19 +158,20 @@ export const runAPI = (express, app, __dirname) => {
return fail(res, "error.api.auth.jwt.missing");
}
if (!authorization.startsWith("Bearer ") || authorization.length > 256) {
if (authorization.length >= 256) {
return fail(res, "error.api.auth.jwt.invalid");
}
const verifyJwt = jwt.verify(
authorization.split("Bearer ", 2)[1]
);
if (!verifyJwt) {
const [ type, token, ...rest ] = authorization.split(" ");
if (!token || type.toLowerCase() !== 'bearer' || rest.length) {
return fail(res, "error.api.auth.jwt.invalid");
}
req.rateLimitKey = generateHmac(req.header("Authorization"), ipSalt);
if (!jwt.verify(token)) {
return fail(res, "error.api.auth.jwt.invalid");
}
req.rateLimitKey = generateHmac(token, ipSalt);
} catch {
return fail(res, "error.api.generic");
}