/** * 操作审计 · 模拟数据生成器(种子随机,确定性输出) * * 记录字段与插件 spec 一致:username / attemptedUsername / method / action / * resourceType / resourceName / path / statusCode / remoteIp / userAgent / changes。 * 资源解析、语义动作、登录结果与字段 diff 全部经 AuditCore 按真实捕获管线推导, * 便于在演示页复现「谁改了什么」而不仅是「谁点了什么」。 * * 内置故事线(固定时间锚点,可直接按演示页提示复现): * 1. 排查「hello-halo 这篇文章谁改过」(多用户多次编辑,含字段 diff) * 2. 登录爆破回溯(同一 IP、无差别撞库,含失败回显页) * 3. 忘记密码后登录成功(失败 → 成功清零) * 4. 定时发布(系统内部变更,执行者「系统」、无 IP/UA) * 5. 插件生命周期(升级 / 停用 / 卸载,含字段 diff) * 6. 越权与异常(403 / 404 / 500) */ (function (root, factory) { if (typeof module === 'object' && module.exports) { module.exports = factory(require('./audit-core.js')); } else { root.MockData = factory(root.AuditCore); } })(typeof self !== 'undefined' ? self : this, function (AuditCore) { 'use strict'; var DAY = 86400000; var MIN = 60000; var BASE_COUNT = 116; // ---------- 种子随机 ---------- function mulberry32(seed) { var a = seed >>> 0; return function () { a |= 0; a = (a + 0x6D2B79F5) | 0; var t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } function rint(r, min, max) { return Math.floor(r() * (max - min + 1)) + min; } function pick(r, arr) { return arr[rint(r, 0, arr.length - 1)]; } function hex(r, n) { var s = ''; var chars = '0123456789abcdef'; for (var i = 0; i < n; i++) { s += chars[rint(r, 0, 15)]; } return s; } /** 演示用的一次性假口令(运行时生成,非真实凭据;仅用于验证敏感字段脱敏)。 */ function demoSecret(r) { return 'demo-only-' + hex(r, 12); } // ---------- 基础素材 ---------- var UAS = { chrome: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.2151.97 Safari/537.36', edge: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.2151.97 Safari/537.36 Edg/119.0.2151.97', firefox: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0', safari: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15', bot: 'python-requests/2.31.0' }; var USERS = { guichumo: { ips: ['192.168.1.23', '116.24.65.187'], uas: ['chrome', 'edge'] }, 'editor.zhang': { ips: ['116.24.65.201'], uas: ['edge', 'firefox'] }, 'writer.li': { ips: ['120.79.10.45'], uas: ['safari', 'chrome'] } }; var POSTS = [ { name: 'hello-halo', title: '你好,Halo', summary: '从零开始搭建自己的写作空间。' }, { name: 'self-host-guide', title: '自托管指南', summary: '用 Docker 部署 Halo 的完整步骤。' }, { name: 'reading-notes-11', title: '读书笔记 #11', summary: '本周读完的三本书。' }, { name: 'plugin-dev-diary', title: '插件开发日志', summary: '从脚手架到上架。' }, { name: 'weekly-2026-w46', title: '每周摘要 · 2026 W46', summary: '站点一周动态。' }, { name: 'spring-festival-2026', title: '春节计划', summary: '假期更新安排。' } ]; var PLUGINS = [ { name: 'plugin-search-widget', displayName: '搜索组件', version: { old: '1.2.0', next: '1.3.0' } }, { name: 'plugin-comment-widget', displayName: '评论组件', version: { old: '1.5.0', next: '1.6.0' } }, { name: 'plugin-sitemap', displayName: 'Sitemap', version: { old: '1.0.4', next: '1.0.5' } }, { name: 'plugin-links', displayName: '友情链接', version: { old: '2.1.1', next: '2.2.0' } } ]; // ---------- 文档快照(diff 用,结构贴近 Halo 扩展模型)---------- function postDoc(entry, patch) { var spec = { title: entry.title, slug: entry.name, visible: 'PUBLIC', publish: true, pinned: false, allowComment: true, summary: entry.summary }; Object.keys(patch || {}).forEach(function (key) { spec[key] = patch[key]; }); return { apiVersion: 'content.halo.run/v1alpha1', kind: 'Post', metadata: { name: entry.name, labels: { 'content.halo.run/pinned': 'false' }, annotations: { 'content.halo.run/permalink-pattern': '/archives/:slug' }, version: 128, creationTimestamp: '2026-10-01T08:00:00Z' }, spec: spec, status: { phase: 'PUBLISHED', permalink: '/archives/' + entry.name } }; } function commentDoc(name, approved) { return { apiVersion: 'content.halo.run/v1alpha1', kind: 'Comment', metadata: { name: name, version: 7 }, spec: { raw: '写得很好,收藏了。', owner: { displayName: '访客' + name.slice(-4), kind: 'Email' }, approved: approved, hidden: false, allowNotification: true } }; } function pluginDoc(plugin, enabled, version) { return { apiVersion: 'plugin.halo.run/v1alpha1', kind: 'Plugin', metadata: { name: plugin.name, labels: { 'plugin.halo.run/plugin-name': plugin.name } }, spec: { displayName: plugin.displayName, version: version, enabled: enabled, author: { name: 'halo-sigs' }, requires: '>=2.20.0' }, status: { phase: enabled ? 'STARTED' : 'DISABLED' } }; } // ---------- 记录组装 ---------- function recCtx(r, username) { var u = USERS[username] || USERS.guichumo; return { username: username, remoteIp: pick(r, u.ips), userAgent: UAS[pick(r, u.uas)], authenticated: true }; } function make(overrides) { return AuditCore.buildRecord(Object.assign({ statusCode: 200, authenticated: true, recordChanges: true }, overrides)); } function postPath(name) { return '/apis/content.halo.run/v1alpha1/posts/' + name; } function postsPath() { return '/apis/content.halo.run/v1alpha1/posts'; } function commentPath(name) { return '/apis/content.halo.run/v1alpha1/comments/' + name; } function pluginPath(name) { return '/apis/plugin.halo.run/v1alpha1/plugins/' + name; } /** * 基础操作集:[权重, 构造函数(r) → 记录输入] * 每次构造经过 buildRecord:资源解析、语义动作、登录结果、字段 diff 与插件一致。 */ var ACTIONS = [ [16, function (r) { var entry = pick(r, POSTS); var ctx = recCtx(r, pick(r, ['guichumo', 'editor.zhang', 'writer.li'])); return Object.assign(ctx, { method: 'PUT', path: postPath(entry.name), previous: postDoc(entry), body: postDoc(entry, { title: entry.title + '(修订)', summary: entry.summary + ' 已补充示例。' }) }); }], [9, function (r) { var entry = pick(r, POSTS); var ctx = recCtx(r, pick(r, ['guichumo', 'editor.zhang'])); return Object.assign(ctx, { method: 'POST', path: postPath(entry.name) + '/publish' }); }], [7, function (r) { var ctx = recCtx(r, 'guichumo'); var slug = 'draft-' + hex(r, 5); return Object.assign(ctx, { method: 'POST', path: postsPath(), statusCode: 201, body: { apiVersion: 'content.halo.run/v1alpha1', kind: 'Post', metadata: { name: slug }, spec: { title: '草稿 · ' + slug, slug: slug, visible: 'PUBLIC', publish: false } } }); }], [6, function (r) { var ctx = recCtx(r, 'guichumo'); return Object.assign(ctx, { method: 'PUT', path: '/apis/api.console.halo.run/v1alpha1/configmaps/system', previous: { apiVersion: 'v1alpha1', kind: 'ConfigMap', metadata: { name: 'system', version: 9 }, data: { theme: 'theme-earth', locale: 'zh-CN' } }, body: { apiVersion: 'v1alpha1', kind: 'ConfigMap', metadata: { name: 'system', version: 10 }, data: { theme: 'theme-earth', locale: 'zh-CN', 'site.title': '青竹小站' } } }); }], [5, function (r) { var plugin = pick(r, PLUGINS); var ctx = recCtx(r, 'guichumo'); return Object.assign(ctx, { method: 'PUT', path: pluginPath(plugin.name) + '/activation', previous: pluginDoc(plugin, false, plugin.version.old), body: pluginDoc(plugin, true, plugin.version.old) }); }], [5, function (r) { var ctx = recCtx(r, pick(r, ['editor.zhang', 'writer.li'])); var name = 'attachment-' + hex(r, 6); return Object.assign(ctx, { method: 'POST', path: '/apis/storage.halo.run/v1alpha1/attachments', statusCode: 201, body: { apiVersion: 'storage.halo.run/v1alpha1', kind: 'Attachment', metadata: { name: name }, spec: { displayName: 'cover-' + hex(r, 4) + '.png', mediaType: 'image/png', size: rint(r, 80, 640) * 1024 } } }); }], [3, function (r) { var ctx = recCtx(r, 'editor.zhang'); var name = 'attachment-' + hex(r, 6); return Object.assign(ctx, { method: 'DELETE', path: '/apis/storage.halo.run/v1alpha1/attachments/' + name, statusCode: 204, previous: { apiVersion: 'storage.halo.run/v1alpha1', kind: 'Attachment', metadata: { name: name }, spec: { displayName: 'old-cover.png', mediaType: 'image/png', size: 245760 } } }); }], [4, function (r) { var ctx = recCtx(r, 'writer.li'); return Object.assign(ctx, { method: 'POST', path: '/apis/api.halo.run/v1alpha1/comments/comment-' + hex(r, 6) + '/reply', body: { apiVersion: 'content.halo.run/v1alpha1', kind: 'Reply', metadata: { name: 'reply-' + hex(r, 4) }, spec: { raw: '感谢支持,已更新示例。', quoteReply: 'comment-' + hex(r, 4) } } }); }], [3, function (r) { var ctx = recCtx(r, 'editor.zhang'); var name = 'comment-' + hex(r, 6); return Object.assign(ctx, { method: 'DELETE', path: commentPath(name), statusCode: 204, previous: commentDoc(name, true) }); }], [3, function (r) { var ctx = recCtx(r, 'guichumo'); return Object.assign(ctx, { method: 'PUT', path: '/apis/api.console.halo.run/v1alpha1/roles/role-template-editor', previous: { apiVersion: 'v1alpha1', kind: 'Role', metadata: { name: 'role-template-editor', labels: { 'rbac.authorization.halo.run/display-name': '编辑' } }, rules: [{ apiGroups: ['content.halo.run'], resources: ['posts'], verbs: ['get', 'list', 'create', 'update'] }] }, body: { apiVersion: 'v1alpha1', kind: 'Role', metadata: { name: 'role-template-editor', labels: { 'rbac.authorization.halo.run/display-name': '编辑' } }, rules: [{ apiGroups: ['content.halo.run'], resources: ['posts'], verbs: ['get', 'list', 'create', 'update', 'delete'] }] } }); }], [2, function (r) { var ctx = recCtx(r, 'guichumo'); return Object.assign(ctx, { method: 'POST', path: '/apis/api.console.halo.run/v1alpha1/users', statusCode: 201, body: { apiVersion: 'v1alpha1', kind: 'User', metadata: { name: 'alice-' + hex(r, 4) }, spec: { displayName: 'Alice', email: 'alice-' + hex(r, 3) + '@example.com', // 运行时生成的假口令:用于验证 spec.password 在 diff 中被脱敏 password: demoSecret(r), roles: ['role-template-author'] } } }); }], [2, function (r) { var ctx = recCtx(r, 'guichumo'); return Object.assign(ctx, { method: 'PUT', path: '/apis/api.console.halo.run/v1alpha1/users/guichumo/password', previous: { apiVersion: 'v1alpha1', kind: 'User', metadata: { name: 'guichumo' }, spec: { password: demoSecret(r) } }, body: { apiVersion: 'v1alpha1', kind: 'User', metadata: { name: 'guichumo' }, spec: { password: demoSecret(r) } } }); }], [2, function (r) { var ctx = recCtx(r, 'writer.li'); return Object.assign(ctx, { method: 'POST', path: '/apis/content.halo.run/v1alpha1/comments/comment-' + hex(r, 6) + '/approve' }); }], [2, function (r) { var ctx = recCtx(r, 'editor.zhang'); return Object.assign(ctx, { method: 'POST', path: '/apis/api.console.halo.run/v1alpha1/categories', statusCode: 201, body: { apiVersion: 'content.halo.run/v1alpha1', kind: 'Category', metadata: { name: 'category-' + hex(r, 4) }, spec: { displayName: '技术随笔', slug: 'tech-notes', priority: 10 } } }); }], [2, function (r) { var ctx = recCtx(r, 'editor.zhang'); return Object.assign(ctx, { method: 'PUT', path: '/apis/theme.halo.run/v1alpha1/themes/theme-earth/activation' }); }], [1, function (r) { var ctx = recCtx(r, 'guichumo'); return Object.assign(ctx, { method: 'PUT', path: '/apis/api.halo.run/v1alpha1/settings/basic' }); }], [1, function (r) { var ctx = recCtx(r, 'guichumo'); return Object.assign(ctx, { method: 'POST', path: '/apis/api.halo.run/v1alpha1/tags', statusCode: 201, body: { apiVersion: 'content.halo.run/v1alpha1', kind: 'Tag', metadata: { name: 'tag-' + hex(r, 4) }, spec: { displayName: 'Halo', slug: 'halo' } } }); }], [1, function (r) { var ctx = recCtx(r, 'guichumo'); return Object.assign(ctx, { method: 'PUT', path: '/apis/api.halo.run/v1alpha1/menus/menu-primary', previous: { apiVersion: 'v1alpha1', kind: 'Menu', metadata: { name: 'menu-primary' }, spec: { displayName: '主菜单', menuItems: ['/archives', '/categories'] } }, body: { apiVersion: 'v1alpha1', kind: 'Menu', metadata: { name: 'menu-primary' }, spec: { displayName: '主菜单', menuItems: ['/archives', '/categories', '/about'] } } }); }] ]; var TOTAL_WEIGHT = ACTIONS.reduce(function (sum, item) { return sum + item[0]; }, 0); /** * 生成模拟审计日志(按时间倒序,id 从旧到新递增) * @param {number} [now] 当前时间戳(默认 Date.now()),测试传固定值保证确定性 * @returns {Array} */ function generateLogs(now) { if (typeof now !== 'number') { now = Date.now(); } var r = mulberry32(20261122); var logs = []; function add(record) { logs.push(record); return record; } // ---- 基础记录:均匀散布在近 90 天 ---- for (var i = 0; i < BASE_COUNT; i++) { var roll = r() * TOTAL_WEIGHT; var action = ACTIONS[0][1]; for (var j = 0; j < ACTIONS.length; j++) { roll -= ACTIONS[j][0]; if (roll <= 0) { action = ACTIONS[j][1]; break; } } var input = action(r); input.timestamp = new Date(now - rint(r, 5 * MIN, 90 * DAY)).toISOString(); add(make(input)); } // ---- 故事线 1:排查「hello-halo 这篇文章谁改过」 ---- var hello = POSTS[0]; add(make({ username: 'editor.zhang', remoteIp: '116.24.65.201', userAgent: UAS.edge, method: 'PUT', path: postPath(hello.name), timestamp: new Date(now - 6 * DAY - 3 * 3600 * 1000).toISOString(), previous: postDoc(hello), body: postDoc(hello, { title: '你好,Halo(补充开箱截图)', summary: '从零开始搭建自己的写作空间,附开箱截图。' }) })); add(make({ username: 'guichumo', remoteIp: '192.168.1.23', userAgent: UAS.chrome, method: 'PUT', path: postPath(hello.name), timestamp: new Date(now - 2 * DAY - 5 * 3600 * 1000).toISOString(), previous: postDoc(hello, { title: '你好,Halo(补充开箱截图)' }), body: postDoc(hello, { title: '你好,Halo', summary: '从零开始搭建自己的写作空间。', visible: 'PRIVATE' }) })); add(make({ username: 'guichumo', remoteIp: '192.168.1.23', userAgent: UAS.chrome, method: 'PUT', path: postPath(hello.name), timestamp: new Date(now - 2 * DAY - 2 * 3600 * 1000).toISOString(), previous: postDoc(hello, { visible: 'PRIVATE' }), body: postDoc(hello, { visible: 'PUBLIC', pinned: true }) })); // ---- 故事线 2:登录爆破回溯(同一 IP 无差别撞库 + 失败回显) ---- var burstBase = now - 20 * DAY - 3 * 3600 * 1000; var attackAccounts = ['admin', 'root', 'oracle', 'test', 'admin', 'postgres']; attackAccounts.forEach(function (account, idx) { add(make({ method: 'POST', path: '/login', statusCode: 302, location: '/login?error=invalid-credential&method=form', attemptedUsername: account, remoteIp: '203.0.113.66', userAgent: UAS.bot, timestamp: new Date(burstBase + [0, 41, 88, 135, 210, 400][idx] * 1000).toISOString() })); }); // 浏览器随后渲染失败回显页(GET,HTTP 200):审计标注 login-failure,红色「失败」 add(make({ method: 'GET', path: '/login?error=invalid-credential&method=form', statusCode: 200, remoteIp: '203.0.113.66', userAgent: UAS.bot, timestamp: new Date(burstBase + 401 * 1000).toISOString() })); // ---- 故事线 3:忘记密码(失败两次后成功) ---- var retryBase = now - 12 * DAY - 10 * 3600 * 1000; [ { at: 0, account: 'guichumo', ok: false }, { at: 97, account: 'guichumo', ok: false }, { at: 180, account: 'guichumo', ok: true } ].forEach(function (step) { add(make({ username: 'guichumo', method: 'POST', path: '/login', statusCode: 302, location: step.ok ? '/uc' : '/login?error=bad-credentials&method=form', attemptedUsername: step.account, remoteIp: '192.168.1.23', userAgent: UAS.chrome, timestamp: new Date(retryBase + step.at * 1000).toISOString() })); }); add(make({ username: 'guichumo', method: 'POST', path: '/login', statusCode: 302, location: '/challenges/two-factor/totp', attemptedUsername: 'guichumo', remoteIp: '192.168.1.23', userAgent: UAS.chrome, timestamp: new Date(now - 9 * DAY - 8 * 3600 * 1000).toISOString() })); add(make({ username: 'guichumo', method: 'POST', path: '/logout', statusCode: 302, location: '/login?logout', remoteIp: '192.168.1.23', userAgent: UAS.chrome, timestamp: new Date(now - 4 * DAY - 11 * 3600 * 1000).toISOString() })); // ---- 故事线 4:定时发布(系统内部变更:无 IP/UA,执行者「系统」) ---- add(AuditCore.buildSystemRecord({ id: 'auditlog-system-0001', timestamp: new Date(now - DAY - 5 * 3600 * 1000).toISOString(), method: 'PUT', resourceType: 'posts', resourceName: 'weekly-2026-w46', path: '/apis/content.halo.run/v1alpha1/posts/weekly-2026-w46', changes: [ { field: 'spec.publish', changeType: 'UPDATED', oldValue: 'false', newValue: 'true' }, { field: 'spec.publishTime', changeType: 'ADDED', oldValue: null, newValue: '2026-11-21T02:00:00Z' } ] })); add(AuditCore.buildSystemRecord({ id: 'auditlog-system-0002', timestamp: new Date(now - 3 * DAY - 4 * 3600 * 1000).toISOString(), method: 'DELETE', resourceType: 'posts', resourceName: 'draft-night-notes', path: '/apis/content.halo.run/v1alpha1/posts/draft-night-notes', changes: [ { field: 'spec.title', changeType: 'REMOVED', oldValue: '夜间随笔(未发布)', newValue: null }, { field: 'metadata.name', changeType: 'REMOVED', oldValue: 'draft-night-notes', newValue: null } ] })); // ---- 故事线 5:插件生命周期(升级 / 停用 / 卸载) ---- var search = PLUGINS[0]; add(make({ username: 'guichumo', remoteIp: '192.168.1.23', userAgent: UAS.chrome, method: 'PUT', path: pluginPath(search.name) + '/upgrade', previous: pluginDoc(search, true, search.version.old), body: pluginDoc(search, true, search.version.next), timestamp: new Date(now - 7 * DAY - 6 * 3600 * 1000).toISOString() })); add(make({ username: 'guichumo', remoteIp: '192.168.1.23', userAgent: UAS.chrome, method: 'PUT', path: pluginPath(search.name) + '/activation', previous: pluginDoc(search, true, search.version.next), body: pluginDoc(search, false, search.version.next), timestamp: new Date(now - 6 * DAY - 9 * 3600 * 1000).toISOString() })); var links = PLUGINS[3]; add(make({ username: 'guichumo', remoteIp: '116.24.65.187', userAgent: UAS.edge, method: 'DELETE', path: pluginPath(links.name) + '/uninstall', statusCode: 204, previous: pluginDoc(links, true, links.version.next), timestamp: new Date(now - 1 * DAY - 7 * 3600 * 1000).toISOString() })); // ---- 故事线 6:越权与异常 ---- add(make({ username: 'editor.zhang', remoteIp: '116.24.65.201', userAgent: UAS.edge, method: 'PUT', path: '/apis/api.console.halo.run/v1alpha1/configmaps/system', statusCode: 403, timestamp: new Date(now - 8 * DAY - 14 * 3600 * 1000).toISOString() })); add(make({ username: 'writer.li', remoteIp: '120.79.10.45', userAgent: UAS.safari, method: 'DELETE', path: commentPath('comment-a3f802'), statusCode: 403, timestamp: new Date(now - 5 * DAY - 9 * 3600 * 1000).toISOString() })); add(make({ username: 'guichumo', remoteIp: '192.168.1.23', userAgent: UAS.chrome, method: 'DELETE', path: '/apis/storage.halo.run/v1alpha1/attachments/attachment-dead42', statusCode: 404, timestamp: new Date(now - 3 * DAY - 20 * 3600 * 1000).toISOString() })); add(make({ username: 'writer.li', remoteIp: '120.79.10.45', userAgent: UAS.chrome, method: 'POST', path: '/apis/api.content.halo.run/v1alpha1/posts/self-host-guide/publish', statusCode: 500, timestamp: new Date(now - 2 * DAY - 6 * 3600 * 1000).toISOString() })); // ---- 故事线 7:误操作定位(删除文章,含字段 diff) ---- var deleted = { name: 'weekly-2026-w45', title: '每周摘要 · 2026 W45', summary: '站点一周动态。' }; add(make({ username: 'guichumo', remoteIp: '192.168.1.23', userAgent: UAS.chrome, method: 'DELETE', path: postPath(deleted.name), statusCode: 204, previous: postDoc(deleted), timestamp: new Date(now - DAY - 2 * 3600 * 1000).toISOString() })); // ---- 排序 + 分配记录标识(最新记录编号最大) ---- logs.sort(function (a, b) { return new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime(); }); logs.forEach(function (log, idx) { if (!log.id || log.id.indexOf('auditlog-system-') !== 0) { log.id = 'auditlog-' + String(logs.length - idx).padStart(4, '0'); } }); return logs; } return { generateLogs: generateLogs }; });