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。
207 lines
8.0 KiB
TypeScript
207 lines
8.0 KiB
TypeScript
/**
|
||
* 跳转/下载出口的白名单校验扫描器(判据在 tests/url-outlet-guard.test.ts)。
|
||
*
|
||
* 为什么按 AST 而不是 grep:`window.location.href = 服务端返回的 file_url` 这类口子写法差别很大
|
||
* (`window.open(x)`、`saveUrlWithProgress(url, ...)`、`bridge.save_file_from_url(url, ...)`、
|
||
* 先存进变量再跳),grep 只能盯一种形状;而真正要判的是
|
||
* **"这次跳转的地址过没过白名单"**——校验可能写在同一个函数的开头,也可能被抽成本地小函数
|
||
* (本仓的收口写法就是这样)。所以按"宿主函数"判,再把具名本地函数的守卫沿调用链传上去
|
||
* (不动点迭代):函数改名、换排版、加参数都不会让尺子失明。
|
||
*
|
||
* 口径:
|
||
* ① 出口 = `open(` / `saveUrlWithProgress(` / `save_file_from_url(` 调用、`*.location.href =` 赋值、
|
||
* `*.location.assign(`/`*.location.replace(` 调用;
|
||
* ② 出口任一祖先函数体里出现 `isHttpUrl(` 或 `safeHttpUrl(` → 已校验;
|
||
* ③ 祖先函数调用了本文件里"已校验"的具名函数 → 也算已校验。
|
||
*/
|
||
import { createRequire } from 'node:module'
|
||
import { extractScript, parseModule } from './task-terminal-ast-ruler.ts'
|
||
|
||
const require = createRequire(import.meta.url)
|
||
const ts = require('typescript')
|
||
|
||
/** 白名单校验函数名(src/shared/utils/safe-url.ts 的出口) */
|
||
export const GUARD_NAMES = new Set(['isHttpUrl', 'safeHttpUrl'])
|
||
|
||
export interface UrlOutlet {
|
||
file: string
|
||
line: number
|
||
/** 出口形状的名字,报错时给可读证据 */
|
||
kind: string
|
||
/** 最内层具名宿主函数(找不到=模块顶层) */
|
||
host: string
|
||
/** 命中的那条语句(截断) */
|
||
snippet: string
|
||
/** 是否过了白名单(按祖先函数与调用链判) */
|
||
guarded: boolean
|
||
}
|
||
|
||
export interface OutletScan {
|
||
file: string
|
||
outlets: UrlOutlet[]
|
||
unguarded: UrlOutlet[]
|
||
}
|
||
|
||
function walk(node: any, cb: (n: any) => void): void {
|
||
cb(node)
|
||
ts.forEachChild(node, (child: any) => walk(child, cb))
|
||
}
|
||
|
||
/** 调用点的名字:`foo(...)` 取 foo,`a.b(...)` 取 b(`window.open` 就当 open)。 */
|
||
function calledName(call: any): string {
|
||
const callee = call.expression
|
||
if (!callee) return ''
|
||
if (ts.isIdentifier(callee)) return callee.text
|
||
if (ts.isPropertyAccessExpression(callee) && callee.name) return callee.name.text
|
||
return ''
|
||
}
|
||
|
||
function calleeText(call: any): string {
|
||
const callee = call.expression
|
||
return callee && typeof callee.getText === 'function' ? callee.getText() : ''
|
||
}
|
||
|
||
/** `x.location.href = ...` / `location.href = ...` 这类赋值出口。 */
|
||
function isLocationHrefAssignment(node: any): boolean {
|
||
if (!ts.isBinaryExpression(node)) return false
|
||
if (node.operatorToken?.kind !== ts.SyntaxKind.EqualsToken) return false
|
||
const left = node.left
|
||
if (!ts.isPropertyAccessExpression(left) || left.name?.text !== 'href') return false
|
||
const base = left.expression
|
||
return ts.isPropertyAccessExpression(base) && base.name?.text === 'location'
|
||
}
|
||
|
||
function outletKindOf(node: any): string | null {
|
||
if (ts.isCallExpression(node)) {
|
||
const name = calledName(node)
|
||
if (name === 'open') return 'window.open'
|
||
if (name === 'saveUrlWithProgress') return 'saveUrlWithProgress'
|
||
if (name === 'save_file_from_url') return 'bridge.save_file_from_url'
|
||
if ((name === 'assign' || name === 'replace') && /\.location$/.test(calleeText(node))) return `location.${name}`
|
||
return null
|
||
}
|
||
if (isLocationHrefAssignment(node)) return 'location.href='
|
||
return null
|
||
}
|
||
|
||
function isFunctionLike(node: any): boolean {
|
||
return ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)
|
||
|| ts.isArrowFunction(node) || ts.isMethodDeclaration(node)
|
||
}
|
||
|
||
function functionName(node: any): string {
|
||
if (!node) return ''
|
||
if (node.name && typeof node.name.text === 'string') return node.name.text
|
||
if (node.parent && ts.isVariableDeclaration(node.parent) && ts.isIdentifier(node.parent.name)) return node.parent.name.text
|
||
return ''
|
||
}
|
||
|
||
/** 沿 parent 链收集具名宿主函数(校验常写在入口函数开头,所以整条链都算)。 */
|
||
function hostChainOf(node: any): string[] {
|
||
const names: string[] = []
|
||
let cur = node
|
||
while (cur) {
|
||
if (isFunctionLike(cur)) {
|
||
const name = functionName(cur)
|
||
if (name && !names.includes(name)) names.push(name)
|
||
}
|
||
cur = cur.parent
|
||
}
|
||
return names
|
||
}
|
||
|
||
/** 文件里具名函数 → 它(含嵌套块)调用到的名字集合。 */
|
||
function collectCallGraph(sf: any): Map<string, string[]> {
|
||
const graph = new Map<string, string[]>()
|
||
walk(sf, (node: any) => {
|
||
if (!isFunctionLike(node)) return
|
||
const name = functionName(node)
|
||
if (!name) return
|
||
const calls = new Set<string>(graph.get(name) ?? [])
|
||
walk(node, (inner: any) => {
|
||
if (!ts.isCallExpression(inner)) return
|
||
const called = calledName(inner)
|
||
if (called) calls.add(called)
|
||
})
|
||
graph.set(name, [...calls])
|
||
})
|
||
return graph
|
||
}
|
||
|
||
/** 哪些函数名"带白名单校验":自己调过 isHttpUrl/safeHttpUrl,或调用链上过。 */
|
||
function guardedNamesOf(graph: Map<string, string[]>): Set<string> {
|
||
const guarded = new Set<string>()
|
||
for (const [name, calls] of graph) {
|
||
if (calls.some((call) => GUARD_NAMES.has(call))) guarded.add(name)
|
||
}
|
||
let changed = true
|
||
while (changed) {
|
||
changed = false
|
||
for (const [name, calls] of graph) {
|
||
if (guarded.has(name)) continue
|
||
if (calls.some((call) => guarded.has(call))) {
|
||
guarded.add(name)
|
||
changed = true
|
||
}
|
||
}
|
||
}
|
||
return guarded
|
||
}
|
||
|
||
export function scanUrlOutlets(code: string, file: string, globallyGuarded: Set<string> = new Set()): OutletScan {
|
||
const { script, lineOffset } = extractScript(code, file)
|
||
const sf = parseModule(script, file)
|
||
const guarded = guardedNamesOf(collectCallGraph(sf))
|
||
const outlets: UrlOutlet[] = []
|
||
walk(sf, (node: any) => {
|
||
const kind = outletKindOf(node)
|
||
if (!kind) return
|
||
const hosts = hostChainOf(node)
|
||
// 出口本身就是把守点的情况(`saveUrlWithProgress(...)` 这类收口函数):
|
||
// 被调函数在别的模块里先校验再用地址,调用方不必重复判
|
||
const calleeGuarded = ts.isCallExpression(node) && globallyGuarded.has(calledName(node))
|
||
outlets.push({
|
||
file,
|
||
line: sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1 + lineOffset,
|
||
kind,
|
||
host: hosts[0] ?? '(模块顶层)',
|
||
snippet: script.slice(node.getStart(sf), node.getEnd()).replace(/\s+/g, ' ').trim().slice(0, 90),
|
||
guarded: calleeGuarded || hosts.some((host) => guarded.has(host)),
|
||
})
|
||
})
|
||
return { file, outlets, unguarded: outlets.filter((outlet) => !outlet.guarded) }
|
||
}
|
||
|
||
/** 一个文件里"导出、且自己调过白名单"的函数名(跨模块把守点)。 */
|
||
export function exportedGuardedNames(code: string, file: string): string[] {
|
||
const { script } = extractScript(code, file)
|
||
const sf = parseModule(script, file)
|
||
const graph = collectCallGraph(sf)
|
||
const locallyGuarded = guardedNamesOf(graph)
|
||
const exported = new Set<string>()
|
||
walk(sf, (node: any) => {
|
||
if (!(ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export)) return
|
||
if (ts.isVariableStatement(node)) {
|
||
for (const decl of node.declarationList.declarations) {
|
||
if (ts.isIdentifier(decl.name) && decl.initializer && isFunctionLike(decl.initializer)) exported.add(decl.name.text)
|
||
}
|
||
return
|
||
}
|
||
if (!isFunctionLike(node)) return
|
||
const name = functionName(node)
|
||
if (name) exported.add(name)
|
||
})
|
||
return [...exported].filter(
|
||
(name) => locallyGuarded.has(name) && (graph.get(name) ?? []).some((call) => GUARD_NAMES.has(call)),
|
||
)
|
||
}
|
||
|
||
/** 全库两遍:先把各模块的把守函数收上来,再判每个出口。 */
|
||
export function scanAllUrlOutlets(sources: Array<{ file: string; code: string }>): OutletScan[] {
|
||
const globallyGuarded = new Set<string>()
|
||
for (const { file, code } of sources) {
|
||
for (const name of exportedGuardedNames(code, file)) globallyGuarded.add(name)
|
||
}
|
||
return sources.map(({ file, code }) => scanUrlOutlets(code, file, globallyGuarded))
|
||
}
|