mirror of
https://github.com/FJurmanovic/sugarr.git
synced 2026-11-11 07:41:08 +00:00
Harden auth hook and fix eslint config for intentional unused args
- authHook: use crypto.timingSafeEqual for the bearer token comparison instead of !==, avoiding a timing side-channel on the secret key. - .eslintrc.cjs: add argsIgnorePattern for underscore-prefixed unused params, fixing false-positive no-unused-vars errors on intentional _ctx/_params placeholders used throughout the LlmTool hierarchy.
This commit is contained in:
@@ -1,5 +1,15 @@
|
||||
import { timingSafeEqual } from 'node:crypto';
|
||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
||||
|
||||
function safeEqual(a: string, b: string): boolean {
|
||||
const bufA = Buffer.from(a);
|
||||
const bufB = Buffer.from(b);
|
||||
if (bufA.length !== bufB.length) {
|
||||
return false;
|
||||
}
|
||||
return timingSafeEqual(bufA, bufB);
|
||||
}
|
||||
|
||||
export function registerAuthHook(
|
||||
app: FastifyInstance,
|
||||
secretKey: string,
|
||||
@@ -12,8 +22,9 @@ export function registerAuthHook(
|
||||
}
|
||||
const header = request.headers.authorization ?? '';
|
||||
const token = header.startsWith('Bearer ') ? header.slice('Bearer '.length) : undefined;
|
||||
if (token !== secretKey) {
|
||||
if (token === undefined || !safeEqual(token, secretKey)) {
|
||||
reply.code(401).send({ error: 'unauthorized' });
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user