3987b8d7fe
- 此前全树 0 处全局错误处理器:装 app.config.errorHandler + window error + unhandledrejection, 每条都留可诊断记录、界面只出中文、处理器自身异常不重抛也不白屏(9 例判据)。 - safeHttpUrl 未覆盖的 3 处(两个品牌 Tab 与 useVersionUpdate 直装服务端 file_url)收口; 令牌键 aiimage_auth_token 由 4 处各自定义 + 2 处内联收成唯一定义点。 - 「localStorage 有 uid 就免校验」改走 auth-gate 单点,剩余例外台账化且同口径必须能抓到 评审点名的原写法(6 例);卸载配对 3 处补齐 + 全树判据(7 例)。 - task-status-text 把 CANCELLED 显示成"执行中"改对,CANCELED/CANCELLED 两拼写都映射(4 例)。 - 主审计补的四处修复:新判据里两处拿数组和 0 比(怎么写都红);静默 catch 的"用户可见出口" 符号表认不出本仓 userNotice 与注入式 notify,把真报了用户的 catch 判成静默; 新错误出口带来的 2 个 try/catch 按棘轮合同写理由进基线(296→298,silent 一格没涨)。 - 验收:node --test 820 例 0 红、npx vue-tsc --noEmit rc=0。
138 lines
8.2 KiB
TypeScript
138 lines
8.2 KiB
TypeScript
import { test } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { fileURLToPath } from 'node:url'
|
||
import { dirname, join, resolve, relative, sep } from 'node:path'
|
||
import { listSourceFiles, readSource } from './helpers/task-terminal-ast-ruler.ts'
|
||
import { exportedGuardedNames, scanAllUrlOutlets, scanUrlOutlets } from './helpers/url-outlet-scanner.ts'
|
||
import { isHttpUrl, safeHttpUrl } from '../src/shared/utils/safe-url.ts'
|
||
|
||
/**
|
||
* 跳转/下载出口的白名单强制校验(2026-10-09 评审安全面:`safeHttpUrl` 已经有了,
|
||
* 但 `BrandBrandTab.vue`、`BrandVariantTab.vue`、`useVersionUpdate.ts` 三处直接拿服务端返回的
|
||
* `file_url` 去 `window.open` / `location.href`)。
|
||
*
|
||
* 判据按 AST 判"这次跳转的地址过没过白名单"(口径与理由写在 helpers/url-outlet-scanner.ts)。
|
||
* 本轮顺手把把守点下沉到了 `saveUrlWithProgress`:14 个下载出口共用同一个桥入口,
|
||
* 在那一处判比在每页各补一遍靠谱,也防止"新增一页忘了判"。
|
||
*
|
||
* 台账:未过校验的出口数只许减(现在钉 0),新增一处就红。
|
||
*/
|
||
|
||
const here = dirname(fileURLToPath(import.meta.url))
|
||
const ROOT = resolve(here, '..')
|
||
|
||
/** 台账:已知未校验的跳转/下载出口(文件 → 处数)。本轮清零,往后只许减不许加。 */
|
||
const UNGUARDED_LEDGER: Record<string, number> = {}
|
||
|
||
const FILES = listSourceFiles(resolve(ROOT, 'src')).map((p) => relative(ROOT, p).split(sep).join('/'))
|
||
const SOURCES = FILES.map((file) => ({ file, code: readSource(file, ROOT) }))
|
||
|
||
function scan(sources = SOURCES) {
|
||
return scanAllUrlOutlets(sources)
|
||
}
|
||
|
||
const SCANS = scan()
|
||
const ALL_OUTLETS = SCANS.flatMap((s) => s.outlets)
|
||
const ALL_UNGUARDED = SCANS.flatMap((s) => s.unguarded)
|
||
|
||
function scanOf(file: string) {
|
||
return SCANS.find((s) => s.file === file)
|
||
}
|
||
|
||
test('分母自证:出口真的被数到了(扫不到就不许报"全部已校验")', () => {
|
||
assert.ok(FILES.length > 100, `只扫到 ${FILES.length} 个源文件,分母不对`)
|
||
assert.ok(ALL_OUTLETS.length >= 20, `全树只数到 ${ALL_OUTLETS.length} 处跳转/下载出口(下限 20)——扫描器失明`)
|
||
const kinds = new Set(ALL_OUTLETS.map((outlet) => outlet.kind))
|
||
for (const kind of ['window.open', 'saveUrlWithProgress', 'location.href=']) {
|
||
assert.ok(kinds.has(kind), `出口形状 ${kind} 一种都没扫到,判据口径变了`)
|
||
}
|
||
// 非零对照:评审点名的 3 个文件各自都得有出口进了分母
|
||
assert.ok(scanOf('src/pages/brand/components/BrandBrandTab.vue')?.outlets.length >= 2, 'BrandBrandTab 的下载出口没进分母')
|
||
assert.ok(scanOf('src/pages/brand/components/BrandVariantTab.vue')?.outlets.length >= 2, 'BrandVariantTab 的下载出口没进分母')
|
||
assert.ok(scanOf('src/shared/composables/useVersionUpdate.ts')?.outlets.length >= 1, 'useVersionUpdate 的跳转出口没进分母')
|
||
})
|
||
|
||
test('全树跳转/下载出口都过了白名单(台账之外 0 处)', () => {
|
||
const ledger: Record<string, number> = {}
|
||
for (const outlet of ALL_UNGUARDED) {
|
||
ledger[outlet.file] = (ledger[outlet.file] ?? 0) + 1
|
||
}
|
||
const worse: string[] = []
|
||
for (const [file, count] of Object.entries(ledger)) {
|
||
const allowed = UNGUARDED_LEDGER[file]
|
||
if (allowed === undefined) worse.push(`${file} 出现未校验出口 ${count} 处`)
|
||
else if (count > allowed) worse.push(`${file} 从 ${allowed} 处涨到 ${count} 处`)
|
||
}
|
||
const evidence = ALL_UNGUARDED.map((o) => `${o.file}:${o.line} ${o.kind}(宿主 ${o.host})${o.snippet}`).join('\n')
|
||
assert.deepEqual(worse, [], `这些跳转/下载没先校验地址:\n${evidence}`)
|
||
// 台账里的例外现在都不存在了,留着就是尺子在放水
|
||
for (const file of Object.keys(UNGUARDED_LEDGER)) {
|
||
assert.ok(file in ledger, `台账里的 ${file} 已经没有未校验出口了,把这一格删掉(只许减)`)
|
||
}
|
||
})
|
||
|
||
test('评审点名的 3 处:现在都经白名单,且拒绝时看得见', () => {
|
||
for (const file of [
|
||
'src/pages/brand/components/BrandBrandTab.vue',
|
||
'src/pages/brand/components/BrandVariantTab.vue',
|
||
'src/shared/composables/useVersionUpdate.ts',
|
||
]) {
|
||
const found = scanOf(file)
|
||
assert.ok(found, `${file} 没进分母`)
|
||
assert.deepEqual(found.unguarded, [], `${file} 还有未校验的跳转/下载出口`)
|
||
const source = readSource(file, ROOT)
|
||
assert.match(source, /isHttpUrl\(/, `${file} 里没找到判定式白名单`)
|
||
assert.match(source, /safeHttpUrl\(/, `${file} 里没找到安全地址出口`)
|
||
assert.match(source, /\[safe-url\]/, `${file} 校验不通过时没留一行可查的记录`)
|
||
}
|
||
// 被拦下时不是"什么都不做":三处都得把话说到界面上
|
||
assert.match(readSource('src/pages/brand/components/BrandBrandTab.vue', ROOT), /function openDownloadUrl\(/)
|
||
assert.match(readSource('src/pages/brand/components/BrandVariantTab.vue', ROOT), /async function downloadSource\([\s\S]*?isHttpUrl\(/)
|
||
assert.match(readSource('src/shared/composables/useVersionUpdate.ts', ROOT), /async function startUpdate\([\s\S]*?isHttpUrl\([\s\S]*?hint\.value = '更新包地址不可用/)
|
||
})
|
||
|
||
test('把守点下沉:saveUrlWithProgress 自己先校验,新增页面漏判也跳不出去', () => {
|
||
const source = readSource('src/shared/utils/download-progress.ts', ROOT)
|
||
assert.match(source, /export async function saveUrlWithProgress\([\s\S]{0,400}?if \(!isHttpUrl\(url\)\)/,
|
||
'saveUrlWithProgress 没有在入口先校验地址')
|
||
assert.match(source, /return \{ success: false, error: UNSAFE_DOWNLOAD_URL_TEXT \}/, '校验不通过时没有把原因回给调用方')
|
||
assert.ok(exportedGuardedNames(source, 'src/shared/utils/download-progress.ts').includes('saveUrlWithProgress'),
|
||
'扫描器没把 saveUrlWithProgress 认成跨模块把守点')
|
||
})
|
||
|
||
test('isHttpUrl 真值表:与 safeHttpUrl 同一个口径,不各判一套', () => {
|
||
for (const ok of ['https://cdn.example.com/a.mp4', 'http://127.0.0.1:5173/x', '/newApi/download/x']) {
|
||
assert.equal(isHttpUrl(ok), true, `${ok} 该放行`)
|
||
assert.notEqual(safeHttpUrl(ok), '#', `${ok} 两个函数判得不一致`)
|
||
}
|
||
for (const bad of ['javascript:alert(1)', 'data:text/html,<script>alert(1)</script>', 'file:///C:/win.ini', ' ', '', undefined, null]) {
|
||
assert.equal(isHttpUrl(bad as string), false, `${bad} 该拦下`)
|
||
assert.equal(safeHttpUrl(bad as string), '#', `${bad} 两个函数判得不一致`)
|
||
}
|
||
})
|
||
|
||
test('注入自证:把白名单调用摘掉,扫描器立刻报未校验', () => {
|
||
const file = 'src/pages/brand/components/BrandBrandTab.vue'
|
||
const source = SOURCES.find((s) => s.file === file)?.code ?? ''
|
||
assert.match(source, /isHttpUrl\(/, '锚点不在了,注入自证不作数')
|
||
// 只把校验函数改名,出口形状与个数都不动:判据必须因此翻红
|
||
const stripped = source.replace(/\bisHttpUrl\(/g, 'uncheckedHas(').replace(/\bsafeHttpUrl\(/g, 'rawValueOf(')
|
||
assert.notEqual(stripped, source, '注入没生效,这条自证是假的')
|
||
const before = scanUrlOutlets(source, file)
|
||
const after = scanUrlOutlets(stripped, file)
|
||
assert.equal(after.outlets.length, before.outlets.length, '摘掉校验把出口也弄丢了,分母在放水')
|
||
assert.deepEqual(before.unguarded, [], '已校验的写法被判成了违例(口径过严)')
|
||
assert.ok(after.unguarded.length >= 2, `摘掉校验后未校验出口只有 ${after.unguarded.length} 处(应 ≥2),这条尺子是恒绿的`)
|
||
// 顶层(无宿主函数)的跳转也必须被抓到
|
||
const topLevel = scanUrlOutlets('window.location.href = serverFileUrl\n', '<内存样本>')
|
||
assert.deepEqual(topLevel.unguarded.map((o) => o.kind), ['location.href='], '模块顶层的跳转出口漏判')
|
||
})
|
||
|
||
test('桥与降级路径都收口:open_external_url 那条也要先判', () => {
|
||
const source = readSource('src/pages/amazon/AmazonConsolePage.vue', ROOT)
|
||
const report = scanUrlOutlets(source, 'src/pages/amazon/AmazonConsolePage.vue')
|
||
assert.ok(report.outlets.length >= 1, 'AmazonConsolePage 的教程下载出口没进分母')
|
||
assert.deepEqual(report.unguarded, [], '教程地址(服务端 /api/tutorial/latest 透传)没先过白名单')
|
||
assert.match(source, /api\.open_external_url\(url\)/, '桥侧用的还是未校验的原值')
|
||
})
|