From b9d6ca5da8e6e213ce76d2d4346a5d1e67d825d9 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Wed, 12 Aug 2026 09:06:50 +0800 Subject: [PATCH] security: server-owned audit logs + fullscreen custom pages Port upstream PR #22 + Lite style updates: - lib/audit.js: server-side logAudit(req, action, userId), records IP from x-forwarded-for / cf-connecting-ip - login/register/create_post/create_reply record server-side - Remove client logEvent calls (AppProvider/NewPost/ReplyList) - Remove forgeable POST /api/event-logs endpoint - CustomPage: fullscreen immersive (fixed, 100vh-nav, mobile 56px nav offset) matching upstream Lite --- app/api/auth/login/route.js | 3 +++ app/api/auth/register/route.js | 3 +++ app/api/event-logs/route.js | 19 ++---------------- app/api/posts/[id]/replies/route.js | 2 ++ app/api/posts/route.js | 2 ++ app/globals.css | 28 ++++++++++++++++++++++++++ components/AppProvider.js | 5 +---- components/CustomPage.js | 2 +- components/NewPost.js | 1 - components/ReplyList.js | 1 - lib/audit.js | 31 +++++++++++++++++++++++++++++ 11 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 lib/audit.js diff --git a/app/api/auth/login/route.js b/app/api/auth/login/route.js index fc32072..ebff3c3 100644 --- a/app/api/auth/login/route.js +++ b/app/api/auth/login/route.js @@ -3,6 +3,7 @@ import bcrypt from 'bcryptjs'; import jwt from 'jsonwebtoken'; import pool from '@/lib/db'; import { JWT_SECRET } from '@/lib/auth'; +import { logAudit } from '@/lib/audit'; export async function POST(req) { const { email, password } = await req.json(); @@ -24,6 +25,8 @@ export async function POST(req) { JWT_SECRET, { expiresIn: '7d' } ); + // 服务端审计:登录成功 + await logAudit(req, 'login', user.id); return Response.json({ token, user: { diff --git a/app/api/auth/register/route.js b/app/api/auth/register/route.js index a5dff7d..6f4e9d8 100644 --- a/app/api/auth/register/route.js +++ b/app/api/auth/register/route.js @@ -2,6 +2,7 @@ import bcrypt from 'bcryptjs'; import pool from '@/lib/db'; import { verifyCaptcha } from '@/lib/captcha'; +import { logAudit } from '@/lib/audit'; export async function POST(req) { const { email, password, username, captcha_id, captcha_answer, captcha_sig } = await req.json(); @@ -30,6 +31,8 @@ export async function POST(req) { [email, hash, username, avatar, role] ); + // 服务端审计:注册成功 + await logAudit(req, 'register', r.rows[0].id); return Response.json({ user: r.rows[0], message: isFirstUser ? '你是第一个用户,已自动设为管理员!' : '注册成功', diff --git a/app/api/event-logs/route.js b/app/api/event-logs/route.js index d493b3d..1df0245 100644 --- a/app/api/event-logs/route.js +++ b/app/api/event-logs/route.js @@ -27,20 +27,5 @@ export async function GET(req) { } } -export async function POST(req) { - const user = getUser(req); - if (!user) { - return Response.json({ error: '请先登录' }, { status: 401 }); - } - const { action } = await req.json(); - try { - await pool.query( - `INSERT INTO event_logs (user_id, action, ip) - VALUES ($1, $2, $3)`, - [user.id, action, req.headers.get('x-forwarded-for') || '0.0.0.0'] - ); - return Response.json({ success: true }); - } catch { - return Response.json({ success: true }); - } -} +// 审计日志由服务端在关键操作后记录(对齐上游 PR #22)—— +// 客户端上报端点已移除,防止伪造审计记录 diff --git a/app/api/posts/[id]/replies/route.js b/app/api/posts/[id]/replies/route.js index 5f13b29..3be394c 100644 --- a/app/api/posts/[id]/replies/route.js +++ b/app/api/posts/[id]/replies/route.js @@ -3,6 +3,7 @@ import pool from '@/lib/db'; import { getUser } from '@/lib/auth'; import { jsonWithEtag } from '@/lib/http-cache'; import { verifyCaptcha } from '@/lib/captcha'; +import { logAudit } from '@/lib/audit'; export async function GET(req, { params }) { try { @@ -43,6 +44,7 @@ export async function POST(req, { params }) { RETURNING *`, [(await params).id, user.id, content, reply_to_id || null] ); + await logAudit(req, 'create_reply', user.id); await pool.query('UPDATE posts SET updated_at = NOW() WHERE id = $1', [(await params).id]); return Response.json(r.rows[0]); } catch { diff --git a/app/api/posts/route.js b/app/api/posts/route.js index d673d58..6f75fee 100644 --- a/app/api/posts/route.js +++ b/app/api/posts/route.js @@ -3,6 +3,7 @@ import pool from '@/lib/db'; import { getUser } from '@/lib/auth'; import { jsonWithEtag } from '@/lib/http-cache'; import { verifyCaptcha } from '@/lib/captcha'; +import { logAudit } from '@/lib/audit'; // 避免 GET 被静态优化导致写方法 405(动态接口,不能缓存) export const dynamic = 'force-dynamic'; @@ -73,6 +74,7 @@ export async function POST(req) { RETURNING *`, [user.id, title || '无标题', content, images || []] ); + await logAudit(req, 'create_post', user.id); return Response.json(r.rows[0]); } catch { return Response.json({ error: '发布失败,请稍后重试' }, { status: 500 }); diff --git a/app/globals.css b/app/globals.css index ab13287..2f59c1f 100644 --- a/app/globals.css +++ b/app/globals.css @@ -3144,3 +3144,31 @@ a.btn-secondary { flex-wrap: wrap; } } + +/* ===== 自定义页面 —— 全屏沉浸式(对齐上游 Lite) ===== */ +.custom-page-container { + position: fixed !important; + top: 64px !important; + left: 0 !important; + width: 100vw !important; + height: calc(100vh - 64px) !important; + z-index: 40 !important; + overflow: hidden !important; + padding: 0 !important; + margin: 0 !important; + background: transparent !important; +} +.custom-page-container iframe { + display: block !important; + width: 100% !important; + height: 100% !important; + border: none !important; + border-radius: 0 !important; + background: transparent !important; +} +@media (max-width: 768px) { + .custom-page-container { + top: 56px !important; + height: calc(100vh - 56px) !important; + } +} diff --git a/components/AppProvider.js b/components/AppProvider.js index de02e3d..cd49ef3 100644 --- a/components/AppProvider.js +++ b/components/AppProvider.js @@ -181,8 +181,7 @@ export default function AppProvider({ children, cachedName = '' }) { try { const user = await API.getMe(); setCurrentUser(user); - API.logEvent('login').catch(() => {}); - } catch { + } catch { setToken(null); } } @@ -395,7 +394,6 @@ export default function AppProvider({ children, cachedName = '' }) { const result = await API.login(email, password); if (result.user) { setCurrentUser(result.user); - API.logEvent('login').catch(() => {}); } return result; }, []); @@ -405,7 +403,6 @@ export default function AppProvider({ children, cachedName = '' }) { const loginResult = await API.login(email, password); if (loginResult.user) { setCurrentUser(loginResult.user); - API.logEvent('register').catch(() => {}); } return result; }, []); diff --git a/components/CustomPage.js b/components/CustomPage.js index 48af9f0..89e86b7 100644 --- a/components/CustomPage.js +++ b/components/CustomPage.js @@ -49,7 +49,7 @@ export default function CustomPage({ pageName }) { `; return ( -
+