security: timing-safe API key, SSRF webhook guard, API no-cache, Referrer-Policy
This commit is contained in:
@@ -116,10 +116,17 @@ function apiKeyGuard(req, res, next) {
|
||||
if (p === '/api/health' || p.startsWith('/api/install')) return next();
|
||||
const key = getApiKey();
|
||||
if (!key) return next();
|
||||
if (req.headers['x-api-key'] !== key) return res.status(401).json({ error: '无效的 API 密钥' });
|
||||
if (!req.headers['x-api-key'] || req.headers['x-api-key'].length !== key.length) return res.status(401).json({ error: '无效的 API 密钥' });
|
||||
if (!timingSafeEqual(req.headers['x-api-key'], key)) return res.status(401).json({ error: '无效的 API 密钥' });
|
||||
next();
|
||||
}
|
||||
|
||||
function timingSafeEqual(a, b) {
|
||||
let diff = a.length ^ b.length;
|
||||
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
||||
return diff === 0;
|
||||
}
|
||||
|
||||
function methodGuard(allowed) {
|
||||
return (req, res, next) => {
|
||||
if (!allowed.includes(req.method)) {
|
||||
|
||||
Reference in New Issue
Block a user