feat(webui): support environment agreement consent

Allow unattended deployments to accept the EULA and Privacy Notice through SNOWLUMA_ACCEPT_EULA and SNOWLUMA_ACCEPT_PRIVACY.

Require both values, reject invalid configuration, keep environment consent non-persistent, and log whether acceptance is complete or partial.

Document the deployment contract and add regression coverage for parsing, persistence isolation, and stored-consent compatibility.
This commit is contained in:
DrSmoothl committed 2026-07-13 23:15:10 +08:00
1 parent defe15694b
commit 74939da3d7
4 files changed
+126 -10

No files matched your search

+50 -7
View File
@@ -18,6 +18,18 @@ const AGREEMENT_FILES = [
{ id: 'privacy' as const, file: 'PRIVACY.md' },
];
export const EULA_ACCEPT_ENV = 'SNOWLUMA_ACCEPT_EULA';
export const PRIVACY_ACCEPT_ENV = 'SNOWLUMA_ACCEPT_PRIVACY';
type ConsentEnvironment = Readonly<Record<string, string | undefined>>;
export interface EnvironmentConsent {
eulaAccepted: boolean;
privacyAccepted: boolean;
/** Both documents must be accepted; a partial opt-in never unlocks the gate. */
accepted: boolean;
}
export interface AgreementDoc {
id: 'eula' | 'privacy';
/** First markdown `# ` heading, for the tab/section title. */
@@ -41,15 +53,44 @@ export interface AgreementsPayload {
/** Content-hash version of the current agreement set. */
version: string;
/**
* True when the operator must (re-)accept: the agreements carry real text
* AND the stored consent version differs from the current one. A missing or
* unreadable agreement set fails OPEN (no gate) so a broken bundle can never
* lock the admin out of their own panel.
* True when the operator must (re-)accept: the agreements carry real text,
* environment consent is incomplete, AND the stored consent version differs
* from the current one. A missing or unreadable agreement set fails OPEN (no
* gate) so a broken bundle can never lock the admin out of their own panel.
*/
consentRequired: boolean;
documents: AgreementDoc[];
}
function parseEnvironmentAcceptance(name: string, raw: string | undefined): boolean {
if (raw === undefined || raw.trim() === '') return false;
const normalized = raw.trim().toLowerCase();
if (normalized === '1' || normalized === 'true') return true;
if (normalized === '0' || normalized === 'false') return false;
throw new Error(
`${name} must be one of: 1, true, 0, false; received ${JSON.stringify(raw)}`,
);
}
/**
* Resolve declarative consent for unattended deployments. Environment consent
* is intentionally ephemeral: removing either variable restores the normal
* versioned consent gate instead of silently writing a permanent record.
*/
export function resolveEnvironmentConsent(
env: ConsentEnvironment = process.env,
): EnvironmentConsent {
const eulaAccepted = parseEnvironmentAcceptance(EULA_ACCEPT_ENV, env[EULA_ACCEPT_ENV]);
const privacyAccepted = parseEnvironmentAcceptance(PRIVACY_ACCEPT_ENV, env[PRIVACY_ACCEPT_ENV]);
return {
eulaAccepted,
privacyAccepted,
accepted: eulaAccepted && privacyAccepted,
};
}
// ── Agreement loading (pure-ish; file read + parse, cached) ─────────────────
/**
@@ -85,7 +126,7 @@ export function parseAgreementMeta(text: string): {
const title = text.match(/^#\s+(.+?)\s*$/m)?.[1]?.trim() ?? '';
// Matches both "Version: 1.0" / "版本 / Version:** 1.0" style lines.
const declaredVersion =
text.match(/(?:Version|版本)[^\n0-9]*([0-9][0-9A-Za-z.\-]*)/)?.[1]?.trim() ?? '';
text.match(/(?:Version|版本)[^\n0-9]*([0-9][0-9A-Za-z.-]*)/)?.[1]?.trim() ?? '';
const effectiveDate =
text.match(/(?:Effective date|生效日期)[^\n0-9]*(\d{4}-\d{2}-\d{2})/)?.[1] ?? '';
return { title, declaredVersion, effectiveDate };
@@ -169,8 +210,10 @@ export function recordConsent(version: string): ConsentRecord {
return record;
}
/** True when the agreements carry real text and the stored consent is stale. */
export function isConsentRequired(): boolean {
/** True when neither environment nor stored consent covers the current agreements. */
export function isConsentRequired(env: ConsentEnvironment = process.env): boolean {
if (resolveEnvironmentConsent(env).accepted) return false;
const { docs, version } = loadAgreements();
const hasContent = docs.some((d) => d.text.trim().length > 0);
if (!hasContent) return false; // fail open: never brick the panel on a broken bundle
+26 -2
View File
@@ -20,7 +20,15 @@ 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 {
EULA_ACCEPT_ENV,
getAgreementsPayload,
isConsentRequired,
loadAgreements,
PRIVACY_ACCEPT_ENV,
recordConsent,
resolveEnvironmentConsent,
} from './consent';
import { resolveTlsContext, validateTlsPair } from './tls';
import { coerceSettingsPatch } from './system-settings';
import { buildBackup, planRestore, validateBackup } from './backup';
@@ -419,8 +427,24 @@ export async function initWebUI(
// 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.
const environmentConsent = resolveEnvironmentConsent();
let consentGatePending = isConsentRequired();
if (consentGatePending) log.info('awaiting EULA/PRIVACY consent before the panel unlocks');
if (environmentConsent.accepted) {
log.info(
'EULA/PRIVACY consent supplied by %s and %s (agreements version=%s)',
EULA_ACCEPT_ENV,
PRIVACY_ACCEPT_ENV,
loadAgreements().version,
);
} else if (environmentConsent.eulaAccepted || environmentConsent.privacyAccepted) {
log.warn(
'partial environment consent ignored; set both %s=1 and %s=1 to accept the agreements',
EULA_ACCEPT_ENV,
PRIVACY_ACCEPT_ENV,
);
} else if (consentGatePending) {
log.info('awaiting EULA/PRIVACY consent before the panel unlocks');
}
const app = new Hono();