dsh-git-push 2.20.18 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/cli-tool.mjs +2 -4
- package/cli.mjs +36 -2
- package/lib/app/apply.js +9 -9
- package/lib/app/command-registry.js +44 -20
- package/lib/app/handlers/meta.js +7 -142
- package/lib/app/handlers/npm.js +18 -1
- package/lib/app/handlers/preprocess.js +107 -0
- package/lib/app/handlers/repo-actions.js +2 -2
- package/lib/app/http-handlers.js +25 -12
- package/lib/app/index.js +2 -1
- package/lib/app/inject-text.js +3 -2
- package/lib/app/preprocess-client.js +136 -0
- package/lib/app/repo-ownership.js +1 -1
- package/lib/app/schema.js +3 -3
- package/lib/app/severity.js +14 -0
- package/lib/app/slash-commands.js +5 -164
- package/lib/app/tool-call.js +75 -140
- package/lib/app/tools.js +13 -6
- package/lib/cli/commands-preprocess.mjs +74 -0
- package/lib/cli/commands-tool.mjs +3 -2
- package/lib/cli/commands-vcs.mjs +4 -4
- package/lib/cli/shared.mjs +42 -13
- package/lib/client-parts/git/controller.js +2 -2
- package/lib/client.js +2 -2
- package/lib/commit-push/pre-commit-engine.js +57 -10
- package/lib/commit-push.js +125 -36
- package/lib/{exempt/index.js → git/exempt-markers.js} +8 -1
- package/lib/git/ignore.js +1 -1
- package/lib/git/repos.js +2 -2
- package/lib/git/sensitive.js +1 -1
- package/lib/npm/publish.js +20 -5
- package/lib/plugin/auto-push.js +2 -2
- package/lib/preprocess/providers.js +191 -0
- package/lib/self/index.js +1 -1
- package/lib/skip-dirs.js +14 -55
- package/package.json +29 -37
- package/scripts/build-client.mjs +2 -0
- package/scripts/check.mjs +4 -2
- package/scripts/local-registry.mjs +2 -1
- package/skills/dev/dsh-git-push.md +5 -0
- package/cli-audit.mjs +0 -141
- package/lib/app/audit-api.js +0 -173
- package/lib/app/host-doc-checks.js +0 -25
- package/lib/app/scope-check.js +0 -69
- package/lib/ast/brace.js +0 -120
- package/lib/ast/callgraph.js +0 -135
- package/lib/ast/code-lines.js +0 -175
- package/lib/ast/consts.js +0 -12
- package/lib/ast/control-flow.js +0 -470
- package/lib/ast/credential.js +0 -315
- package/lib/ast/dataflow.js +0 -391
- package/lib/ast/dup-code.js +0 -165
- package/lib/ast/fs-hotspots.js +0 -154
- package/lib/ast/index.js +0 -38
- package/lib/ast/io-risk-const.js +0 -95
- package/lib/ast/io-risk-fn.js +0 -208
- package/lib/ast/io-risk-loop.js +0 -308
- package/lib/ast/io-risk.js +0 -314
- package/lib/ast/lang.js +0 -156
- package/lib/ast/line-count.js +0 -52
- package/lib/ast/magic-number.js +0 -272
- package/lib/ast/naming.js +0 -292
- package/lib/ast/perf.js +0 -488
- package/lib/ast/scope-collisions.js +0 -228
- package/lib/ast/scope.js +0 -241
- package/lib/ast/shell.js +0 -131
- package/lib/ast/size.js +0 -448
- package/lib/ast/symbol-index.js +0 -399
- package/lib/ast/tokenizer.js +0 -421
- package/lib/ast/treesitter.js +0 -413
- package/lib/audit/analysis-coverage.js +0 -98
- package/lib/audit/audit-file.js +0 -205
- package/lib/audit/auditignore-rules.js +0 -148
- package/lib/audit/checks.js +0 -17
- package/lib/audit/client-entry.js +0 -93
- package/lib/audit/collector.js +0 -610
- package/lib/audit/ext-runner.js +0 -80
- package/lib/audit/file-context.js +0 -391
- package/lib/audit/finding.js +0 -101
- package/lib/audit/gitignore-match.js +0 -113
- package/lib/audit/glob.js +0 -63
- package/lib/audit/history-report.js +0 -100
- package/lib/audit/history.js +0 -126
- package/lib/audit/ignore-blind.js +0 -102
- package/lib/audit/index.js +0 -33
- package/lib/audit/orchestrate.js +0 -672
- package/lib/audit/public.js +0 -24
- package/lib/audit/repo-level.js +0 -30
- package/lib/audit/report-yaml.js +0 -90
- package/lib/audit/scope-policy.js +0 -110
- package/lib/audit/slot.js +0 -66
- package/lib/audit-rules/audit-rules-comment.yml +0 -199
- package/lib/audit-rules/audit-rules-cpp.yml +0 -114
- package/lib/audit-rules/audit-rules-csharp.yml +0 -82
- package/lib/audit-rules/audit-rules-docs.yml +0 -32
- package/lib/audit-rules/audit-rules-dsh.yml +0 -177
- package/lib/audit-rules/audit-rules-filehealth.yml +0 -42
- package/lib/audit-rules/audit-rules-folder.yml +0 -99
- package/lib/audit-rules/audit-rules-frontend.yml +0 -335
- package/lib/audit-rules/audit-rules-go.yml +0 -103
- package/lib/audit-rules/audit-rules-i18n.yml +0 -45
- package/lib/audit-rules/audit-rules-java.yml +0 -105
- package/lib/audit-rules/audit-rules-nodejs.yml +0 -662
- package/lib/audit-rules/audit-rules-npm.yml +0 -187
- package/lib/audit-rules/audit-rules-paths.yml +0 -98
- package/lib/audit-rules/audit-rules-performance.yml +0 -245
- package/lib/audit-rules/audit-rules-php.yml +0 -101
- package/lib/audit-rules/audit-rules-private.yml +0 -24
- package/lib/audit-rules/audit-rules-python.yml +0 -106
- package/lib/audit-rules/audit-rules-robustness.yml +0 -31
- package/lib/audit-rules/audit-rules-rust.yml +0 -109
- package/lib/audit-rules/audit-rules-structure.yml +0 -158
- package/lib/audit-rules/audit-rules-swift.yml +0 -99
- package/lib/audit-rules/audit-rules-template.yml +0 -108
- package/lib/audit-ui-sdk/contract.js +0 -117
- package/lib/audit-ui-sdk/index.js +0 -17
- package/lib/checks/button-bind.js +0 -347
- package/lib/checks/common.js +0 -170
- package/lib/checks/credential-file.js +0 -43
- package/lib/checks/dataflow.js +0 -40
- package/lib/checks/dispatch.js +0 -117
- package/lib/checks/dup-code.js +0 -69
- package/lib/checks/dup-const.js +0 -232
- package/lib/checks/file-health.js +0 -150
- package/lib/checks/filter.js +0 -137
- package/lib/checks/folder.js +0 -173
- package/lib/checks/fs-scanner.js +0 -57
- package/lib/checks/hardcoded-path.js +0 -98
- package/lib/checks/index.js +0 -49
- package/lib/checks/io.js +0 -66
- package/lib/checks/magic-number.js +0 -42
- package/lib/checks/npm-json.js +0 -273
- package/lib/checks/private.js +0 -64
- package/lib/checks/regex.js +0 -298
- package/lib/checks/scope-collision.js +0 -68
- package/lib/checks/semantic.js +0 -134
- package/lib/checks/structural.js +0 -281
- package/lib/cli/commands-audit.mjs +0 -142
- package/lib/cli/commands-doc.mjs +0 -127
- package/lib/client-parts/audit/audit.js +0 -301
- package/lib/exempt/tool.js +0 -198
- package/lib/git/module-splitter.js +0 -47
- package/lib/link-check/index.js +0 -187
- package/lib/lint-adapter/detect.js +0 -161
- package/lib/lint-adapter/index.js +0 -50
- package/lib/lint-adapter/runner.js +0 -133
- package/lib/rule/compilers/credential.js +0 -62
- package/lib/rule/compilers/dataflow.js +0 -38
- package/lib/rule/compilers/file-health.js +0 -75
- package/lib/rule/compilers/frontend.js +0 -86
- package/lib/rule/compilers/func.js +0 -35
- package/lib/rule/compilers/helpers.js +0 -71
- package/lib/rule/compilers/index.js +0 -29
- package/lib/rule/compilers/link-semantic.js +0 -45
- package/lib/rule/compilers/list-kinds.js +0 -12
- package/lib/rule/compilers/numeric-kinds.js +0 -73
- package/lib/rule/compilers/regex.js +0 -78
- package/lib/rule/compilers/structure.js +0 -107
- package/lib/rule/compilers.js +0 -17
- package/lib/rule/homoglyph.js +0 -45
- package/lib/rule/loader.js +0 -274
- package/lib/rule/registry.js +0 -122
- package/lib/rule/scope.js +0 -67
- package/lib/score/docs-score.js +0 -166
- package/lib/score/index.js +0 -234
- package/scripts/__pycache__/module-splitter.cpython-38.pyc +0 -0
- package/scripts/audit-ext/_example-readme-present.mjs +0 -34
- package/scripts/audit-ext/dsh-inject-declarations.mjs +0 -209
- package/scripts/audit-ext/variable-min-length.mjs +0 -102
- package/scripts/audit-gate.mjs +0 -235
- package/scripts/audit-runner.mjs +0 -35
- package/scripts/audit-runtime-check.mjs +0 -129
- package/scripts/browser-page-probe.mjs +0 -271
- package/scripts/check-js-scope.mjs +0 -87
- package/scripts/clean-date-comments.py +0 -102
- package/scripts/cpu-sample.mjs +0 -188
- package/scripts/doc-drift.mjs +0 -200
- package/scripts/hot-path-probe-cjs.cjs +0 -71
- package/scripts/hot-path-probe-fs-shim.mjs +0 -28
- package/scripts/hot-path-probe-fsp-shim.mjs +0 -31
- package/scripts/hot-path-probe.mjs +0 -153
- package/scripts/module-splitter.py +0 -800
- package/scripts/picker-repro.mjs +0 -115
- package/scripts/platform-scan.mjs +0 -168
- package/scripts/probe-recheck.mjs +0 -47
- package/scripts/rename-locator.mjs +0 -97
- package/scripts/rule-switch.mjs +0 -97
- package/scripts/rules-solo-audit.mjs +0 -173
- package/scripts/scan-file-io.mjs +0 -717
- package/scripts/scan-fs-hotspots.mjs +0 -107
- package/scripts/symbol-index.mjs +0 -85
- package/scripts/verify-prestep.mjs +0 -100
- package/skills/dev/audit-rule-authoring.md +0 -58
- package/skills/guide/guide-audit.md +0 -270
- /package/lib/{audit → app}/ownership.js +0 -0
- /package/lib/app/{schema-audit.js → schema-preprocess.js} +0 -0
- /package/lib/{audit-defaults.js → git/defaults.js} +0 -0
package/lib/app/scope-check.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 「拼接产物作用域检查」的**共享执行器**(工具 / 独立脚本 / 审计规则共用一份判定)。
|
|
3
|
-
*
|
|
4
|
-
* 为什么要有这一层:同一件事有三个入口(`scope_check` 工具、`scripts/check-js-scope.mjs`、
|
|
5
|
-
* 审计规则 kind=scope-collision),执行流程一旦各写一份就会漂移。这里只做
|
|
6
|
-
* 「找契约 → 逐文件分析 → 汇总」,真正的判定在 `lib/ast/scope-collisions.js`,
|
|
7
|
-
* 「按文件所属项目取契约」在 `lib/audit/scope-policy.js`(中立模块,审计侧也用它)。
|
|
8
|
-
*
|
|
9
|
-
* 契约适用口径(与审计侧**故意不同**,各有各的输入面):
|
|
10
|
-
* · 本执行器:目标文件是调用方**显式点名**的 ⇒ 视为产物;项目给了 `files` 清单
|
|
11
|
-
* 且不含该文件时仍不套契约(典型场景:分片源码不是产物,套前缀会把局部变量
|
|
12
|
-
* 全判成缺前缀)。
|
|
13
|
-
* · 审计侧(lib/checks/scope-collision.js):那是**全仓扫描**,必须严格按
|
|
14
|
-
* `files` 清单命中,否则每个 js 文件都会被套上前缀契约。
|
|
15
|
-
*/
|
|
16
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
17
|
-
import { resolve } from 'node:path';
|
|
18
|
-
|
|
19
|
-
import { analyzeScopeCollisions } from '../ast/scope-collisions.js';
|
|
20
|
-
import { POLICY_SOURCE, readProjectScopePolicy } from '../audit/scope-policy.js';
|
|
21
|
-
|
|
22
|
-
export { POLICY_SOURCE };
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 找出某个文件所属项目的契约配置(兼容旧名,实现在中立模块)。
|
|
26
|
-
* @param {string} file 目标文件绝对路径
|
|
27
|
-
*/
|
|
28
|
-
export function policyForFile(file) {
|
|
29
|
-
return readProjectScopePolicy(file);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 逐文件跑检查。
|
|
34
|
-
* @param {{files:string[], prefix?:string, allowlist?:string[], topLevelMustBeEmpty?:boolean}} o
|
|
35
|
-
* prefix/allowlist/topLevelMustBeEmpty 显式给出时覆盖项目配置(顶层零声明需显式 true/false)。
|
|
36
|
-
* @returns {{ok:boolean, reports:Array<object>, policySources:string[]}}
|
|
37
|
-
*/
|
|
38
|
-
export function runScopeCheck({ files = [], prefix = '', allowlist = [], topLevelMustBeEmpty } = {}) {
|
|
39
|
-
const reports = [];
|
|
40
|
-
const policySources = new Set();
|
|
41
|
-
for (const raw of files) {
|
|
42
|
-
const file = resolve(raw);
|
|
43
|
-
const own = readProjectScopePolicy(file);
|
|
44
|
-
// 项目没给 files 清单时,点名文件即产物;给了清单就只认清单内的文件
|
|
45
|
-
const contractApplies = own.declared && (own.files.length === 0 || own.isDeclaredArtifact);
|
|
46
|
-
const explicit = [];
|
|
47
|
-
if (prefix) explicit.push('prefix');
|
|
48
|
-
if (Array.isArray(allowlist) && allowlist.length) explicit.push('allowlist');
|
|
49
|
-
if (typeof topLevelMustBeEmpty === 'boolean') explicit.push('topLevel');
|
|
50
|
-
const policy = {
|
|
51
|
-
prefix: prefix || (contractApplies ? own.prefix : ''),
|
|
52
|
-
allowlist: Array.isArray(allowlist) && allowlist.length ? allowlist : (contractApplies ? own.allowlist : []),
|
|
53
|
-
topLevelMustBeEmpty: typeof topLevelMustBeEmpty === 'boolean'
|
|
54
|
-
? topLevelMustBeEmpty
|
|
55
|
-
: (contractApplies && own.topLevelMustBeEmpty),
|
|
56
|
-
};
|
|
57
|
-
const source = explicit.length
|
|
58
|
-
? `${POLICY_SOURCE.CLI}(${explicit.join('/')})`
|
|
59
|
-
: `${own.source}${own.configPath ? `:${own.configPath}` : ''}`;
|
|
60
|
-
policySources.add(source);
|
|
61
|
-
if (!existsSync(file)) {
|
|
62
|
-
reports.push({ file, ok: false, missing: true, violations: [`文件不存在:${file}`], declarations: [], duplicates: [] });
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
const result = analyzeScopeCollisions(readFileSync(file, 'utf8'), policy);
|
|
66
|
-
reports.push({ file, ...result, policy });
|
|
67
|
-
}
|
|
68
|
-
return { ok: reports.every((r) => r.ok), reports, policySources: [...policySources] };
|
|
69
|
-
}
|
package/lib/ast/brace.js
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AST 实现层 · 括号与区间工具
|
|
3
|
-
*
|
|
4
|
-
* 职责:括号配对、区间包含判定。供复杂度/嵌套/函数行数检查复用,
|
|
5
|
-
* 让「某个 token 属于哪个函数体」这类判断只在一处实现。
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
// 从 function 关键字向后找函数体 '{' 的最大 token 跨度:与 control-flow.js 共用同一来源
|
|
9
|
-
// (lib/ast/consts.js)——两处必须永远相等(自审 duplicate-constant 报的真重复)
|
|
10
|
-
import { FN_BODY_LOOKAHEAD } from './consts.js';
|
|
11
|
-
|
|
12
|
-
/** 括号平衡扫描:从 startIdx 的 '{' 找匹配 '}',返回 [openIdx, closeIdx] 或 null。 */
|
|
13
|
-
export function matchBrace(tokens, openIdx) {
|
|
14
|
-
let depth = 0;
|
|
15
|
-
for (let i = openIdx; i < tokens.length; i++) {
|
|
16
|
-
const t = tokens[i];
|
|
17
|
-
if (t.type !== 'punct') continue;
|
|
18
|
-
if (t.value === '{') depth++;
|
|
19
|
-
else if (t.value === '}') {
|
|
20
|
-
depth--;
|
|
21
|
-
if (depth === 0) return [openIdx, i];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** 前向圆括号配对:从 startIdx 的 '(' 找匹配 ')',返回其下标;不匹配返回 -1。 */
|
|
28
|
-
export function matchParenForward(tokens, startIdx) {
|
|
29
|
-
let depth = 0;
|
|
30
|
-
for (let i = startIdx; i < tokens.length; i++) {
|
|
31
|
-
const t = tokens[i];
|
|
32
|
-
if (t.type !== 'punct') continue;
|
|
33
|
-
if (t.value === '(') depth++;
|
|
34
|
-
else if (t.value === ')') {
|
|
35
|
-
depth--;
|
|
36
|
-
if (depth === 0) return i;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return -1;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 从 closeIdx 处 closePunct 向右→左配对,返回 openPunct 下标;不匹配返回 -1。
|
|
44
|
-
* 抽公共:原 matchingOpen({})/ matchingBracketBack([])/ matchingParenBack(())
|
|
45
|
-
* + isBlockParen 的配对循环是同一逻辑四处复制,统一为本函数(dup-code 规则命中案例)。
|
|
46
|
-
* @param {Array} tokens token 流
|
|
47
|
-
* @param {number} closeIdx 右括号下标
|
|
48
|
-
* @param {string} openPunct 左括号字符('{' / '[' / '(')
|
|
49
|
-
* @param {string} closePunct 右括号字符('}' / ']' / ')')
|
|
50
|
-
* @returns {number} 配对左括号下标;无配对返回 -1
|
|
51
|
-
*/
|
|
52
|
-
export function matchPairBack(tokens, closeIdx, openPunct, closePunct) {
|
|
53
|
-
let level = 0;
|
|
54
|
-
for (let j = closeIdx; j >= 0; j--) {
|
|
55
|
-
const tj = tokens[j];
|
|
56
|
-
if (tj.type !== 'punct') continue;
|
|
57
|
-
if (tj.value === closePunct) level++;
|
|
58
|
-
else if (tj.value === openPunct) { level--; if (level === 0) return j; }
|
|
59
|
-
}
|
|
60
|
-
return -1;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 判断 `)` 是否属于控制流/函数签名的参数表尾(即其后 `{` 是块语句)。
|
|
65
|
-
* 从 `)` 向前做括号配对找起始 `(`,再看 `(` 前一个 token:
|
|
66
|
-
* if/for/while/switch/catch/function/标识符(函数声明/表达式/方法名) → 是块语句头。
|
|
67
|
-
* @param {Array} tokens token 流
|
|
68
|
-
* @param {object} closeTok `)` token
|
|
69
|
-
* @returns {boolean}
|
|
70
|
-
*/
|
|
71
|
-
export function isBlockParen(tokens, closeTok) {
|
|
72
|
-
const closeIdx = tokens.indexOf(closeTok);
|
|
73
|
-
const openIdx = matchPairBack(tokens, closeIdx, '(', ')');
|
|
74
|
-
if (openIdx === -1) return false;
|
|
75
|
-
for (let j = openIdx - 1; j >= 0; j--) {
|
|
76
|
-
const tj = tokens[j];
|
|
77
|
-
if (tj.type === 'ws' || tj.type === 'comment') continue;
|
|
78
|
-
if (tj.type === 'ident') return true; // if (…) / myFunc (…) / function f (…)
|
|
79
|
-
// `)` `]` `.` 等 → 调用表达式(如 foo.bar(…)),其后 `{` 多半是对象实参 → 不计
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** 找与 closeIdx 处 `}` 配对的 `{` 下标(无配对返回 -1)。 */
|
|
86
|
-
export function matchingOpen(tokens, closeIdx) {
|
|
87
|
-
return matchPairBack(tokens, closeIdx, '{', '}');
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* 检查圈复杂度(max-complexity kind,AST 级)。
|
|
92
|
-
* 复杂度 = 1 + 分支关键字数(if/for/while/case/catch/&&/||/?/??)。
|
|
93
|
-
* @param {string} text 文件全文
|
|
94
|
-
* @param {object} [opts] { warn=10, block=20 }
|
|
95
|
-
* @returns {Array<{line:number, name:string, complexity:number, level:string}>}
|
|
96
|
-
*/
|
|
97
|
-
/**
|
|
98
|
-
* 收集 [from, to) 区间内所有「嵌套 function 体」的 token 区间。
|
|
99
|
-
* 供圈复杂度计算排除内层贡献(内层由外层循环单独评估)。
|
|
100
|
-
*
|
|
101
|
-
* @param {Array} tokens token 流
|
|
102
|
-
* @param {number} from 起始下标(含)
|
|
103
|
-
* @param {number} to 结束下标(不含)
|
|
104
|
-
* @returns {Array<[number, number]>} 各内层函数体的 [openBraceIdx, closeBraceIdx]
|
|
105
|
-
*/
|
|
106
|
-
export function collectInnerFnRanges(tokens, from, to) {
|
|
107
|
-
const ranges = [];
|
|
108
|
-
for (let i = from; i < to; i++) {
|
|
109
|
-
const t = tokens[i];
|
|
110
|
-
if (!(t.type === 'ident' && t.value === 'function')) continue;
|
|
111
|
-
let openIdx = -1;
|
|
112
|
-
for (let j = i + 1; j < Math.min(to, i + FN_BODY_LOOKAHEAD); j++) {
|
|
113
|
-
if (tokens[j].type === 'punct' && tokens[j].value === '{') { openIdx = j; break; }
|
|
114
|
-
}
|
|
115
|
-
if (openIdx === -1) continue;
|
|
116
|
-
const r = matchBrace(tokens, openIdx);
|
|
117
|
-
if (r) ranges.push(r);
|
|
118
|
-
}
|
|
119
|
-
return ranges;
|
|
120
|
-
}
|
package/lib/ast/callgraph.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 调用链追踪(方案 Step 5/P3)——单文件调用图
|
|
3
|
-
*
|
|
4
|
-
* 用途:io-risk 等规则的「请求路径」判定升级——不只靠函数体内特征(detectRequestContext),
|
|
5
|
-
* 还看**调用链**:被请求路径函数调用的函数,也在请求路径上。
|
|
6
|
-
*
|
|
7
|
-
* 局限(按方案标记「需人工复核」不强行判断):跨文件调用、动态调用 obj[method]()、
|
|
8
|
-
* 回调传递断链。
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { tokenize } from './tokenizer.js';
|
|
12
|
-
|
|
13
|
-
/** 请求路径函数名模式(子串匹配,与 io-risk 的 REQUEST_PATTERNS 语义互补)。 */
|
|
14
|
-
const REQUEST_PATH_NAMES = ['handle', 'request', 'route', 'onrequest', 'middleware', 'api', 'controller', 'service'];
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 构建单文件调用图:calleeName → Set(callerName)(调用者列表)。
|
|
18
|
-
* 只统计「具名函数体内」的具名调用(ident(...) 且被调名是 ident),
|
|
19
|
-
* 动态调用(obj[method](...) / 匿名回调)天然落不进图——按盲区处理。
|
|
20
|
-
* @param {string} text 文件全文
|
|
21
|
-
* @returns {Map<string, Set<string>>} callee → callers
|
|
22
|
-
*/
|
|
23
|
-
export function buildCallGraph(text) {
|
|
24
|
-
const tokens = tokenize(text);
|
|
25
|
-
const graph = new Map();
|
|
26
|
-
// 函数边界(名称 → [startIdx, endIdx]):function NAME / const NAME = function / NAME = (...) => / NAME(...) { }
|
|
27
|
-
const fnRanges = collectNamedFnRanges(tokens);
|
|
28
|
-
for (const [fnName, [startIdx, endIdx]] of fnRanges) {
|
|
29
|
-
for (let i = startIdx; i < endIdx; i++) {
|
|
30
|
-
const t = tokens[i];
|
|
31
|
-
if (t.type !== 'ident') continue;
|
|
32
|
-
const nx = tokens[i + 1];
|
|
33
|
-
if (!(nx && nx.type === 'punct' && nx.value === '(')) continue;
|
|
34
|
-
// 排除函数自身声明(function NAME(处的 NAME 后是 '(' 但那是声明)——fnName 的声明 token 不在本函数体前
|
|
35
|
-
if (t.value === fnName && tokens[i - 1] && tokens[i - 1].type === 'ident' && ['function', 'const', 'let', 'var'].includes(tokens[i - 1].value)) continue;
|
|
36
|
-
const callee = t.value;
|
|
37
|
-
if (!graph.has(callee)) graph.set(callee, new Set());
|
|
38
|
-
graph.get(callee).add(fnName);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return graph;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 判定函数是否在请求路径上(调用链向上追溯:本函数被某请求路径函数调用,或
|
|
46
|
-
* 该调用者又被请求路径函数调用……)。向上遍历防环。
|
|
47
|
-
* @param {string} fnName 目标函数名
|
|
48
|
-
* @param {Map<string, Set<string>>} graph buildCallGraph 结果
|
|
49
|
-
* @param {(name:string)=>boolean} [isRequestName] 请求路径函数名判定(缺省 REQUEST_PATH_NAMES)
|
|
50
|
-
* @returns {boolean}
|
|
51
|
-
*/
|
|
52
|
-
export function isInRequestPath(fnName, graph, isRequestName) {
|
|
53
|
-
const isReq = isRequestName || ((n) => REQUEST_PATH_NAMES.some((p) => String(n).toLowerCase().includes(p)));
|
|
54
|
-
const visited = new Set();
|
|
55
|
-
const stack = [fnName];
|
|
56
|
-
while (stack.length) {
|
|
57
|
-
const cur = stack.pop();
|
|
58
|
-
if (visited.has(cur)) continue;
|
|
59
|
-
visited.add(cur);
|
|
60
|
-
const callers = graph.get(cur);
|
|
61
|
-
if (!callers) continue;
|
|
62
|
-
for (const caller of callers) {
|
|
63
|
-
if (isReq(caller)) return true;
|
|
64
|
-
stack.push(caller);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** ① function NAME(...) { ... } 声明收集(找函数体 `{` 限 24 token)。 */
|
|
71
|
-
function collectFunctionDecls(tokens) {
|
|
72
|
-
const out = [];
|
|
73
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
74
|
-
const t = tokens[i];
|
|
75
|
-
if (!(t.type === 'ident' && t.value === 'function')) continue;
|
|
76
|
-
let j = i + 1;
|
|
77
|
-
let name = '(匿名)';
|
|
78
|
-
if (tokens[j] && tokens[j].type === 'ident') { name = tokens[j].value; j += 1; }
|
|
79
|
-
// 找函数体 {(限 24 token)
|
|
80
|
-
let openIdx = -1;
|
|
81
|
-
for (let k = j; k < Math.min(tokens.length, j + 24); k++) {
|
|
82
|
-
if (tokens[k].type === 'punct' && tokens[k].value === '{') { openIdx = k; break; }
|
|
83
|
-
if (tokens[k].type === 'punct' && tokens[k].value === ';') break;
|
|
84
|
-
}
|
|
85
|
-
if (openIdx === -1) continue;
|
|
86
|
-
const closeIdx = matchBraceIdx(tokens, openIdx);
|
|
87
|
-
if (closeIdx > openIdx) { if (name !== '(匿名)') out.push([name, [openIdx, closeIdx]]); i = closeIdx; }
|
|
88
|
-
}
|
|
89
|
-
return out;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** ② const NAME = (...) => { } 箭头函数收集(找 => 后第一个 { 限 30 token)。 */
|
|
93
|
-
function collectArrowFns(tokens) {
|
|
94
|
-
const out = [];
|
|
95
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
96
|
-
const t = tokens[i];
|
|
97
|
-
if (!(t.type === 'ident' && ['const', 'let', 'var'].includes(t.value))) continue;
|
|
98
|
-
const name = tokens[i + 1];
|
|
99
|
-
if (!(name && name.type === 'ident')) continue;
|
|
100
|
-
// 向后找 => 后第一个 {(限 30 token)
|
|
101
|
-
for (let j = i + 2; j < Math.min(tokens.length, i + 30); j++) {
|
|
102
|
-
if (tokens[j].type === 'punct' && tokens[j].value === '=>') {
|
|
103
|
-
let openIdx = -1;
|
|
104
|
-
for (let k = j + 1; k < Math.min(tokens.length, j + 24); k++) {
|
|
105
|
-
if (tokens[k].type === 'punct' && tokens[k].value === '{') { openIdx = k; break; }
|
|
106
|
-
}
|
|
107
|
-
if (openIdx !== -1) {
|
|
108
|
-
const closeIdx = matchBraceIdx(tokens, openIdx);
|
|
109
|
-
if (closeIdx > openIdx) out.push([name.value, [openIdx, closeIdx]]);
|
|
110
|
-
}
|
|
111
|
-
break;
|
|
112
|
-
}
|
|
113
|
-
if (tokens[j].type === 'punct' && tokens[j].value === ';') break;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
return out;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** 收集具名函数体 token 区间:[name, [startIdx, endIdx]](function NAME / const NAME = (...) => {} / NAME = function)。 */
|
|
120
|
-
export function collectNamedFnRanges(tokens) {
|
|
121
|
-
return [...collectFunctionDecls(tokens), ...collectArrowFns(tokens)];
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/** 从 openIdx(应为 `{`)找配对闭括号下标(无配对返回 openIdx)。 */
|
|
125
|
-
function matchBraceIdx(tokens, openIdx) {
|
|
126
|
-
let depth = 0;
|
|
127
|
-
for (let j = openIdx; j < tokens.length; j++) {
|
|
128
|
-
if (tokens[j].type === 'punct' && tokens[j].value === '{') depth++;
|
|
129
|
-
else if (tokens[j].type === 'punct' && tokens[j].value === '}') {
|
|
130
|
-
depth--;
|
|
131
|
-
if (depth === 0) return j;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return openIdx;
|
|
135
|
-
}
|
package/lib/ast/code-lines.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AST 实现层 · 代码行判定
|
|
3
|
-
*
|
|
4
|
-
* 职责:判断某行是否「真在代码里」(而非注释/字符串内),并取出代码里的字符串字面量。
|
|
5
|
-
* 供 regex 类规则做二次确认,避免注释中的示例被当真实代码报错。
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { tokenize } from './tokenizer.js';
|
|
9
|
-
|
|
10
|
-
/* ───────────────────────── 正则初筛 → AST 精筛(通用设施) ───────────────────────── */
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 正则初筛 → token 级精筛(所有文本型检查的通用两段式)。
|
|
14
|
-
*
|
|
15
|
-
* 为什么两段式:正则快、能一次筛出候选行;但正则无法区分「代码」与「注释/字符串」,
|
|
16
|
-
* 单用必然误报(注释版本号、CSS 字号、i18n 字典值)。tokenizer 能精确分类,但全量
|
|
17
|
-
* token 化成本高于正则。两段式 = 正则初筛候选行 → 只对候选行做 token 判定。
|
|
18
|
-
*
|
|
19
|
-
* @param {string} text 文件全文
|
|
20
|
-
* @param {number[]} candidateLines 正则初筛命中的行号(1-based)
|
|
21
|
-
* @returns {{isCode: (line:number)=>boolean}} isCode(line) 判定该行是否含「代码 token」
|
|
22
|
-
* (注释/字符串/模板串/纯空白行 → false;含 ident/num/punct 的行 → true)
|
|
23
|
-
*/
|
|
24
|
-
export function makeCodeLineFilter(text = '', candidateLines = [], opts = {}) {
|
|
25
|
-
// 散文体文档(markdown/txt):tokenizer 会把自然语言词判成 ident
|
|
26
|
-
// (「CIFS」「挂载适配」都是 ident),于是整段散文被当成「含代码 token 的行」,
|
|
27
|
-
// astConfirm 形同虚设——实测 README 版本记录里说明「token 被误报」的那句话
|
|
28
|
-
// 本身被 security/no-hardcoded-credentials 报成 blocker。
|
|
29
|
-
// 故对这类文件改用文档语义:只有**围栏代码块内**与**行内代码**才算代码。
|
|
30
|
-
if (isProseDoc(text, opts.file)) return makeProseCodeFilter(text, candidateLines);
|
|
31
|
-
|
|
32
|
-
const want = new Set(candidateLines);
|
|
33
|
-
const codeLines = new Set();
|
|
34
|
-
const suspect = new Set();
|
|
35
|
-
for (const t of tokenize(text)) {
|
|
36
|
-
if (!want.has(t.line)) continue;
|
|
37
|
-
if (t.type === 'ident' || t.type === 'num') codeLines.add(t.line);
|
|
38
|
-
if (t.type === 'punct') suspect.add(t.line);
|
|
39
|
-
}
|
|
40
|
-
for (const lineNo of suspect) {
|
|
41
|
-
// 只有标点(如 `}` `);`)不算代码行,需同时有 ident/num
|
|
42
|
-
if (!codeLines.has(lineNo)) codeLines.delete(lineNo);
|
|
43
|
-
}
|
|
44
|
-
return {
|
|
45
|
-
isCode: (line) => codeLines.has(line),
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/* ───────────────────────── 列级精筛(行级判定的加强版) ───────────────────────── */
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* 正则初筛 → **列级** token 精筛。
|
|
53
|
-
*
|
|
54
|
-
* 为什么需要:行级判定只能回答「这一行有没有代码 token」,回答不了
|
|
55
|
-
* 「正则命中的**那一处**是不是落在字符串/注释里」。实测遗留误报:
|
|
56
|
-
*
|
|
57
|
-
* const hang = await provider.execute(session, { script: 'while (true) {}' });
|
|
58
|
-
*
|
|
59
|
-
* 该行确实含 ident(script / provider 等)⇒ 行级判定放行 ⇒
|
|
60
|
-
* performance/busy-wait-loop 把字符串字面量里的 while(true) 当成真实死循环报出 ✗。
|
|
61
|
-
*
|
|
62
|
-
* 做法:在行级判定之上,用 tokenizer 的 str/tmpl/comment token 构造「不透明区间」,
|
|
63
|
-
* 命中列落在区间内 ⇒ 判为「不是代码」。
|
|
64
|
-
*
|
|
65
|
-
* @param {string} text 文件全文
|
|
66
|
-
* @param {number[]} candidateLines 候选行(1-based)
|
|
67
|
-
* @param {{file?:string}} [opts]
|
|
68
|
-
* @returns {{isCode:(line:number)=>boolean, isCodeHit:(line:number, col:number)=>boolean}}
|
|
69
|
-
* isCodeHit 的 col 为 **1-based 列**(正则 exec 的 index + 1);
|
|
70
|
-
* 列缺失/非法时**退化为行级判定**(宁可放行,不因拿不到列而误杀)。
|
|
71
|
-
*/
|
|
72
|
-
export function makeCodeSpanFilter(text = '', candidateLines = [], opts = {}) {
|
|
73
|
-
const base = makeCodeLineFilter(text, candidateLines, opts);
|
|
74
|
-
// 不透明区间只取**单行 token**:跨行 token(三引号/多行块注释)的 col 是续行内位置,
|
|
75
|
-
// 用它构造区间会算错。跨行场景本就由行级判定覆盖——续行不产 ident/num ⇒ 不算代码行。
|
|
76
|
-
const opaque = new Map(); // line → [[startCol, endColExclusive), ...]
|
|
77
|
-
// onlyComment:只把**注释**当不透明区间,字符串仍算代码。
|
|
78
|
-
// 为什么需要这一档:paths/凭据类规则的命中本来就写在字符串里(路径字面量),
|
|
79
|
-
// 若把字符串也一并当「非代码」会把这些规则整体废掉(实测 hardcoded-absolute-path 5→0);
|
|
80
|
-
// 而「注释里的示例代码被当真」是纯误报,两者必须分开处理。
|
|
81
|
-
const opaqueTypes = opts.onlyComment ? new Set(['comment']) : new Set(['str', 'tmpl', 'comment']);
|
|
82
|
-
for (const t of tokenize(text)) {
|
|
83
|
-
if (!opaqueTypes.has(t.type)) continue;
|
|
84
|
-
const v = String(t.value);
|
|
85
|
-
if (v.includes('\n')) continue;
|
|
86
|
-
const col = Number(t.col);
|
|
87
|
-
if (!Number.isFinite(col) || col < 1) continue;
|
|
88
|
-
const arr = opaque.get(t.line) || [];
|
|
89
|
-
arr.push([col, col + v.length]);
|
|
90
|
-
opaque.set(t.line, arr);
|
|
91
|
-
}
|
|
92
|
-
return {
|
|
93
|
-
isCode: base.isCode,
|
|
94
|
-
isCodeHit: (line, col) => {
|
|
95
|
-
// onlyComment(只判注释)**不走行级判定**:行级判定里编码了「散文体文档只有围栏代码算代码」
|
|
96
|
-
// 的语义,若沿用它,`contact: x@y.com` 这种普通文档行会被判非代码 ⇒ 所有正则规则
|
|
97
|
-
// 在 .md/.txt 上整体失效(实测:隐私规则端到端用例直接转红)。
|
|
98
|
-
// 这里只回答「这一列是否落在注释 token 区间内」,与文件体裁无关。
|
|
99
|
-
if (!opts.onlyComment && !base.isCode(line)) return false;
|
|
100
|
-
const ranges = opaque.get(line);
|
|
101
|
-
if (!ranges || !ranges.length) return true;
|
|
102
|
-
const c = Number(col);
|
|
103
|
-
if (!Number.isFinite(c) || c < 1) return true; // 拿不到列 ⇒ 退化为行级判定
|
|
104
|
-
for (const [s, e] of ranges) if (c >= s && c < e) return false;
|
|
105
|
-
return true;
|
|
106
|
-
},
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 是否散文体文档 —— **只按扩展名判定**。
|
|
112
|
-
*
|
|
113
|
-
* 刻意不做内容特征兜底:shell 注释 `# cd "$X"` 与 markdown 标题 `# 标题`
|
|
114
|
-
* 在文本层面完全同形,任何基于「以 # 开头」的启发式都会把 shell 脚本
|
|
115
|
-
* 误判为文档,进而让所有行按「非围栏、非行内代码」被剔除——实测使
|
|
116
|
-
* folder/cd-to-maybe-missing 规则在 .sh 上整体失效(回归测试 test 542 失败)。
|
|
117
|
-
* 调用方(lib/ast/shell.js)不传文件名,此时按代码处理是正确的默认。
|
|
118
|
-
* @param {string} text 文件全文(当前未使用,保留签名以备扩展)
|
|
119
|
-
* @param {string} file 文件路径(用于取扩展名)
|
|
120
|
-
* @returns {boolean}
|
|
121
|
-
*/
|
|
122
|
-
function isProseDoc(text, file) {
|
|
123
|
-
return /\.(md|markdown|txt|rst)$/i.test(String(file || ''));
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* 文档语义的代码行判定:只有围栏代码块内、以及**行内代码片段本身**算「代码」。
|
|
128
|
-
*
|
|
129
|
-
* 关键区别在行内代码:不能「该行有行内代码 → 整行放行」。文档里一行往往既有
|
|
130
|
-
* 散文又有多个行内片段(如版本记录一行写「...`chmod`/`utimes` 因 SMB 无 POSIX
|
|
131
|
-
* Extensions...`lib/fsx.js` 统一收口...」),整行放行等于把散文也当代码。
|
|
132
|
-
* 实测 README 说明「token 被误报」的那句话因此被报成 blocker。
|
|
133
|
-
* 做法:把该行的行内代码片段拼成「代码视图」,仅当代码视图里含 ident/num
|
|
134
|
-
* (即片段本身像代码)时才标为候选,供后续 token 级精筛继续判定。
|
|
135
|
-
*
|
|
136
|
-
* @returns {{isCode: (line:number)=>boolean}}
|
|
137
|
-
*/
|
|
138
|
-
function makeProseCodeFilter(text, candidateLines) {
|
|
139
|
-
const want = new Set(candidateLines);
|
|
140
|
-
const lines = String(text).split('\n');
|
|
141
|
-
const codeLines = new Set();
|
|
142
|
-
let inFence = false;
|
|
143
|
-
for (let i = 0; i < lines.length; i++) {
|
|
144
|
-
const line = lines[i];
|
|
145
|
-
if (/^\s*(```|~~~)/.test(line)) {
|
|
146
|
-
// 围栏标记行本身不算代码(它是文档语法)
|
|
147
|
-
inFence = !inFence;
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
if (inFence) { codeLines.add(i + 1); continue; }
|
|
151
|
-
if (!want.has(i + 1)) continue; // 只判候选行,省 token 化成本
|
|
152
|
-
// 取该行全部行内代码片段,判断「片段自身」是否像代码
|
|
153
|
-
const snippets = String(line).match(/`[^`]+`/g) || [];
|
|
154
|
-
if (!snippets.length) continue;
|
|
155
|
-
const snippetText = snippets.map((x) => x.slice(1, -1)).join('\n');
|
|
156
|
-
for (const t of tokenize(snippetText)) {
|
|
157
|
-
if (t.type === 'ident' || t.type === 'num' || t.type === 'str') { codeLines.add(i + 1); break; }
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return { isCode: (line) => codeLines.has(line) };
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* 判定一组字符串字面量值是否出现在代码里(供「重复硬编码串」等规则精筛)。
|
|
165
|
-
* @param {string} text 文件全文
|
|
166
|
-
* @returns {Set<string>} 代码中出现的字符串字面量裸值集合
|
|
167
|
-
*/
|
|
168
|
-
export function codeStringLiterals(text = '') {
|
|
169
|
-
const out = new Set();
|
|
170
|
-
for (const t of tokenize(text)) {
|
|
171
|
-
if (t.type !== 'str' && t.type !== 'tmpl') continue;
|
|
172
|
-
out.add(String(t.value).replace(/^(['"`])([\s\S]*)\1$/, '$2'));
|
|
173
|
-
}
|
|
174
|
-
return out;
|
|
175
|
-
}
|
package/lib/ast/consts.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// AST 判定层公共常量(单一来源)。
|
|
2
|
-
//
|
|
3
|
-
// 为什么单独成模块:同一常量此前在 lib/ast 的多个文件各写一份(如 FN_BODY_LOOKAHEAD 在
|
|
4
|
-
// brace.js 与 control-flow.js),两处必须永远相等——漏改一处会出现「括号匹配按 20 回看、
|
|
5
|
-
// 控制流按别的值回看」这类静默不一致(自审 maintainability/duplicate-constant-def 报的真重复)。
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 找函数体起始 `{` 时向后回看的 token 数。
|
|
9
|
-
* 函数签名可能很长(多行形参/默认值/泛型/装饰器),回看窗口太小会找不到 `{` 而漏判函数体,
|
|
10
|
-
* 太大则可能越过边界把下一个块的 `{` 当成函数体——20 是实测能覆盖本仓库全部形态的值。
|
|
11
|
-
*/
|
|
12
|
-
export const FN_BODY_LOOKAHEAD = 20;
|