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)
This commit is contained in:
lezi-fun committed 2026-08-11 20:25:16 +08:00
1 parent 2780dab8e2
commit 7188d07aea
12 files changed
+141 -51

No files matched your search

+19 -6
View File
@@ -41,8 +41,15 @@ export const API = {
if (data.token) setToken(data.token);
return data;
},
register(email, password, username) {
return apiFetch('/auth/register', { method: 'POST', body: JSON.stringify({ email, password, username }) });
// 服务端验证码(HMAC 签名挑战)
getCaptcha() {
return apiFetch('/captcha');
},
register(email, password, username, captcha) {
return apiFetch('/auth/register', {
method: 'POST',
body: JSON.stringify({ email, password, username, captcha_id: captcha?.id, captcha_answer: captcha?.answer, captcha_sig: captcha?.sig }),
});
},
logout() { setToken(null); },
getMe() { return apiFetch('/auth/me'); },
@@ -54,8 +61,11 @@ export const API = {
return apiFetch(q);
},
getPost(postId) { return apiFetch('/posts/' + postId); },
createPost(title, content, images) {
return apiFetch('/posts', { method: 'POST', body: JSON.stringify({ title, content, images: images || [] }) });
createPost(title, content, images, captcha) {
return apiFetch('/posts', {
method: 'POST',
body: JSON.stringify({ title, content, images: images || [], captcha_id: captcha?.id, captcha_answer: captcha?.answer, captcha_sig: captcha?.sig }),
});
},
updatePost(postId, title, content, images) {
return apiFetch('/posts/' + postId, { method: 'PUT', body: JSON.stringify({ title, content, images }) });
@@ -67,8 +77,11 @@ export const API = {
// 回复
getReplies(postId) { return apiFetch('/posts/' + postId + '/replies'); },
createReply(postId, content) {
return apiFetch('/posts/' + postId + '/replies', { method: 'POST', body: JSON.stringify({ content }) });
createReply(postId, content, captcha) {
return apiFetch('/posts/' + postId + '/replies', {
method: 'POST',
body: JSON.stringify({ content, captcha_id: captcha?.id, captcha_answer: captcha?.answer, captcha_sig: captcha?.sig }),
});
},
deleteReply(replyId) { return apiFetch('/replies/' + replyId, { method: 'DELETE' }); },