Files
forumlify/lib/captcha.js
T
lezi-fun 7188d07aea security: server-side CAPTCHA (HMAC-signed) + magic-byte upload sniff + pagination bounds
Port upstream ac4af62 + PR #14/#17:
- lib/captcha.js: HMAC(CAPTCHA_SECRET, id:answer) challenge; GET
  /api/captcha returns {id, text, sig}; register/createPost/createReply
  verify answer server-side (bad answer / forged sig rejected)
- Frontend (Modals/NewPost/ReplyList): fetch challenge from server,
  submit id+answer+sig; no more client-side answer check (was bypassable)
- Upload: sniff JPEG/PNG/GIF/WebP magic bytes (ignore client MIME), UUID
  filenames — blocks spoofed HTML-as-image (stored XSS)
- Posts pagination: limit cap 50, page cap (deep-pagination DoS)
- CAPTCHA_SECRET env (falls back to JWT_SECRET)
2026-08-11 20:25:16 +08:00

38 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 服务端验证码:一次性算术挑战,答案签名用 HMAC(对齐上游 ac4af62)
// - GET /api/captcha 返回 { id, text, sig };sig = HMAC(secret, `${id}:${answer}`)
// - 前端提交 captcha_id + captcha_answer + sig
// - 服务端用提交的答案重算 HMAC 与 sig 比对(防绕过/重放,防前端读答案)
import crypto from 'crypto';
function secret() {
// CAPTCHA_SECRET 可单独配置;默认回退 JWT_SECRET
return process.env.CAPTCHA_SECRET || process.env.JWT_SECRET || 'forumlify';
}
// 生成挑战:返回 { id, text, answer, sig };sig 用于校验答案
export function generateCaptchaChallenge() {
const a = Math.floor(Math.random() * 10) + 1;
const b = Math.floor(Math.random() * 10) + 1;
const id = crypto.randomUUID();
const answer = a + b;
const sig = crypto
.createHmac('sha256', secret())
.update(`${id}:${answer}`)
.digest('hex');
return { id, text: `${a} + ${b} = ?`, answer, sig };
}
// 验证:提交的答案重算签名与 sig 比对(长度固定,timingSafeEqual 安全)
export function verifyCaptcha(id, answer, sig) {
if (!id || answer === undefined || answer === null || answer === '' || !sig) return false;
const n = typeof answer === 'number' ? answer : parseInt(answer, 10);
if (Number.isNaN(n)) return false;
const expected = crypto
.createHmac('sha256', secret())
.update(`${id}:${n}`)
.digest('hex');
const a = Buffer.from(expected, 'hex');
const b = Buffer.from(String(sig), 'hex');
return a.length === b.length && crypto.timingSafeEqual(a, b);
}