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
This commit is contained in:
1 parent
45ace3ba75
commit
b9d6ca5da8
11 files changed
+73
-24
No files matched your search
@@ -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: {
|
||||
|
||||
@@ -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 ? '你是第一个用户,已自动设为管理员!' : '注册成功',
|
||||
|
||||
@@ -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)——
|
||||
// 客户端上报端点已移除,防止伪造审计记录
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 });
|
||||
|
||||
Reference in new issue
Block a user