Harden bot, storage and web API

This commit is contained in:
Shinuwa 2026-06-30 17:25:00 +02:00
parent e974bc6660
commit 20624a5c9c
43 changed files with 1265 additions and 1012 deletions

View file

@ -1,4 +1,9 @@
import { config } from "../../config.js";
import { createHash, timingSafeEqual } from "node:crypto";
function tokenDigest(value) {
return createHash("sha256").update(value).digest();
}
function authMiddleware(req, res, next) {
if (!config.webApiKey) {
@ -6,9 +11,10 @@ function authMiddleware(req, res, next) {
}
const header = req.headers.authorization || "";
const token = header.replace("Bearer ", "");
const match = /^Bearer ([^\s]+)$/.exec(header);
const token = match?.[1] || "";
if (token === config.webApiKey) {
if (timingSafeEqual(tokenDigest(token), tokenDigest(config.webApiKey))) {
return next();
}