feat(webui): add trace warning and full export

Require explicit acknowledgement whenever operators enable TRACE and keep its active state visible until they restore normal logging.\n\nAdd an authenticated browser download for the complete retained logging snapshot, including runtime context and clear privacy guidance, while preserving the existing filtered-view export and memory-only TRACE boundary.\n\nCover the transition, snapshot, formatting, download, and client contracts with focused regression tests.
This commit is contained in:
DrSmoothl committed 2026-07-31 12:40:51 +08:00
1 parent a67614fa09
commit c3a1970e1c
12 files changed
+423 -11

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
import type { LogEntry } from '@snowluma/common/logger';
export interface FullTraceExportMetadata {
version: string;
operatingSystem: string;
architecture: string;
nodeVersion: string;
logLevel: LogEntry['level'];
exportedAt: string;
}
const PRIVACY_WARNING = 'WARNING: This export may contain unredacted private data and credentials. Sanitize it before submission.';
export interface FullTraceDownload {
body: string;
headers: {
'Content-Type': string;
'Content-Disposition': string;
'Cache-Control': string;
};
}
export function buildFullTraceDownload(
entries: readonly LogEntry[],
metadata: FullTraceExportMetadata,
): FullTraceDownload {
const timestamp = metadata.exportedAt
.slice(0, 19)
.replace(/[:.]/g, '-');
return {
body: formatFullTraceExport(entries, metadata),
headers: {
'Content-Type': 'text/plain; charset=utf-8',
'Content-Disposition': `attachment; filename="snowluma-trace-${timestamp}.log"`,
'Cache-Control': 'no-store',
},
};
}
export function formatFullTraceExport(
entries: readonly LogEntry[],
metadata: FullTraceExportMetadata,
): string {
const lines = [
'SnowLuma full TRACE export',
'==========================',
`SnowLuma version: ${metadata.version}`,
`Operating system: ${metadata.operatingSystem}`,
`Architecture: ${metadata.architecture}`,
`Node.js version: ${metadata.nodeVersion}`,
`Current log level: ${metadata.logLevel.toUpperCase()}`,
`Export time: ${metadata.exportedAt}`,
`Retained records: ${entries.length}`,
'',
PRIVACY_WARNING,
'This is a snapshot of the normal and TRACE records still retained in memory; records already evicted cannot be recovered.',
'',
'Logs',
'----',
];
if (entries.length === 0) {
lines.push('(No log records are currently retained.)');
} else {
lines.push(...entries.map((entry) => entry.line));
}
return `${lines.join('\n')}\n`;
}
+19 -1
View File
@@ -9,6 +9,7 @@ import {
import {
createLogger,
getLogLevel,
getLogSnapshot,
getRecentLogs,
logInitialWebuiCredentials,
LOG_LEVELS,
@@ -71,7 +72,8 @@ import {
sniffImageMime,
writeBackgroundImage,
} from './ui-config';
import { getUpdateInfo } from './update-check';
import { currentVersion, getUpdateInfo } from './update-check';
import { buildFullTraceDownload } from './log-export';
import { loadNotificationsConfig, saveNotificationsConfig } from '../notifications/config';
import type { NotificationManager } from '../notifications/manager';
import { StorageManagementService } from './storage-management';
@@ -1183,6 +1185,22 @@ export async function initWebUI(
return c.json({ list: getRecentLogs(limit) });
});
app.get('/api/logs/export/trace', (c) => {
const exportedAt = new Date().toISOString();
const download = buildFullTraceDownload(getLogSnapshot(), {
version: currentVersion(),
operatingSystem: os.platform(),
architecture: os.arch(),
nodeVersion: process.version,
logLevel: getLogLevel(),
exportedAt,
});
for (const [name, value] of Object.entries(download.headers)) {
c.header(name, value);
}
return c.body(download.body);
});
app.get('/api/logs/level', (c) => {
return c.json({ level: getLogLevel(), levels: [...LOG_LEVELS] });
});
+1 -1
View File
@@ -46,7 +46,7 @@ interface GithubRelease {
published_at?: string;
}
function currentVersion(): string {
export function currentVersion(): string {
return typeof __APP_VERSION__ === 'string' && __APP_VERSION__ ? __APP_VERSION__ : '0.0.0';
}