diff --git a/README.md b/README.md index e813ef89..6c526501 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ SnowLuma 是一个基于 TypeScript 的协议转换框架,旨在为 QQ 客户 2. 运行 `./launcher.bat` (Windows) 或 `./launcher.sh` (Linux)。 3. 访问 `http://localhost:5099` (初始账号 `admin`,密码见控制台输出)。 +无人值守部署可同时设置 `SNOWLUMA_ACCEPT_EULA=1` 与 `SNOWLUMA_ACCEPT_PRIVACY=1` 完成协议确认。两项必须同时设置;移除任意变量后会恢复按当前协议版本检查 WebUI 同意记录。设置这些变量即表示您已阅读并同意 [`EULA.md`](EULA.md) 与 [`PRIVACY.md`](PRIVACY.md)。 + ## 许可 / License SnowLuma 采用 **源码可见非商业许可(SnowLuma Source-Available Non-Commercial License)**,见 [`LICENSE`](LICENSE)。**这不是 OSI 开源许可**:源码公开供查看、学习与非商业自托管,但**禁止任何商业使用**(出售、转售、付费托管 / 代搭 / 代运营等),公开发布修改版须事先书面授权。随附的原生附加组件(`snowluma-*.node` 等)为专有组件,不在本许可范围内。二进制发行包另受 [`EULA.md`](EULA.md) 约束;商业授权请联系 motricseven@foxmail.com。参与贡献请阅读 [`CONTRIBUTING.md`](CONTRIBUTING.md)。 @@ -57,4 +59,3 @@ SnowLuma is under a **source-available, non-commercial** license (see [`LICENSE`

- diff --git a/packages/core/src/webui/consent.ts b/packages/core/src/webui/consent.ts index c8278a2f..4790e4a9 100644 --- a/packages/core/src/webui/consent.ts +++ b/packages/core/src/webui/consent.ts @@ -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>; + +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 diff --git a/packages/core/src/webui/server.ts b/packages/core/src/webui/server.ts index 0f33bb71..2162de2c 100644 --- a/packages/core/src/webui/server.ts +++ b/packages/core/src/webui/server.ts @@ -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(); diff --git a/packages/core/tests/webui/consent.test.ts b/packages/core/tests/webui/consent.test.ts index b8629f2d..4cf69fa9 100644 --- a/packages/core/tests/webui/consent.test.ts +++ b/packages/core/tests/webui/consent.test.ts @@ -4,9 +4,14 @@ import os from 'os'; import path from 'path'; import { computeAgreementsVersion, + EULA_ACCEPT_ENV, + isConsentRequired, + loadAgreements, parseAgreementMeta, + PRIVACY_ACCEPT_ENV, recordConsent, loadConsentRecord, + resolveEnvironmentConsent, } from '@/webui/consent'; describe('computeAgreementsVersion', () => { @@ -66,6 +71,35 @@ describe('parseAgreementMeta', () => { }); }); +describe('environment consent', () => { + it('requires explicit acceptance of both EULA and PRIVACY', () => { + expect(resolveEnvironmentConsent({})).toEqual({ + eulaAccepted: false, + privacyAccepted: false, + accepted: false, + }); + expect(resolveEnvironmentConsent({ [EULA_ACCEPT_ENV]: '1' })).toEqual({ + eulaAccepted: true, + privacyAccepted: false, + accepted: false, + }); + }); + + it('accepts trimmed, case-insensitive true values', () => { + expect(resolveEnvironmentConsent({ + [EULA_ACCEPT_ENV]: ' true ', + [PRIVACY_ACCEPT_ENV]: 'TRUE', + }).accepted).toBe(true); + }); + + it('fails fast on an invalid configured value', () => { + expect(() => resolveEnvironmentConsent({ + [EULA_ACCEPT_ENV]: 'yes', + [PRIVACY_ACCEPT_ENV]: '1', + })).toThrow(`${EULA_ACCEPT_ENV} must be one of: 1, true, 0, false`); + }); +}); + describe('consent persistence', () => { let tmp: string; let cwd: string; @@ -100,6 +134,20 @@ describe('consent persistence', () => { expect(stored?.version !== 'old-version').toBe(false); }); + it('environment consent unlocks without writing a permanent consent record', () => { + expect(isConsentRequired({ + [EULA_ACCEPT_ENV]: '1', + [PRIVACY_ACCEPT_ENV]: 'true', + })).toBe(false); + expect(loadConsentRecord()).toBeNull(); + expect(isConsentRequired({ [EULA_ACCEPT_ENV]: '1' })).toBe(true); + }); + + it('stored consent still covers the current agreement version', () => { + recordConsent(loadAgreements().version); + expect(isConsentRequired({})).toBe(false); + }); + it('ignores a malformed consent.json (treated as no consent)', () => { fs.mkdirSync('config', { recursive: true }); fs.writeFileSync(path.join('config', 'consent.json'), '{ not json');