All checks were successful
Deploy Palico Bot / deploy (push) Successful in 32s
- Deploy workflow: pin node:24-bookworm; install openssh-client only if missing - Auth failure limiter counts only 401s (422/404 no longer lock out clients) - trust proxy configurable (WEB_TRUSTED_PROXIES), never "true" - DM route: loopback connections only, dedicated NOTIFICATIONS_API_KEY, 503 when unset - Jukebox: no polling without a key, stop on 401 - /invitation: posts the temporary invite message after checking the link - Stop tracking storage/library.json; README describes the real deployment Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
48 lines
1.6 KiB
JavaScript
48 lines
1.6 KiB
JavaScript
import { config } from "../../config.js";
|
|
import { createHash, timingSafeEqual } from "node:crypto";
|
|
|
|
function tokenDigest(value) {
|
|
return createHash("sha256").update(value).digest();
|
|
}
|
|
|
|
function readBearerToken(req) {
|
|
const header = req.headers.authorization || "";
|
|
const match = /^Bearer ([^\s]+)$/.exec(header);
|
|
return match?.[1] || "";
|
|
}
|
|
|
|
// Exige `Authorization: Bearer <clé>`. Sans clé configurée, la route est désactivée (503),
|
|
// jamais ouverte.
|
|
function createBearerAuth(getExpectedKey) {
|
|
return function bearerAuth(req, res, next) {
|
|
const expectedKey = getExpectedKey();
|
|
if (!expectedKey) {
|
|
return res.status(503).json({ error: "Service désactivé", code: "DISABLED" });
|
|
}
|
|
|
|
if (timingSafeEqual(tokenDigest(readBearerToken(req)), tokenDigest(expectedKey))) {
|
|
return next();
|
|
}
|
|
|
|
return res.status(401).json({ error: "Unauthorized" });
|
|
};
|
|
}
|
|
|
|
const authMiddleware = createBearerAuth(() => config.webApiKey);
|
|
|
|
const LOOPBACK_ADDRESSES = new Set(["127.0.0.1", "::1", "::ffff:127.0.0.1"]);
|
|
|
|
function isLoopbackAddress(address) {
|
|
return LOOPBACK_ADDRESSES.has(address);
|
|
}
|
|
|
|
// N'accepte que les connexions ouvertes depuis la machine elle-même. On lit l'adresse de
|
|
// la socket, pas req.ip : une requête relayée par Caddy arrive de son adresse à lui.
|
|
function localOnly(req, res, next) {
|
|
if (isLoopbackAddress(req.socket?.remoteAddress)) {
|
|
return next();
|
|
}
|
|
return res.status(403).json({ error: "Route réservée aux applications locales", code: "LOCAL_ONLY" });
|
|
}
|
|
|
|
export { authMiddleware, createBearerAuth, isLoopbackAddress, localOnly };
|