feat(demo): 按插件当前实现重写演示页
- 列表/筛选对齐真实 Console:八维筛选、语义动作中文描述、登录失败结果语义 - 详情抽屉展示字段级 diff(脱敏/截断/封顶)与 AuditLog Extension 原始 JSON - 新增行内「历史」快捷、按筛选条件导出 UTF-8 BOM CSV - 设置页覆盖审计开关/字段变更/系统变更/保留期与登录失败告警(含测试与模拟触发) - 模拟器支持 HTTP 捕获管线与系统内部变更两条通道 - 核心纯函数重写并扩充至 56 个单元用例
This commit is contained in:
+592
-117
@@ -1,9 +1,18 @@
|
||||
/**
|
||||
* 操作审计 · 模拟数据生成器(种子随机,确定性输出)
|
||||
*
|
||||
* 记录字段与产品设计 4.1 表格一一对应;resourceType/resourceName 通过
|
||||
* AuditCore.resolveResource 从路径解析得出(模拟真实捕获管线)。
|
||||
* 内置若干"故事线"数据:登录爆破、越权 403、服务端 500 等,便于验证筛选场景。
|
||||
* 记录字段与插件 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) {
|
||||
@@ -16,7 +25,7 @@
|
||||
|
||||
var DAY = 86400000;
|
||||
var MIN = 60000;
|
||||
var BASE_COUNT = 128; // 随机生成的基础记录条数
|
||||
var BASE_COUNT = 116;
|
||||
|
||||
// ---------- 种子随机 ----------
|
||||
function mulberry32(seed) {
|
||||
@@ -38,10 +47,17 @@
|
||||
function hex(r, n) {
|
||||
var s = '';
|
||||
var chars = '0123456789abcdef';
|
||||
for (var i = 0; i < n; i++) s += chars[rint(r, 0, 15)];
|
||||
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',
|
||||
@@ -52,156 +68,615 @@
|
||||
};
|
||||
|
||||
var USERS = {
|
||||
'guichumo': { ips: ['192.168.1.23', '116.24.65.187'], uas: ['chrome', 'edge'] },
|
||||
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 SLUGS = ['hello-halo', 'self-host-guide', 'weekly-2026-w46', 'reading-notes-11', 'plugin-dev-diary', 'spring-festival-2026'];
|
||||
var PLUGINS = ['plugin-search-widget', 'plugin-comment-widget', 'plugin-sitemap', 'plugin-links'];
|
||||
|
||||
function postPath(r) { return '/apis/content.halo.run/v1alpha1/posts/' + pick(r, SLUGS); }
|
||||
function publishPath(r) { return '/apis/api.content.halo.run/v1alpha1/posts/' + pick(r, SLUGS) + '/publish'; }
|
||||
function commentPath(r) { return '/apis/content.halo.run/v1alpha1/comments/comment-' + hex(r, 6); }
|
||||
function approvePath(r) { return '/apis/api.halo.run/v1alpha1/comments/comment-' + hex(r, 6) + '/approve'; }
|
||||
function replyPath(r) { return '/apis/api.halo.run/v1alpha1/comments/comment-' + hex(r, 6) + '/reply'; }
|
||||
function attachPath(r) { return '/apis/storage.halo.run/v1alpha1/attachments/attachment-' + hex(r, 6); }
|
||||
|
||||
// [权重, 操作者, 方法, 路径函数, 状态码]
|
||||
var ACTIONS = [
|
||||
[16, 'guichumo', 'PUT', postPath, 200],
|
||||
[10, 'guichumo', 'POST', publishPath, 200],
|
||||
[6, 'guichumo', 'POST', function () { return '/apis/content.halo.run/v1alpha1/posts'; }, 201],
|
||||
[5, 'guichumo', 'PUT', function () { return '/apis/v1alpha1/configmaps/system'; }, 200],
|
||||
[4, 'guichumo', 'PUT', function (r) { return '/apis/plugin.halo.run/v1alpha1/plugins/' + pick(r, PLUGINS); }, 200],
|
||||
[4, 'guichumo', 'POST', function () { return '/apis/storage.halo.run/v1alpha1/attachments'; }, 201],
|
||||
[4, 'guichumo', 'DELETE', attachPath, 204],
|
||||
[3, 'guichumo', 'DELETE', commentPath, 204],
|
||||
[3, 'guichumo', 'PUT', function () { return '/apis/v1alpha1/roles/role-template-editor'; }, 200],
|
||||
[2, 'guichumo', 'POST', function () { return '/login'; }, 200],
|
||||
[2, 'guichumo', 'POST', function () { return '/logout'; }, 200],
|
||||
[12, 'editor.zhang', 'PUT', postPath, 200],
|
||||
[6, 'editor.zhang', 'POST', function () { return '/apis/content.halo.run/v1alpha1/posts'; }, 201],
|
||||
[2, 'editor.zhang', 'POST', function () { return '/login'; }, 200],
|
||||
[2, 'editor.zhang', 'POST', function () { return '/apis/storage.halo.run/v1alpha1/attachments'; }, 201],
|
||||
[8, 'writer.li', 'PUT', postPath, 200],
|
||||
[6, 'writer.li', 'POST', approvePath, 200],
|
||||
[3, 'writer.li', 'POST', replyPath, 200],
|
||||
[2, 'writer.li', 'POST', function () { return '/login'; }, 200]
|
||||
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 TOTAL_WEIGHT = ACTIONS.reduce(function (sum, a) { return sum + a[0]; }, 0);
|
||||
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' } }
|
||||
];
|
||||
|
||||
function userCtx(r, name) {
|
||||
var u = USERS[name];
|
||||
// ---------- 文档快照(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 {
|
||||
username: name,
|
||||
remoteIp: pick(r, u.ips),
|
||||
userAgent: UAS[pick(r, u.uas)]
|
||||
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 rec(timestamp, username, method, path, statusCode, remoteIp, userAgent) {
|
||||
var res = AuditCore.resolveResource(path);
|
||||
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 {
|
||||
id: null,
|
||||
timestamp: timestamp,
|
||||
username: username,
|
||||
method: AuditCore.normalizeMethod(method),
|
||||
path: path,
|
||||
resourceType: res.resourceType,
|
||||
resourceName: res.resourceName,
|
||||
statusCode: statusCode,
|
||||
remoteIp: remoteIp,
|
||||
userAgent: userAgent
|
||||
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(20261121);
|
||||
if (typeof now !== 'number') {
|
||||
now = Date.now();
|
||||
}
|
||||
var r = mulberry32(20261122);
|
||||
var logs = [];
|
||||
|
||||
// 基础记录:均匀散布在近 90 天
|
||||
for (var i = 0; i < BASE_COUNT; i++) {
|
||||
var roll = r() * TOTAL_WEIGHT;
|
||||
var action = ACTIONS[0];
|
||||
for (var j = 0; j < ACTIONS.length; j++) {
|
||||
roll -= ACTIONS[j][0];
|
||||
if (roll <= 0) { action = ACTIONS[j]; break; }
|
||||
}
|
||||
var ctx = userCtx(r, action[1]);
|
||||
var time = now - rint(r, 5 * MIN, 90 * DAY);
|
||||
logs.push(rec(
|
||||
new Date(time).toISOString(), ctx.username,
|
||||
action[2], action[3](r), action[4],
|
||||
ctx.remoteIp, ctx.userAgent
|
||||
));
|
||||
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. 攻击者爆破登录:20 天前,同一 IP 6 次失败,UA 为 python-requests
|
||||
var burstBase = now - 20 * DAY - 3600 * 1000 * 3; // 约 20 天前的凌晨
|
||||
[0, 41, 88, 135, 210, 400].forEach(function (off) {
|
||||
logs.push(rec(
|
||||
new Date(burstBase + off * 1000).toISOString(), '未知用户',
|
||||
'POST', '/login', 401, '203.0.113.66', UAS.bot
|
||||
));
|
||||
// ---- 故事线 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()
|
||||
}));
|
||||
|
||||
// 2. 站长忘记密码:12 天前输错两次后登录成功
|
||||
var b2 = now - 12 * DAY - 3600 * 1000 * 10;
|
||||
[[0, 401], [97, 401], [180, 200]].forEach(function (pair) {
|
||||
var ctx = { remoteIp: '192.168.1.23', userAgent: UAS.chrome };
|
||||
logs.push(rec(
|
||||
new Date(b2 + pair[0] * 1000).toISOString(),
|
||||
pair[1] === 200 ? 'guichumo' : '未知用户',
|
||||
'POST', '/login', pair[1], ctx.remoteIp, ctx.userAgent
|
||||
));
|
||||
// ---- 故事线 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()
|
||||
}));
|
||||
|
||||
// 3. 越权尝试:8 天前 editor.zhang 尝试修改系统设置被拒绝
|
||||
logs.push(rec(
|
||||
new Date(now - 8 * DAY - 3600 * 1000 * 14).toISOString(), 'editor.zhang',
|
||||
'PUT', '/apis/v1alpha1/configmaps/system', 403, '116.24.65.201', UAS.edge
|
||||
));
|
||||
// ---- 故事线 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 }
|
||||
]
|
||||
}));
|
||||
|
||||
// 4. 权限不足:5 天前 writer.li 尝试删除评论被拒绝
|
||||
logs.push(rec(
|
||||
new Date(now - 5 * DAY - 3600 * 1000 * 9).toISOString(), 'writer.li',
|
||||
'DELETE', '/apis/content.halo.run/v1alpha1/comments/comment-a3f802', 403, '120.79.10.45', UAS.safari
|
||||
));
|
||||
// ---- 故事线 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()
|
||||
}));
|
||||
|
||||
// 5. 资源不存在:3 天前删除一个已不存在的附件
|
||||
logs.push(rec(
|
||||
new Date(now - 3 * DAY - 3600 * 1000 * 20).toISOString(), 'guichumo',
|
||||
'DELETE', '/apis/storage.halo.run/v1alpha1/attachments/attachment-dead42', 404, '192.168.1.23', UAS.chrome
|
||||
));
|
||||
// ---- 故事线 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()
|
||||
}));
|
||||
|
||||
// 6. 服务端异常:2 天前发布文章返回 500
|
||||
logs.push(rec(
|
||||
new Date(now - 2 * DAY - 3600 * 1000 * 6).toISOString(), 'writer.li',
|
||||
'POST', '/apis/api.content.halo.run/v1alpha1/posts/self-host-guide/publish', 500, '120.79.10.45', UAS.chrome
|
||||
));
|
||||
// ---- 故事线 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()
|
||||
}));
|
||||
|
||||
// 7. 误操作定位:1 天前删除了一篇文章
|
||||
logs.push(rec(
|
||||
new Date(now - DAY - 3600 * 1000 * 2).toISOString(), 'guichumo',
|
||||
'DELETE', '/apis/content.halo.run/v1alpha1/posts/weekly-2026-w45', 204, '192.168.1.23', UAS.chrome
|
||||
));
|
||||
|
||||
// 排序 + 分配 id(最新记录 id 数字最大)
|
||||
// ---- 排序 + 分配记录标识(最新记录编号最大) ----
|
||||
logs.sort(function (a, b) {
|
||||
return new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime();
|
||||
});
|
||||
logs.forEach(function (l, idx) {
|
||||
l.id = 'audit-' + String(logs.length - idx).padStart(4, '0');
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user