feat(webui): EULA/PRIVACY consent gate — backend

Server-side consent gate: GET /api/agreements (texts + content-hash version +
consentRequired) and POST /api/agreements/record-consent, persisted to
config/consent.json. Enforced in the auth middleware (CONSENT_ALLOWLIST,
ordered before the must-change-password gate) so the gate is real, not just a
frontend convention. The version is content-derived, so consent is stable
across app upgrades and re-prompts only when the agreement text changes. The
EULA/PRIVACY docs are bundled into dist/ (build fails if they are missing).
This commit is contained in:
DrSmoothl committed 2026-06-19 02:26:48 +08:00
1 parent f71ed2e4db
commit 8c7efc34a9
4 files changed
+382

No files matched your search

+70
View File
@@ -14,6 +14,7 @@ import os from 'os';
import path from 'path';
import { fileURLToPath } from 'url';
import { evaluatePasswordRules, isStrongPassword, WebuiAuth } from './auth';
import { getAgreementsPayload, isConsentRequired, loadAgreements, recordConsent } from './consent';
import { resolveTlsContext, validateTlsPair } from './tls';
import { coerceSettingsPatch } from './system-settings';
import { buildBackup, planRestore, validateBackup } from './backup';
@@ -58,6 +59,23 @@ const MUST_CHANGE_ALLOWLIST = new Set([
'/api/auth/state',
'/api/auth/check-strength',
'/api/auth/change-password',
// Consent is collected AFTER login but BEFORE the forced password change, so
// reading + recording it must be reachable while the session mustChangePassword.
'/api/agreements',
'/api/agreements/record-consent',
'/api/logout',
]);
// Endpoints reachable while consent is still pending. Everything else is 403'd
// with consentRequired:true until the operator accepts — the same server-side
// enforcement pattern as MUST_CHANGE_ALLOWLIST, so the consent gate is real and
// not merely a frontend convention. Ordered BEFORE the must-change gate so a
// fresh install must consent first, then set its password.
const CONSENT_ALLOWLIST = new Set([
'/api/status',
'/api/auth/state',
'/api/agreements',
'/api/agreements/record-consent',
'/api/logout',
]);
@@ -140,6 +158,12 @@ export async function initWebUI(
log.warn('SNOWLUMA_WEBUI_TRUST_PROXY=1 — only safe behind a reverse proxy that strips client-set X-Real-IP / X-Forwarded-For');
}
// Memoized once at startup (agreements version is fixed per process; a text
// change ships with a redeploy that restarts us). Flipped to false the moment
// consent is recorded, so the middleware never touches disk per request.
let consentGatePending = isConsentRequired();
if (consentGatePending) log.info('awaiting EULA/PRIVACY consent before the panel unlocks');
const app = new Hono();
// ─── Anti-indexing ───────────────────────────────────────────────────────
@@ -181,6 +205,17 @@ export async function initWebUI(
return c.json({ status: 'failed', message: 'Token expired or invalid' }, 401);
}
// Consent gate (before the password gate): block everything outside the
// consent allowlist until the operator has accepted the current agreements.
// `consentGatePending` is memoized (see below) so this is a Set lookup, not
// a disk read, on the hot path.
if (consentGatePending && !CONSENT_ALLOWLIST.has(reqPath)) {
return c.json(
{ status: 'failed', message: '请先阅读并同意用户协议与隐私政策', consentRequired: true },
403,
);
}
if (info.mustChangePassword && !MUST_CHANGE_ALLOWLIST.has(reqPath)) {
return c.json({ status: 'failed', message: '请先修改密码', mustChangePassword: true }, 403);
}
@@ -286,6 +321,41 @@ export async function initWebUI(
return c.json({ success: true, requireRelogin: true });
});
// ─── EULA / PRIVACY consent ──────────────────────────────────────────────
// Both are bearer-gated (consent is collected AFTER login) and live in the
// consent + must-change allowlists so they're reachable during onboarding.
// The version is content-derived, so consent is stable across app upgrades
// and re-prompted only when the agreement text itself changes.
app.get('/api/agreements', (c) => c.json(getAgreementsPayload()));
app.post('/api/agreements/record-consent', async (c) => {
let body: { version?: unknown };
try {
body = await c.req.json();
} catch {
return c.json({ success: false, message: '请求格式错误' }, 400);
}
const version = typeof body.version === 'string' ? body.version : '';
const current = loadAgreements().version;
// Reject a stale/blank version so a client that read an older agreement set
// can't record consent that the server would then treat as current.
if (!version || version !== current) {
return c.json(
{ success: false, message: '协议版本已更新,请刷新后重新确认', currentVersion: current },
409,
);
}
try {
recordConsent(version);
} catch (err) {
log.warn('record consent failed: %s', err instanceof Error ? err.message : String(err));
return c.json({ success: false, message: '保存失败,请检查服务器日志' }, 500);
}
consentGatePending = false; // unlock the rest of the API for this process
log.info('agreements consent recorded (version=%s)', version);
return c.json({ success: true, version });
});
// ─── Avatar proxy (uin validated) ────────────────────────────────────────
app.get('/avatar/:uin', async (c) => {
const uin = c.req.param('uin');