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
+72
-23
No files matched your search
@@ -3,6 +3,7 @@ import bcrypt from 'bcryptjs';
|
|||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
import pool from '@/lib/db';
|
import pool from '@/lib/db';
|
||||||
import { JWT_SECRET } from '@/lib/auth';
|
import { JWT_SECRET } from '@/lib/auth';
|
||||||
|
import { logAudit } from '@/lib/audit';
|
||||||
|
|
||||||
export async function POST(req) {
|
export async function POST(req) {
|
||||||
const { email, password } = await req.json();
|
const { email, password } = await req.json();
|
||||||
@@ -24,6 +25,8 @@ export async function POST(req) {
|
|||||||
JWT_SECRET,
|
JWT_SECRET,
|
||||||
{ expiresIn: '7d' }
|
{ expiresIn: '7d' }
|
||||||
);
|
);
|
||||||
|
// 服务端审计:登录成功
|
||||||
|
await logAudit(req, 'login', user.id);
|
||||||
return Response.json({
|
return Response.json({
|
||||||
token,
|
token,
|
||||||
user: {
|
user: {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
import bcrypt from 'bcryptjs';
|
import bcrypt from 'bcryptjs';
|
||||||
import pool from '@/lib/db';
|
import pool from '@/lib/db';
|
||||||
import { verifyCaptcha } from '@/lib/captcha';
|
import { verifyCaptcha } from '@/lib/captcha';
|
||||||
|
import { logAudit } from '@/lib/audit';
|
||||||
|
|
||||||
export async function POST(req) {
|
export async function POST(req) {
|
||||||
const { email, password, username, captcha_id, captcha_answer, captcha_sig } = await req.json();
|
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]
|
[email, hash, username, avatar, role]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 服务端审计:注册成功
|
||||||
|
await logAudit(req, 'register', r.rows[0].id);
|
||||||
return Response.json({
|
return Response.json({
|
||||||
user: r.rows[0],
|
user: r.rows[0],
|
||||||
message: isFirstUser ? '你是第一个用户,已自动设为管理员!' : '注册成功',
|
message: isFirstUser ? '你是第一个用户,已自动设为管理员!' : '注册成功',
|
||||||
|
|||||||
@@ -27,20 +27,5 @@ export async function GET(req) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(req) {
|
// 审计日志由服务端在关键操作后记录(对齐上游 PR #22)——
|
||||||
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 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,6 +3,7 @@ import pool from '@/lib/db';
|
|||||||
import { getUser } from '@/lib/auth';
|
import { getUser } from '@/lib/auth';
|
||||||
import { jsonWithEtag } from '@/lib/http-cache';
|
import { jsonWithEtag } from '@/lib/http-cache';
|
||||||
import { verifyCaptcha } from '@/lib/captcha';
|
import { verifyCaptcha } from '@/lib/captcha';
|
||||||
|
import { logAudit } from '@/lib/audit';
|
||||||
|
|
||||||
export async function GET(req, { params }) {
|
export async function GET(req, { params }) {
|
||||||
try {
|
try {
|
||||||
@@ -43,6 +44,7 @@ export async function POST(req, { params }) {
|
|||||||
RETURNING *`,
|
RETURNING *`,
|
||||||
[(await params).id, user.id, content, reply_to_id || null]
|
[(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]);
|
await pool.query('UPDATE posts SET updated_at = NOW() WHERE id = $1', [(await params).id]);
|
||||||
return Response.json(r.rows[0]);
|
return Response.json(r.rows[0]);
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import pool from '@/lib/db';
|
|||||||
import { getUser } from '@/lib/auth';
|
import { getUser } from '@/lib/auth';
|
||||||
import { jsonWithEtag } from '@/lib/http-cache';
|
import { jsonWithEtag } from '@/lib/http-cache';
|
||||||
import { verifyCaptcha } from '@/lib/captcha';
|
import { verifyCaptcha } from '@/lib/captcha';
|
||||||
|
import { logAudit } from '@/lib/audit';
|
||||||
|
|
||||||
// 避免 GET 被静态优化导致写方法 405(动态接口,不能缓存)
|
// 避免 GET 被静态优化导致写方法 405(动态接口,不能缓存)
|
||||||
export const dynamic = 'force-dynamic';
|
export const dynamic = 'force-dynamic';
|
||||||
@@ -73,6 +74,7 @@ export async function POST(req) {
|
|||||||
RETURNING *`,
|
RETURNING *`,
|
||||||
[user.id, title || '无标题', content, images || []]
|
[user.id, title || '无标题', content, images || []]
|
||||||
);
|
);
|
||||||
|
await logAudit(req, 'create_post', user.id);
|
||||||
return Response.json(r.rows[0]);
|
return Response.json(r.rows[0]);
|
||||||
} catch {
|
} catch {
|
||||||
return Response.json({ error: '发布失败,请稍后重试' }, { status: 500 });
|
return Response.json({ error: '发布失败,请稍后重试' }, { status: 500 });
|
||||||
|
|||||||
@@ -3144,3 +3144,31 @@ a.btn-secondary {
|
|||||||
flex-wrap: wrap;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -181,7 +181,6 @@ export default function AppProvider({ children, cachedName = '' }) {
|
|||||||
try {
|
try {
|
||||||
const user = await API.getMe();
|
const user = await API.getMe();
|
||||||
setCurrentUser(user);
|
setCurrentUser(user);
|
||||||
API.logEvent('login').catch(() => {});
|
|
||||||
} catch {
|
} catch {
|
||||||
setToken(null);
|
setToken(null);
|
||||||
}
|
}
|
||||||
@@ -395,7 +394,6 @@ export default function AppProvider({ children, cachedName = '' }) {
|
|||||||
const result = await API.login(email, password);
|
const result = await API.login(email, password);
|
||||||
if (result.user) {
|
if (result.user) {
|
||||||
setCurrentUser(result.user);
|
setCurrentUser(result.user);
|
||||||
API.logEvent('login').catch(() => {});
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}, []);
|
}, []);
|
||||||
@@ -405,7 +403,6 @@ export default function AppProvider({ children, cachedName = '' }) {
|
|||||||
const loginResult = await API.login(email, password);
|
const loginResult = await API.login(email, password);
|
||||||
if (loginResult.user) {
|
if (loginResult.user) {
|
||||||
setCurrentUser(loginResult.user);
|
setCurrentUser(loginResult.user);
|
||||||
API.logEvent('register').catch(() => {});
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}, []);
|
}, []);
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ export default function CustomPage({ pageName }) {
|
|||||||
</html>`;
|
</html>`;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ padding: '84px 32px 40px', background: 'var(--bg)', minHeight: '100vh' }}>
|
<div className="custom-page-container" style={{ padding: '84px 32px 40px', background: 'var(--bg)', minHeight: '100vh' }}>
|
||||||
<iframe
|
<iframe
|
||||||
style={{ width: '100%', minHeight: '70vh', border: 'none', borderRadius: 8, background: 'var(--surface)' }}
|
style={{ width: '100%', minHeight: '70vh', border: 'none', borderRadius: 8, background: 'var(--surface)' }}
|
||||||
sandbox="allow-scripts allow-modals allow-same-origin"
|
sandbox="allow-scripts allow-modals allow-same-origin"
|
||||||
|
|||||||
@@ -71,7 +71,6 @@ export default function NewPost() {
|
|||||||
try {
|
try {
|
||||||
// 答案由服务端 HMAC 校验
|
// 答案由服务端 HMAC 校验
|
||||||
await API.createPost(title.trim() || '无标题', content.trim(), images, { id: captcha.id, answer: captchaInput.trim(), sig: captcha.sig });
|
await API.createPost(title.trim() || '无标题', content.trim(), images, { id: captcha.id, answer: captchaInput.trim(), sig: captcha.sig });
|
||||||
API.logEvent('create_post').catch(() => {});
|
|
||||||
toast('发布成功', 'success');
|
toast('发布成功', 'success');
|
||||||
navigate('feed');
|
navigate('feed');
|
||||||
refresh();
|
refresh();
|
||||||
|
|||||||
@@ -52,7 +52,6 @@ export default function ReplyList({ postId, onRefresh }) {
|
|||||||
try {
|
try {
|
||||||
// 答案由服务端 HMAC 校验
|
// 答案由服务端 HMAC 校验
|
||||||
await API.createReply(postId, content.trim(), { id: captcha.id, answer: captchaInput.trim(), sig: captcha.sig }, replyTo);
|
await API.createReply(postId, content.trim(), { id: captcha.id, answer: captchaInput.trim(), sig: captcha.sig }, replyTo);
|
||||||
API.logEvent('create_reply').catch(() => {});
|
|
||||||
setContent('');
|
setContent('');
|
||||||
setCaptchaInput('');
|
setCaptchaInput('');
|
||||||
setReplyTo(null);
|
setReplyTo(null);
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
// 服务端审计日志(对齐上游 PR #22:审计日志服务端拥有,不可伪造)
|
||||||
|
// 用法:await logAudit(req, action)
|
||||||
|
import pool from '@/lib/db';
|
||||||
|
|
||||||
|
// 从请求提取客户端 IP(支持 X-Forwarded-For / CF 头)
|
||||||
|
function clientIp(req) {
|
||||||
|
const fwd = req.headers.get('x-forwarded-for');
|
||||||
|
if (fwd) return fwd.split(',')[0].trim().slice(0, 45);
|
||||||
|
const cf = req.headers.get('cf-connecting-ip');
|
||||||
|
if (cf) return cf.slice(0, 45);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 记录一条审计日志(写入失败不影响主流程,静默忽略)
|
||||||
|
export async function logAudit(req, action, userId = null) {
|
||||||
|
try {
|
||||||
|
if (userId) {
|
||||||
|
await pool.query(
|
||||||
|
`INSERT INTO event_logs (user_id, action, ip) VALUES ($1, $2, $3)`,
|
||||||
|
[userId, String(action).slice(0, 50), clientIp(req)]
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
await pool.query(
|
||||||
|
`INSERT INTO event_logs (action, ip) VALUES ($1, $2)`,
|
||||||
|
[String(action).slice(0, 50), clientIp(req)]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// 审计失败不应阻断主流程
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user