fix: 落实官方规范评审修复(生成 API 客户端 / SSRF 防护 / 凭据迁 Secret)
对应 docs/operation-audit-halo-compliance.md 评审的两项不符合与第 5 节修复建议: 1. API 客户端生成(清单第 2 条):build.gradle 配置 haloPlugin.openApi 管线; 查询/导出/测试端点补 tag、参数声明与类型化 response(生成契约输入); bootstrap OpenAPI 规范 JSON(与 Springdoc 声明一一对手工对齐)驱动 devtools generateApiClient 产出 TS 客户端(ui/src/api/generated,openapi-generator 7.7.0); UI 经 ui/src/api 封装层复用宿主 axiosInstance,替换手写类型与裸 fetch; @halo-dev/api-client 由死依赖转为 axiosInstance 实际来源。 2. 出站安全与凭据(清单第 4 条):新增 OutboundUrlGuard——URI 解析 + scheme 白名单 + 禁止用户名/密码形态 + 全量 DNS 解析并拒绝环回/私网/链路本地/组播/未指定/ 云元数据(169.254.169.254、100.100.100.200、fd00:ec2::254)/CGNAT/ULA/Teredo/ 6to4/文档与保留段;PinnedAddressResolverGroup 钉定校验地址建连(DNS rebinding 防护,TLS SNI/Host 仍取原主机名);重定向显式关闭、三层超时 10 秒。 Webhook 地址凭据迁移 Halo Secret(settings.yaml 改 secret 输入存引用,服务端经 ReactiveExtensionClient 解析键 webhookUrl,fail closed);日志只记主机名, 不再记录可能内嵌 access_token 的完整地址。 测试:后端 244 用例(新增守卫地址段枚举/Secret 解析 fail closed/建连钉定证明等 25 例)+ UI vitest 54 用例 + vue-tsc + ./gradlew build 全部通过。
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import { VButton, VSpace } from '@halo-dev/components'
|
||||
import type { AuditLog, AuditLogChangeType } from '@/utils/audit'
|
||||
import type { AuditLog, ChangeType } from '@/api/generated'
|
||||
import {
|
||||
changeTypeLabel,
|
||||
changeValueDisplay,
|
||||
@@ -34,7 +34,7 @@ const resultText = computed(() => {
|
||||
const changes = computed(() => props.log?.spec.changes ?? [])
|
||||
|
||||
const changeBadgeClass = (changeType?: string | null): string => {
|
||||
switch (changeType as AuditLogChangeType) {
|
||||
switch (changeType as ChangeType) {
|
||||
case 'ADDED':
|
||||
return 'change-added'
|
||||
case 'REMOVED':
|
||||
|
||||
@@ -3,52 +3,47 @@ import { fileURLToPath } from 'node:url'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
// 结构性回归测试:不渲染组件,仅检查 SFC 源码中的导出功能链路。
|
||||
// 导出为按当前筛选条件的全量 CSV 下载:工具栏「导出」按钮 → fetch 导出端点
|
||||
// (筛选参数与 query 一致,但去掉分页参数)→ blob 触发 a[download] 下载,
|
||||
// 期间按钮进入 loading、失败走顶部 VAlert 错误提示。
|
||||
// 历史上本功能不存在,本测试锁定端点路径与下载链路形态,防止路径漂移或链路残缺。
|
||||
// 导出为按当前筛选条件的全量 CSV 下载:工具栏「导出」按钮 → 生成客户端
|
||||
// exportAuditLogs(responseType: 'blob',端点路径与参数由 generateApiClient
|
||||
// 从后端声明生成,禁止手写)→ blob 触发 a[download] 下载,期间按钮进入 loading、
|
||||
// 失败走顶部 VAlert 错误提示。本测试锁定生成客户端链路形态与下载行为,
|
||||
// 防止链路残缺或退回裸 fetch。
|
||||
const sfc = readFileSync(fileURLToPath(new URL('./AuditLogList.vue', import.meta.url)), 'utf-8')
|
||||
|
||||
describe('AuditLogList 导出', () => {
|
||||
it('声明与后端一致的导出端点路径', () => {
|
||||
expect(sfc).toContain(
|
||||
"const EXPORT_ENDPOINT =\n '/apis/audit.guichumo.com/v1alpha1/auditlogs/-/export'",
|
||||
)
|
||||
it('经生成客户端导出(不手写端点路径、不裸 fetch)', () => {
|
||||
expect(sfc).toContain('auditApiClient.audit.exportAuditLogs(exportRequest')
|
||||
// 端点路径由生成客户端承载,SFC 不再出现硬编码路径
|
||||
expect(sfc).not.toContain('/apis/audit.guichumo.com')
|
||||
expect(sfc).not.toMatch(/\bfetch\(/)
|
||||
})
|
||||
|
||||
it('工具栏导出按钮:IconSave 图标、loading 绑定 exporting、点击 exportLogs', () => {
|
||||
// 导出按钮位于 VPageHeader #actions 的 VSpace 内,与「刷新」并列
|
||||
const actionsStart = sfc.indexOf('<template #actions>')
|
||||
expect(actionsStart).toBeGreaterThan(-1)
|
||||
const actionsEnd = sfc.indexOf('</VSpace>', actionsStart)
|
||||
const actions = sfc.slice(actionsStart, actionsEnd)
|
||||
expect(actions).toMatch(/<VButton :loading="exporting"[^>]*@click="exportLogs">/)
|
||||
expect(actions).toContain('<IconSave class="h-full w-full" />')
|
||||
expect(actions).toContain('导出')
|
||||
expect(actions).toContain('刷新')
|
||||
})
|
||||
|
||||
it('导出前去掉分页参数(导出为全量接口)', () => {
|
||||
it('导出请求复用查询筛选条件且不带分页参数(导出为全量接口)', () => {
|
||||
const fnStart = sfc.indexOf('async function exportLogs()')
|
||||
expect(fnStart).toBeGreaterThan(-1)
|
||||
const fn = sfc.slice(fnStart, sfc.indexOf('function resetFilter()', fnStart))
|
||||
expect(fn).toContain('new URLSearchParams(buildQuery())')
|
||||
expect(fn).toContain("params.delete('page')")
|
||||
expect(fn).toContain("params.delete('size')")
|
||||
expect(fn).toContain('const request = listRequest()')
|
||||
expect(fn).toContain('AuditV1alpha1ConsoleApiExportAuditLogsRequest')
|
||||
expect(fn).toContain('fieldSelector: request.fieldSelector')
|
||||
expect(fn).toContain('startTime: request.startTime')
|
||||
expect(fn).not.toContain('page: request.page')
|
||||
expect(fn).not.toContain('size: request.size')
|
||||
})
|
||||
|
||||
it('下载链路:fetch 非 ok 报错进 VAlert,blob → createObjectURL → a[download]', () => {
|
||||
it('二进制响应以 blob 请求(CSV 附件下载)', () => {
|
||||
const fnStart = sfc.indexOf('async function exportLogs()')
|
||||
const fn = sfc.slice(fnStart, sfc.indexOf('function resetFilter()', fnStart))
|
||||
// 错误语义与查询一致:HTTP 非 2xx 抛错并写入 error(顶部 VAlert 展示)
|
||||
expect(fn).toMatch(/throw new Error\(`导出失败(HTTP \$\{response\.status\})`\)/)
|
||||
expect(fn).toContain('error.value = e instanceof Error ? e.message : String(e)')
|
||||
expect(fn).toContain('await response.blob()')
|
||||
expect(fn).toContain("responseType: 'blob'")
|
||||
expect(fn).toContain('URL.createObjectURL(blob)')
|
||||
expect(fn).toMatch(/anchor\.download = exportFilename\(\)/)
|
||||
expect(fn).toContain('anchor.click()')
|
||||
expect(fn).toContain('URL.revokeObjectURL(url)')
|
||||
// 无论成败都复位按钮 loading
|
||||
})
|
||||
|
||||
it('失败进顶部 VAlert:错误摘要写 error,无论成败都复位按钮 loading', () => {
|
||||
const fnStart = sfc.indexOf('async function exportLogs()')
|
||||
const fn = sfc.slice(fnStart, sfc.indexOf('function resetFilter()', fnStart))
|
||||
expect(fn).toContain("error.value = errorMessage(e, '导出失败')")
|
||||
expect(fn).toMatch(/finally\s*\{\s*exporting\.value = false\s*\}/)
|
||||
})
|
||||
|
||||
|
||||
@@ -28,8 +28,8 @@ describe('AuditLogList 查看该资源历史', () => {
|
||||
})
|
||||
|
||||
it('不新增后端参数:复用现有 resourceType fieldSelector 与 keyword 模糊匹配', () => {
|
||||
// 与现有筛选/导出条件共用同一套状态——buildQuery 生成相同参数形态
|
||||
expect(sfc).toContain("params.append('fieldSelector', `spec.resourceType=${filterResourceType.value}`)")
|
||||
expect(sfc).toContain("params.set('keyword', filterKeyword.value.trim())")
|
||||
// 与现有筛选/导出条件共用同一套状态——listRequest 生成相同参数形态(生成客户端请求对象)
|
||||
expect(sfc).toContain('fieldSelector.push(`spec.resourceType=${filterResourceType.value}`)')
|
||||
expect(sfc).toContain('keyword: filterKeyword.value.trim() || undefined')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -14,7 +14,12 @@ import {
|
||||
IconRefreshLine,
|
||||
IconSave,
|
||||
} from '@halo-dev/components'
|
||||
import type { AuditLog, ListResult } from '@/utils/audit'
|
||||
import type {
|
||||
AuditLog,
|
||||
AuditV1alpha1ConsoleApiExportAuditLogsRequest,
|
||||
AuditV1alpha1ConsoleApiListAuditLogsRequest,
|
||||
} from '@/api/generated'
|
||||
import { auditApiClient } from '@/api'
|
||||
import {
|
||||
describeOperation,
|
||||
displayUsername,
|
||||
@@ -26,13 +31,8 @@ import {
|
||||
} from '@/utils/audit'
|
||||
import AuditLogDetail from './AuditLogDetail.vue'
|
||||
|
||||
/** 查询端点路径(与 AuditQueryEndpoint 保持一致)。 */
|
||||
const QUERY_ENDPOINT =
|
||||
'/apis/audit.guichumo.com/v1alpha1/auditlogs/-/query'
|
||||
|
||||
/** 导出端点路径:筛选参数与 query 一致,全量 CSV 附件下载(与 AuditQueryEndpoint 保持一致)。 */
|
||||
const EXPORT_ENDPOINT =
|
||||
'/apis/audit.guichumo.com/v1alpha1/auditlogs/-/export'
|
||||
// 查询/导出走生成客户端(ui/src/api/generated,generateApiClient 产物):
|
||||
// 端点路径与参数由后端 Springdoc 声明生成,禁止在此手写或覆盖。
|
||||
|
||||
const RESOURCE_TYPES = [
|
||||
'posts', 'comments', 'users', 'roles', 'auth', 'settings', 'configmaps',
|
||||
@@ -73,54 +73,52 @@ const hasActiveFilter = computed(
|
||||
filterEndTime.value,
|
||||
)
|
||||
|
||||
function buildQuery(): string {
|
||||
const params = new URLSearchParams()
|
||||
params.set('page', String(page.value))
|
||||
params.set('size', String(size.value))
|
||||
params.set('sort', 'metadata.creationTimestamp,desc')
|
||||
/** 查询请求对象(与后端 AuditQueryEndpoint 声明一致的生成参数形态)。 */
|
||||
function listRequest(): AuditV1alpha1ConsoleApiListAuditLogsRequest {
|
||||
const fieldSelector: string[] = []
|
||||
if (filterUsername.value.trim()) {
|
||||
params.append('fieldSelector', `spec.username=${filterUsername.value.trim()}`)
|
||||
fieldSelector.push(`spec.username=${filterUsername.value.trim()}`)
|
||||
}
|
||||
if (filterResourceType.value) {
|
||||
params.append('fieldSelector', `spec.resourceType=${filterResourceType.value}`)
|
||||
fieldSelector.push(`spec.resourceType=${filterResourceType.value}`)
|
||||
}
|
||||
if (filterMethod.value) {
|
||||
params.append('fieldSelector', `spec.method=${filterMethod.value}`)
|
||||
fieldSelector.push(`spec.method=${filterMethod.value}`)
|
||||
}
|
||||
// 语义动作筛选(spec.action,捕获时标注落库;历史记录无标注不会被筛出)
|
||||
if (filterAction.value) {
|
||||
params.append('fieldSelector', `spec.action=${filterAction.value}`)
|
||||
fieldSelector.push(`spec.action=${filterAction.value}`)
|
||||
}
|
||||
if (filterResult.value) {
|
||||
params.set('result', filterResult.value)
|
||||
return {
|
||||
page: page.value,
|
||||
size: size.value,
|
||||
sort: 'metadata.creationTimestamp,desc',
|
||||
fieldSelector,
|
||||
result: filterResult.value || undefined,
|
||||
keyword: filterKeyword.value.trim() || undefined,
|
||||
startTime: localDateTimeToInstant(filterStartTime.value) ?? undefined,
|
||||
endTime: localDateTimeToInstant(filterEndTime.value) ?? undefined,
|
||||
}
|
||||
if (filterKeyword.value.trim()) {
|
||||
params.set('keyword', filterKeyword.value.trim())
|
||||
}
|
||||
|
||||
/** 统一错误摘要:HTTP 错误取状态码(沿用旧 fetch 行为),其余取消息原文。 */
|
||||
function errorMessage(e: unknown, action: string): string {
|
||||
const status = (e as { response?: { status?: number } })?.response?.status
|
||||
if (status) {
|
||||
return `${action}(HTTP ${status})`
|
||||
}
|
||||
const start = localDateTimeToInstant(filterStartTime.value)
|
||||
const end = localDateTimeToInstant(filterEndTime.value)
|
||||
if (start) {
|
||||
params.set('startTime', start)
|
||||
}
|
||||
if (end) {
|
||||
params.set('endTime', end)
|
||||
}
|
||||
return params.toString()
|
||||
return e instanceof Error ? e.message : String(e)
|
||||
}
|
||||
|
||||
async function fetchLogs() {
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const response = await fetch(`${QUERY_ENDPOINT}?${buildQuery()}`)
|
||||
if (!response.ok) {
|
||||
throw new Error(`查询失败(HTTP ${response.status})`)
|
||||
}
|
||||
const result = (await response.json()) as ListResult<AuditLog>
|
||||
logs.value = result.items ?? []
|
||||
total.value = result.total ?? 0
|
||||
const { data } = await auditApiClient.audit.listAuditLogs(listRequest())
|
||||
logs.value = data.items ?? []
|
||||
total.value = data.total ?? 0
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : String(e)
|
||||
error.value = errorMessage(e, '查询失败')
|
||||
logs.value = []
|
||||
total.value = 0
|
||||
} finally {
|
||||
@@ -138,20 +136,24 @@ function exportFilename(): string {
|
||||
)
|
||||
}
|
||||
|
||||
/** 按当前筛选条件导出全部记录:fetch → blob → 触发下载,失败进顶部错误提示。 */
|
||||
/** 按当前筛选条件导出全部记录:生成客户端(responseType=blob)→ 触发下载,失败进顶部错误提示。 */
|
||||
async function exportLogs() {
|
||||
exporting.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
// 导出为全量接口,无需分页参数
|
||||
const params = new URLSearchParams(buildQuery())
|
||||
params.delete('page')
|
||||
params.delete('size')
|
||||
const response = await fetch(`${EXPORT_ENDPOINT}?${params.toString()}`)
|
||||
if (!response.ok) {
|
||||
throw new Error(`导出失败(HTTP ${response.status})`)
|
||||
// 导出为全量接口,无需分页参数(复用查询请求,仅保留筛选条件)
|
||||
const request = listRequest()
|
||||
const exportRequest: AuditV1alpha1ConsoleApiExportAuditLogsRequest = {
|
||||
fieldSelector: request.fieldSelector,
|
||||
result: request.result,
|
||||
keyword: request.keyword,
|
||||
startTime: request.startTime,
|
||||
endTime: request.endTime,
|
||||
}
|
||||
const blob = await response.blob()
|
||||
const response = await auditApiClient.audit.exportAuditLogs(exportRequest, {
|
||||
responseType: 'blob',
|
||||
})
|
||||
const blob = response.data
|
||||
const url = URL.createObjectURL(blob)
|
||||
const anchor = document.createElement('a')
|
||||
anchor.href = url
|
||||
@@ -159,7 +161,7 @@ async function exportLogs() {
|
||||
anchor.click()
|
||||
URL.revokeObjectURL(url)
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : String(e)
|
||||
error.value = errorMessage(e, '导出失败')
|
||||
} finally {
|
||||
exporting.value = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user