fix(security): 修复看板 XSS 注入、归档覆盖及打包外泄风险

- 增加 HTML 实体转义并过滤危险伪协议,重构为事件委托机制阻断 XSS
- 归档前增加目标文件冲突检测,防止静默覆盖历史笔记与哈希记录
- 新增 --metadata-only 隐私脱敏模式,并对已有项目 index.html 提供覆写保护

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
czm
2026-09-08 16:39:30 +08:00
co-authored by Claude
parent 5c68ff670e
commit 89d074e275
4 changed files with 211 additions and 94 deletions
+6
View File
@@ -54,6 +54,12 @@ const archivedDir = join(agentNoteRoot, "archived", cls);
mkdirSync(archivedDir, { recursive: true });
const archivedPath = join(archivedDir, filename);
if (existsSync(archivedPath)) {
console.error(`Error: target archived note already exists at ${archivedPath}`);
console.error("Refusing to overwrite existing archived note. Please inspect and resolve name collision manually.");
process.exit(1);
}
writeFileSync(targetPath, updatedContent, "utf8");
renameSync(targetPath, archivedPath);
console.log(`Moved: ${relToRoot} -> archived/${cls}/${filename}`);
+37 -4
View File
@@ -15,14 +15,32 @@ if (!existsSync(templatePath)) {
process.exit(1);
}
// 检查是否为 --init 模式(日常开发:生成 69KB 轻量模板,不内联数据,直连本地目录)
// 检查参数
const isInitMode = args.includes('--init');
const cleanArgs = args.filter((a: string) => a !== '--init' && a !== '--bundle');
const isForce = args.includes('--force') || args.includes('-f');
const isMetadataOnly = args.includes('--metadata-only');
const cleanArgs = args.filter((a: string) => !a.startsWith('--') && !a.startsWith('-'));
function checkTargetSafety(targetPath: string) {
if (existsSync(targetPath) && !isForce) {
try {
const existing = readFileSync(targetPath, 'utf8');
const isBoard = existing.includes('generator" content="agent-notes-board"') || existing.includes('id="brand-project-title"');
if (!isBoard) {
console.error(`❌ 错误:目标文件已存在 (${targetPath}) 且并非看板生成文件!`);
console.error(`💡 为防止意外覆盖已有项目页面(如 Vite/React/Vue 项目的 index.html),请指定其他输出路径或传入 --force 确认覆盖。`);
process.exit(1);
}
} catch {}
}
}
if (isInitMode) {
const targetPath = cleanArgs[0] ? resolve(cleanArgs[0]) : resolve(process.cwd(), 'index.html');
const projectName = cleanArgs[1] || '工程决策看板';
checkTargetSafety(targetPath);
let template = readFileSync(templatePath, 'utf8');
template = template.replace('id="brand-project-title">工程决策看板<', () => `id="brand-project-title">${projectName}<`);
@@ -37,6 +55,8 @@ const notesDir = cleanArgs[0] ? resolve(cleanArgs[0]) : resolve(process.cwd(), '
const outputPath = cleanArgs[1] ? resolve(cleanArgs[1]) : resolve(process.cwd(), 'demo.html');
const projectName = cleanArgs[2] || '工程决策看板';
checkTargetSafety(outputPath);
const LIFECYCLES = ['implemented', 'proposed', 'rejected', 'archived'];
if (!existsSync(notesDir)) {
@@ -168,6 +188,19 @@ console.log(`🔍 [打包模式] 正在扫描笔记目录: ${notesDir}`);
const notes = walk(notesDir);
console.log(`✅ 解析完成,共发现 ${notes.length} 篇有效 Agent Notes。`);
if (isMetadataOnly) {
console.log('🔒 [安全脱敏] 启用 --metadata-only 模式,已剥离所有笔记的具体正文和详细论证,仅保留决策元数据与关联拓扑。');
for (const n of notes) {
n.problem = '[正文已脱敏]';
n.decision = '[正文已脱敏]';
n.alternatives = '';
n.consequences = '';
n.rawBody = `# Agent Note: ${n.title}\n\nStatus: ${n.status}\n\n<!-- content redacted for public demo -->`;
}
} else {
console.log('⚠️ [安全提示] 正在生成包含完整正文的数据包。如需对外公开发布且避免泄露内部决策细节,请添加 --metadata-only 参数。');
}
let template = readFileSync(templatePath, 'utf8');
// 注入项目名
@@ -181,5 +214,5 @@ template = template.replace(
);
writeFileSync(outputPath, template, 'utf8');
console.log(`🎉 [打包完成] 自包含大单体看板已生成: ${outputPath}`);
console.log(`💡 该文件内置了全部 ${notes.length} 篇笔记数据,可脱机分发、离线演示或部署至 GitHub Pages。`);
console.log(`🎉 [打包完成] 看板已生成: ${outputPath}`);
console.log(`💡 该文件内置了 ${notes.length} 篇笔记数据,可脱机分发、离线演示或部署至 GitHub Pages。`);