dsh-git-push 2.20.19 → 3.1.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 +76 -47
- package/cli-tool.mjs +2 -4
- package/cli.mjs +11 -4
- package/lib/app/apply.js +15 -13
- package/lib/app/command-registry.js +26 -19
- package/lib/app/handlers/http-constants.js +8 -0
- package/lib/app/handlers/meta.js +2 -2
- package/lib/app/handlers/preprocess.js +104 -0
- package/lib/app/handlers/repo-actions.js +2 -3
- package/lib/app/handlers/repos.js +137 -10
- package/lib/app/handlers/settings.js +34 -15
- package/lib/app/http-handlers.js +13 -42
- package/lib/app/index.js +3 -2
- package/lib/app/inject-text.js +6 -21
- package/lib/app/preprocess-client.js +136 -0
- package/lib/app/repo-ownership.js +1 -1
- package/lib/app/schema.js +20 -10
- package/lib/app/settings-bridge.js +60 -8
- package/lib/app/severity.js +14 -0
- package/lib/app/slash-commands.js +5 -164
- package/lib/app/tool-call.js +96 -163
- package/lib/cli/commands-preprocess.mjs +74 -0
- package/lib/cli/commands-tool.mjs +3 -2
- package/lib/cli/commands-vcs.mjs +39 -5
- package/lib/cli/shared.mjs +36 -10
- package/lib/client/index.js +11 -16
- package/lib/client-parts/git/account.js +21 -12
- package/lib/client-parts/git/actions.js +23 -77
- package/lib/client-parts/git/apply.js +8 -2
- package/lib/client-parts/git/browser.js +1 -1
- package/lib/client-parts/git/const.js +33 -1
- package/lib/client-parts/git/controller.js +175 -176
- package/lib/client-parts/git/icons.js +1 -1
- package/lib/client-parts/git/loader.js +1 -1
- package/lib/client-parts/git/npm.js +56 -37
- package/lib/client-parts/git/page.js +53 -1
- package/lib/client-parts/git/repo.js +8 -1
- package/lib/client-parts/git/settings.js +13 -1
- package/lib/client-parts/git/state.js +237 -0
- package/lib/client-parts/git/utils.js +1 -1
- package/lib/client.js +631 -312
- package/lib/commit-push/pre-commit-engine.js +629 -30
- package/lib/commit-push.js +127 -67
- package/lib/context/index.js +7 -5
- package/lib/core/config.js +1 -27
- package/lib/core/exec.js +2 -11
- package/lib/{exempt/index.js → git/exempt-markers.js} +8 -1
- package/lib/git/identity-rewrite.js +1 -1
- package/lib/git/ignore.js +1 -1
- package/lib/git/index.js +1 -1
- package/lib/git/push.js +2 -17
- package/lib/git/repo-index.js +8 -3
- package/lib/git/repos.js +2 -2
- package/lib/git/sensitive.js +1 -1
- package/lib/npm/install.js +1 -1
- package/lib/npm/publish.js +20 -5
- package/lib/npm/registry.js +2 -2
- package/lib/plugin/auto-push.js +5 -8
- package/lib/preprocess/providers.js +195 -0
- package/lib/self/index.js +1 -1
- package/lib/skip-dirs.js +14 -55
- package/package.json +31 -42
- package/skills/dev/dsh-git-push.md +9 -9
- package/skills/guide/guide-commit-push.md +11 -13
- package/skills/workflow/README.md +0 -1
- package/skills/workflow/task-completion-report.md +2 -2
- package/.auditignore +0 -30
- package/.gitignore +0 -43
- package/cli-audit.mjs +0 -141
- package/lib/BUGFIX-NOTES-2026-09-14.md +0 -278
- package/lib/app/audit-api.js +0 -173
- package/lib/app/handlers/audit.js +0 -150
- package/lib/app/host-doc-checks.js +0 -25
- package/lib/app/schema-audit.js +0 -42
- 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/preprocess/audit.js +0 -98
- package/lib/preprocess/index.js +0 -78
- 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/lib/user-requirements.json +0 -15
- 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/build-client.mjs +0 -131
- package/scripts/bump-version.mjs +0 -178
- package/scripts/check-js-scope.mjs +0 -87
- package/scripts/check.mjs +0 -203
- package/scripts/clean-date-comments.py +0 -102
- package/scripts/cpu-sample.mjs +0 -188
- package/scripts/doc-drift.mjs +0 -200
- package/scripts/doc-func.mjs +0 -212
- package/scripts/doc-tree.mjs +0 -458
- package/scripts/doc-version.mjs +0 -149
- 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/local-registry.mjs +0 -37
- 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/scan-version.mjs +0 -125
- package/scripts/symbol-index.mjs +0 -85
- package/scripts/verify-prestep.mjs +0 -100
- package/scripts/version-source.mjs +0 -173
- 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/{audit-defaults.js → git/defaults.js} +0 -0
package/lib/ast/credential.js
DELETED
|
@@ -1,315 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AST 实现层 · 凭据检查
|
|
3
|
-
*
|
|
4
|
-
* 职责:凭据标识符右侧值判定(硬编码/引用/类型检查)、前缀型密钥串的占位符精筛。
|
|
5
|
-
*
|
|
6
|
-
* 占位符判据(CRED_PLACEHOLDER / CRED_SCHEME_PREFIX / isMeaningfulCredentialValue)
|
|
7
|
-
* 必须只有一份:两处检查共用,否则判据漂移会出现「同一串值 A 规则放行、
|
|
8
|
-
* B 规则拦截」的自相矛盾。
|
|
9
|
-
*
|
|
10
|
-
* 精筛双路径(2026-11-06 方向 E1「有树走树」):
|
|
11
|
-
* checkCredentialRefAst(text, tree) 传入语法树时走 credentialLinesFromTree——按真语法
|
|
12
|
-
* 节点判「赋值左值是不是凭据名、右值是不是纯字面量」,参数默认值(default_parameter /
|
|
13
|
-
* assignment_pattern)、复合右值(binary_operator)、三元(conditional_expression)、
|
|
14
|
-
* f-string 插值天然落空;无树/解析失败回退 scanCredentialTokens(token 级近似)。
|
|
15
|
-
* 回退是常态路径之一(D5 口径)——双向样本回归见 test/test-tree-first-credential.mjs。
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { tokenize } from './tokenizer.js';
|
|
19
|
-
import { credentialLinesFromTree } from './treesitter.js';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 检查「凭据类标识符是否真的被赋了字面量值」(no-hardcoded-credentials kind 的 AST 精筛)。
|
|
23
|
-
*
|
|
24
|
-
* 正则 `token\s*[=:]+\s*['"][^'"]+['"]` 命中即报,会把下列**非硬编码**场景误报:
|
|
25
|
-
* - 类型/存在性检查:`typeof cfg.githubToken === 'string'`、`next.apiKey.trim()`
|
|
26
|
-
* - 字段透传/引用:`cfg.githubToken = init.githubToken`(右侧是变量,不是字面量)
|
|
27
|
-
* - 默认值取自配置:`token = options.token || ''`(右侧是表达式/空串)
|
|
28
|
-
* 本实现在 token 级确认:只有当「凭据标识符」右侧的赋值/初始化**直接是字符串字面量**
|
|
29
|
-
* 且该字面量**非空、非占位符**时才判定为硬编码。
|
|
30
|
-
*
|
|
31
|
-
* @param {string} text 文件全文
|
|
32
|
-
* @param {object} [opts] { literalOnly=true } 是否只认「右侧直接是字面量」
|
|
33
|
-
* @returns {Set<number>} 判定为「确实硬编码凭据」的行号集合
|
|
34
|
-
*/
|
|
35
|
-
/**
|
|
36
|
-
* 凭据占位符判据(模块级共用)——。
|
|
37
|
-
*
|
|
38
|
-
* 为什么不写在单个检查里:占位符识别被两处用到,必须**同一套判据**——
|
|
39
|
-
* ① checkCredentialRefAst(凭据标识符右侧字面量)
|
|
40
|
-
* ② checkPlaceholderCredentialAst(前缀型密钥串,如 ghp_/sk-/AKIA)
|
|
41
|
-
* 各写一份必然漂移,出现「同一串值 A 规则放行、B 规则拦截」的矛盾。
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
/** 明显不是真凭据的值:空、掩码、占位文案、演示/测试数据。 */
|
|
45
|
-
const CRED_PLACEHOLDER = /^(|\s*|x+|\*+|<[^>]*>|your[-_ ]?.*|change[-_ ]?me|todo|placeholder|example.*|demo.*|sample.*|test.*|dummy.*|fake.*|xxx.*|redacted|masked|none|null|undefined)$/i;
|
|
46
|
-
|
|
47
|
-
/** 单段文本是否占位符文案。 */
|
|
48
|
-
function isPlaceholderText(text) {
|
|
49
|
-
return CRED_PLACEHOLDER.test(String(text));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* 常见凭据 scheme 前缀:判占位符前先剥掉再判一次。
|
|
54
|
-
* 否则带前缀的演示值会漏判——`ghp_` + 「Example…」开头的演示值,其首个词不是 example,
|
|
55
|
-
* 匹配不到 `example.*`,就被当成真凭据报拦截(演示数据/夹具里的常见写法)。
|
|
56
|
-
* 剥前缀不会放过真凭据:真实随机串剥完仍非占位符,照样报。
|
|
57
|
-
*/
|
|
58
|
-
const CRED_SCHEME_PREFIX = /^(gh[pousr]_|github_pat_|sk-[a-z]*-?|xox[baprs]-|glpat-|AKIA)/i;
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 运行时求值形态:值不是「写死的字面量」,而是运行期由 shell/模板求出来的。
|
|
62
|
-
*
|
|
63
|
-
* 背景(真实误报):`TOKEN="$(read_token)"` 被 security/no-hardcoded-credentials
|
|
64
|
-
* 报为硬编码凭据——分词器把整个 `"$(read_token)"` 当成一个 str token,本模块
|
|
65
|
-
* 只看「是不是占位符文案」,看不出值是命令替换,于是照报。
|
|
66
|
-
* 这类写法值来自函数/shell,代码里并没有凭据内容,属误报。
|
|
67
|
-
*
|
|
68
|
-
* 只认三种明确形态(刻意收紧):
|
|
69
|
-
* ① `$(cmd)` 命令替换
|
|
70
|
-
* ② `${VAR}` 花括号插值
|
|
71
|
-
* ③ 以 `$` 开头:纯变量引用 `$TOKEN` / `$1`
|
|
72
|
-
* **不**认「含 $ 即可」——`my$ecret`、`pass$word!` 这类带 $ 的真密码必须照报,
|
|
73
|
-
* 否则规则出现后门(实测按「含 $」判会放过 4 类真凭据)。
|
|
74
|
-
*/
|
|
75
|
-
const CRED_INTERPOLATED = /\$\(|\$\{|^\$[A-Za-z0-9_]/;
|
|
76
|
-
|
|
77
|
-
/** 去掉字符串字面量外层引号(含 Python bytes/raw/f 前缀:b"…" / rb'…')。 */
|
|
78
|
-
function stripQuotes(raw) {
|
|
79
|
-
return String(raw)
|
|
80
|
-
.replace(/^([a-zA-Z]{0,2})(\'|"|`)([\s\S]*)\2$/, '$3')
|
|
81
|
-
.trim();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* 主题令牌/标签样式值形状:小写点分段(text.secondary / bg.hover / accent.subtle)
|
|
86
|
-
* 或「小写词+冒号」标签前缀(plugin-preset: / preview-remove:,后接拼接/插值)。
|
|
87
|
-
* 这类值是被 `token=`/`fill_token=` 之类**变量名撞词**带进来的样式枚举名,
|
|
88
|
-
* 不是凭据内容——外部仓库实测 10 条 blocker 全部误报源于此形态。
|
|
89
|
-
* 刻意取最小面:段内禁大写禁特殊字符、点分段≥2 或尾冒号;
|
|
90
|
-
* `hunter2`(全小写无点无冒号)这类真弱口令**不在豁免内**,照报。
|
|
91
|
-
*/
|
|
92
|
-
const THEME_TOKEN_SHAPE = /^[a-z][a-z0-9_-]*(\.[a-z][a-z0-9_-]*)+:?$/;
|
|
93
|
-
const LABEL_COLON_SHAPE = /^[a-z][a-z0-9_-]*:$/;
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* 判断一个字面量值是否算「真凭据」(非空、非掩码、非占位/演示、非主题令牌样式值)。
|
|
97
|
-
*
|
|
98
|
-
* @param raw 原始字面量(可带引号,可带 Python bytes/raw 前缀)
|
|
99
|
-
* @returns true = 像真凭据(应报);false = 占位符/空值/主题令牌(豁免)
|
|
100
|
-
*/
|
|
101
|
-
export function isMeaningfulCredentialValue(raw) {
|
|
102
|
-
const v = stripQuotes(raw);
|
|
103
|
-
if (!v) return false;
|
|
104
|
-
if (isPlaceholderText(v)) return false;
|
|
105
|
-
// 运行时求值($(cmd) / ${VAR} / $VAR)→ 值不在代码里,不是硬编码
|
|
106
|
-
if (CRED_INTERPOLATED.test(v)) return false;
|
|
107
|
-
// 主题令牌/标签前缀形状(text.secondary / plugin-preset:):变量名撞 token 词的样式枚举。
|
|
108
|
-
// scheme 前缀保护——带 ghp_/sk-/AKIA 等凭据前缀的值即便碰巧撞形状也照报,形状豁免不开后门。
|
|
109
|
-
CRED_SCHEME_PREFIX.lastIndex = 0;
|
|
110
|
-
if (!CRED_SCHEME_PREFIX.test(v) && (THEME_TOKEN_SHAPE.test(v) || LABEL_COLON_SHAPE.test(v))) return false;
|
|
111
|
-
return !isPlaceholderText(v.replace(CRED_SCHEME_PREFIX, ''));
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/*
|
|
115
|
-
* 刻意不做「按 -/_ 拆段逐段判占位符」:试过并撤回。
|
|
116
|
-
* 拆段会把「sk- + test + 随机串」形状的值(scheme 段里带 test)整体豁免——
|
|
117
|
-
* 但这类值是真检出:仓库自身测试夹具正是用它证明「非豁免目录的 blocker 会拦截提交」,
|
|
118
|
-
* 放宽后门禁形同虚设。占位符豁免必须取最小面:只认「整串/剥掉 scheme 前缀后即以
|
|
119
|
-
* 占位词开头」,不认藏在中间段里的演示词。
|
|
120
|
-
*/
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* astConfirmKind: "placeholder-credential" 的精筛集合(mode: deny)。
|
|
124
|
-
*
|
|
125
|
-
* 场景:`secret-github-pat` / `secret-openai-key` / `secret-aws-access-key` 这类
|
|
126
|
-
* **前缀形状匹配**的规则(`\\bghp_[A-Za-z0-9]{20,}\\b`),只看形状不看语义——
|
|
127
|
-
* 文档里写「测试用占位符 + ghp_ 前缀」、演示页假数据
|
|
128
|
-
* `ghp_ExampleToken…` 都会被判成真泄漏(error 级,摘要计入拦截数)。
|
|
129
|
-
*
|
|
130
|
-
* 判定:把命中的密钥串剥掉 scheme 前缀后套占位符判据;命中即加入豁免集。
|
|
131
|
-
* 只看「该串本身是不是占位符」,不做路径/文件级豁免——真 token 无论写在哪个文件都照报。
|
|
132
|
-
*
|
|
133
|
-
* @param text 文件全文
|
|
134
|
-
* @returns Set<number> 命中「占位符凭据」的行号(1 起始)
|
|
135
|
-
*/
|
|
136
|
-
export function checkPlaceholderCredentialAst(text = '', file = '') {
|
|
137
|
-
const out = new Set();
|
|
138
|
-
const SCAN = /\b(gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|sk-(?:proj-)?[A-Za-z0-9-]{16,}|AKIA[0-9A-Z]{16})\b/g;
|
|
139
|
-
const lines = String(text).split('\n');
|
|
140
|
-
const isDoc = /\.(md|markdown|txt|rst)$/i.test(String(file || ''));
|
|
141
|
-
for (let i = 0; i < lines.length; i++) {
|
|
142
|
-
SCAN.lastIndex = 0;
|
|
143
|
-
let match;
|
|
144
|
-
while ((match = SCAN.exec(lines[i])) !== null) {
|
|
145
|
-
if (isMeaningfulCredentialValue(match[1]) === false) { out.add(i + 1); break; }
|
|
146
|
-
// 文档示例豁免——md 文档里出现 token 且该行/邻近行含「示例/演示/
|
|
147
|
-
// 占位/example/demo/sample」语义词,是上游文档的示例值引用(如 MCP 配置文档
|
|
148
|
-
// 写 `token=ghp_xxx` 说明用法),不是本仓库真实凭据。
|
|
149
|
-
// 仅文档文件生效;代码文件里的 token 无论上下文如何都照报(防后门)。
|
|
150
|
-
// 判据收紧:语义词需与 token **同行**(跨行引用不算),且 token 值本身
|
|
151
|
-
// 不像随机真实串(真实长随机 PAT 即使行内带「示例」字样也报——见
|
|
152
|
-
// isMeaningfulCredentialValue 的剥离前缀判占位逻辑,这里是文档级补充)。
|
|
153
|
-
if (isDoc && /示例|演示|占位|example|demo|sample|说明|配置示例/i.test(lines[i])) {
|
|
154
|
-
out.add(i + 1);
|
|
155
|
-
break;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return out;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* 凭据语义标识符(命名**以这些词结尾**的变量/字段才纳入判断)。
|
|
164
|
-
*
|
|
165
|
-
* 此前判「名字里含凭据词即可」,外部 Python 仓库实测暴露两类误报:
|
|
166
|
-
* token_pattern = r'…'(正则模式变量)、_ICON_TOKEN_PROP = "_themed_icon_token"(属性名字面量)
|
|
167
|
-
* ——凭据词只是长描述名的**前缀/中段**,变量装的根本不是凭据值。
|
|
168
|
-
* 改为结尾锚定后:APP_UPDATE_TOKEN_SECRET(结尾 SECRET)、API_KEY、db_password、githubToken
|
|
169
|
-
* (camelCase 结尾 Token)这类「名字即凭据」的写法照常命中;
|
|
170
|
-
* password_length / token_pattern / secretCache 这类不再命中。
|
|
171
|
-
* 代价:mytoken2 这类尾部带数字的命名会漏——刻意取此边界(命中集宁窄勿宽,
|
|
172
|
-
* 前缀型真密钥串另有 secret-github-pat 等形状规则兜底)。
|
|
173
|
-
*/
|
|
174
|
-
/**
|
|
175
|
-
* 凭据语义名两级判定:
|
|
176
|
-
* strong(词义无歧义:password/secret/credential/apikey/accesskey/privatekey/pwd…)
|
|
177
|
-
* → 右侧是有效字面量即判硬编码(历史行为)。
|
|
178
|
-
* weak(token/cookie/key 这类**常用普通词**:token_pattern=正则模式、_ICON_TOKEN_PROP=属性名、
|
|
179
|
-
* cookieChars=字符表)→ 仅当值本身有凭据置信度(scheme 前缀 ghp_/sk-/AKIA…
|
|
180
|
-
* 或 ≥20 位混合大小写+数字的高熵串)才判硬编码。
|
|
181
|
-
* 背景(外部 Python 仓库实测):把 token 当无歧义凭据名时,主题令牌/标签前缀/模式变量
|
|
182
|
-
* 全线误报(10 条 blocker 全假);而真凭据 APP_UPDATE_TOKEN_SECRET 的强词是 SECRET。
|
|
183
|
-
* 结尾锚定试过并再放宽回分段匹配——SECRET_KEY(强词在**前段**)被锚定漏掉即回归,
|
|
184
|
-
* 分段 + 强弱分级同时解决两者。
|
|
185
|
-
*/
|
|
186
|
-
// strong:词义即凭据容器(cookie 的值就是会话凭据,属强词;1.1.4 测试夹具锁死其必报行为)。
|
|
187
|
-
const CRED_NAME_STRONG = /(password|passwd|pwd|secret|credential|cookie|api[_-]?key|access[_-]?key|private[_-]?key|auth[_-]?token|session[_-]?token|refresh[_-]?token)/i;
|
|
188
|
-
// weak:常用普通词(token 常做主题令牌/正则模式变量名、key 常做字典键/琴键名)——要值置信度。
|
|
189
|
-
const CRED_NAME_WEAK = /(token|key)/i;
|
|
190
|
-
|
|
191
|
-
/** 值置信度:scheme 前缀或高熵串(≥20 字符、字符集干净、大小写数字混合)。 */
|
|
192
|
-
const CRED_VALUE_ENTROPY = /^[A-Za-z0-9+/_=.:-]{20,}$/;
|
|
193
|
-
|
|
194
|
-
/** 凭据名分级('strong'|'weak'|null),导出供树提取器与 token 扫描共用同一份定义。 */
|
|
195
|
-
export function credentialNameTier(name) {
|
|
196
|
-
const n = String(name || '');
|
|
197
|
-
if (CRED_NAME_STRONG.test(n)) return 'strong';
|
|
198
|
-
if (CRED_NAME_WEAK.test(n)) return 'weak';
|
|
199
|
-
return null;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* 「名字+值」组合判硬编码(token 路径与树路径共用的唯一判据)。
|
|
204
|
-
* strong 名维持历史口径;weak 名要求值有凭据置信度(scheme/高熵)。
|
|
205
|
-
*/
|
|
206
|
-
export function isHardcodedCredentialValue(name, raw) {
|
|
207
|
-
const tier = credentialNameTier(name);
|
|
208
|
-
if (!tier) return false;
|
|
209
|
-
if (!isMeaningfulCredentialValue(raw)) return false;
|
|
210
|
-
if (tier === 'strong') return true;
|
|
211
|
-
const v = stripQuotes(raw);
|
|
212
|
-
CRED_SCHEME_PREFIX.lastIndex = 0;
|
|
213
|
-
if (CRED_SCHEME_PREFIX.test(v)) return true;
|
|
214
|
-
return CRED_VALUE_ENTROPY.test(v) && /[a-z]/.test(v) && /[A-Z]/.test(v) && /\d/.test(v);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/** 凭据语义名判定谓词(旧签名,兼容树提取器入参;tier 判定走 credentialNameTier)。 */
|
|
218
|
-
export function isCredentialIdent(name) {
|
|
219
|
-
return credentialNameTier(name) !== null;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* 在一段 token 序列上判「凭据标识符右侧是否直接是有效字符串字面量」,命中行号写入 out。
|
|
224
|
-
*
|
|
225
|
-
* @param {Array} tokens tokenize() 结果
|
|
226
|
-
* @param {Set<number>} out 命中行号集合(调用方传入,可累加)
|
|
227
|
-
* @param {number} [lineOffset=0] 行号偏移(子文本从第 1 行重新计数时用)
|
|
228
|
-
*/
|
|
229
|
-
function scanCredentialTokens(tokens, out, lineOffset = 0) {
|
|
230
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
231
|
-
const t = tokens[i];
|
|
232
|
-
if (t.type !== 'ident' || !credentialNameTier(t.value)) continue;
|
|
233
|
-
// 向后找该标识符所在语句的赋值/比较运算符(遇到语句边界就放弃)
|
|
234
|
-
let opIdx = -1;
|
|
235
|
-
for (let j = i + 1; j < Math.min(tokens.length, i + 8); j++) {
|
|
236
|
-
const tj = tokens[j];
|
|
237
|
-
if (tj.type !== 'punct') continue;
|
|
238
|
-
// 误报修复:`(` `)` `,` 是表达式/实参/元素边界——标识符若是函数实参
|
|
239
|
-
// (如 encodeURIComponent(token))或数组元素,运算符并不属于它的赋值,
|
|
240
|
-
// 跨过边界会把**下一条语句**的 `:`/`=` 误认成它的(实测 start-preview.mjs:
|
|
241
|
-
// 'path: "/?" + 凭据字段名 + "=" + ..., method: "GET"'——token 后跨过 `)` `,` 命中
|
|
242
|
-
// method 的 `:` → next="GET" 被当硬编码凭据)。除 `.`(属性访问后仍可能 `=`)
|
|
243
|
-
// 外,遇边界一律 break——任何「标识符不是左值」的场景不再误报。
|
|
244
|
-
if ([';', '{', '}', '(', ')', ','].includes(tj.value)) break; // 语句/表达式/实参边界
|
|
245
|
-
if (['=', ':', '==', '===', '!='].includes(tj.value)) { opIdx = j; break; }
|
|
246
|
-
if (tj.value === '.') continue;
|
|
247
|
-
}
|
|
248
|
-
if (opIdx === -1) continue;
|
|
249
|
-
// 只看严格赋值 `=`(`==`/`===` 是判断、`:` 是对象字面量字段声明,另判)
|
|
250
|
-
const opTok = tokens[opIdx];
|
|
251
|
-
const next = tokens[opIdx + 1];
|
|
252
|
-
if (!next) continue;
|
|
253
|
-
// 字面量前缀(Python b"…"/rb'…'、JS 标签 foo`…`):分词器把前缀切成独立 ident,
|
|
254
|
-
// 值仍在紧随的 str/tmpl token 上。不认前缀会让 bytes 真凭据整行漏报
|
|
255
|
-
// (外部仓库实测 SECRET_KEY = b"…" 未被任何路径捕获)。
|
|
256
|
-
// 认前缀的条件:ident 值≤2 字符且紧随的字符串 token **列紧邻**(无前缀与引号之间
|
|
257
|
-
// 只允许一个空格),否则 `token = b; "x"` 这类跨语句会被误并。
|
|
258
|
-
const litTok = literalAfterPrefix(tokens, next, opIdx);
|
|
259
|
-
// 组合判据与树路径同一份(isHardcodedCredentialValue:strong 名直判 / weak 名要值置信度)
|
|
260
|
-
if (opTok.value === '=' && litTok && isHardcodedCredentialValue(t.value, litTok.value)) {
|
|
261
|
-
out.add(t.line + lineOffset);
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
if (opTok.value === ':' && litTok && isHardcodedCredentialValue(t.value, litTok.value)) {
|
|
265
|
-
// 对象字面量 `token: 'abc'` → 真硬编码;`token: cfg.token` 是引用,不报
|
|
266
|
-
out.add(t.line + lineOffset);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* 取运算符右侧的「字面量 token」,兼容 Python bytes/raw/f 前缀与 JS 模板标签。
|
|
273
|
-
*
|
|
274
|
-
* 分词器把 `b"abc"` 切成 ident("b") + str("\"abc\""),直接看 next.type 会当成
|
|
275
|
-
* 「右侧是变量」而漏报真凭据。这里在 next 是**短小写 ident**(≤2 字符,形如 b/r/f/rb/fr)
|
|
276
|
-
* 且其后紧跟一个字符串 token、且**列紧邻**(间隔 ≤1 列)时,返回那个字符串 token。
|
|
277
|
-
* 列紧邻是必需的:`token = b; other = "x"` 里 b 与 "x" 分属两条语句,
|
|
278
|
-
* 不加列约束会把无关字符串当成 b 的值(实测跨语句误并报)。
|
|
279
|
-
*
|
|
280
|
-
* @returns 命中的字符串 token,否则原样返回 next(非字面量时为 null/其它类型,由调用方判 type)
|
|
281
|
-
*/
|
|
282
|
-
function literalAfterPrefix(tokens, next, opIdx) {
|
|
283
|
-
if (!next) return null;
|
|
284
|
-
if (next.type === 'str' || next.type === 'tmpl') return next;
|
|
285
|
-
if (next.type !== 'ident' || !/^[a-zA-Z]{1,2}$/.test(String(next.value))) return null;
|
|
286
|
-
const after = tokens[opIdx + 2];
|
|
287
|
-
if (!after || (after.type !== 'str' && after.type !== 'tmpl')) return null;
|
|
288
|
-
if (after.line !== next.line) return null;
|
|
289
|
-
if (Math.abs(after.col - (next.col + String(next.value).length)) > 1) return null;
|
|
290
|
-
return after;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
export function checkCredentialRefAst(text = '', tree = null) {
|
|
294
|
-
const raw = String(text);
|
|
295
|
-
const out = new Set();
|
|
296
|
-
// 「有树走树」:tree 为真语法树时,凭据赋值由语法节点判定(参数默认值/复合右值/三元
|
|
297
|
-
// 天然落空),bytes/raw/f-string 前缀也正常识别。树不可用或提取异常 ⇒ 回退 token 级。
|
|
298
|
-
if (tree && tree.rootNode) {
|
|
299
|
-
const fromTree = credentialLinesFromTree(tree, isHardcodedCredentialValue);
|
|
300
|
-
if (fromTree) return fromTree; // 树路径成功(含「无命中」的空集合)——不回退,避免两套判定叠加
|
|
301
|
-
}
|
|
302
|
-
scanCredentialTokens(tokenize(raw), out);
|
|
303
|
-
// markdown 行内代码段兜底:`token: "abc"` 里的反引号被分词器当成模板字面量定界符,
|
|
304
|
-
// 整段合并成一个 tmpl token,内部**不产生** ident/punct/str,直接扫原始 token 会漏报。
|
|
305
|
-
// 而「文档里用行内代码段写配置示例」恰恰是最常见的写法——实测漏报:同一行写法在 .js 里
|
|
306
|
-
// 报、放进 .md 的行内代码段就不报。故把 tmpl 内部文本单独再分词扫一遍。
|
|
307
|
-
// 判据仍是同一个 scanCredentialTokens,不新增第二套判定。
|
|
308
|
-
for (const t of tokenize(raw)) {
|
|
309
|
-
if (t.type !== 'tmpl') continue;
|
|
310
|
-
const inner = String(t.value).replace(/^`+/, '').replace(/`+$/, '');
|
|
311
|
-
if (!inner.trim()) continue;
|
|
312
|
-
scanCredentialTokens(tokenize(inner), out, t.line - 1);
|
|
313
|
-
}
|
|
314
|
-
return out;
|
|
315
|
-
}
|