catchword 0.0.0-stage → 1.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/LICENSE +21 -0
- package/README.md +314 -2
- package/bin/catchword.js +8 -0
- package/bin/falcon.js +5 -0
- package/dist/cli/adb.d.ts +6 -0
- package/dist/cli/adb.js +52 -0
- package/dist/cli/audit.d.ts +4 -0
- package/dist/cli/audit.js +91 -0
- package/dist/cli/build.d.ts +13 -0
- package/dist/cli/build.js +139 -0
- package/dist/cli/clean.d.ts +1 -0
- package/dist/cli/clean.js +15 -0
- package/dist/cli/create.d.ts +4 -0
- package/dist/cli/create.js +60 -0
- package/dist/cli/dev.d.ts +10 -0
- package/dist/cli/dev.js +23 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.js +243 -0
- package/dist/cli/native.d.ts +18 -0
- package/dist/cli/native.js +232 -0
- package/dist/cli/simulator.d.ts +8 -0
- package/dist/cli/simulator.js +77 -0
- package/dist/cli/upload.d.ts +9 -0
- package/dist/cli/upload.js +135 -0
- package/dist/cli/util.d.ts +8 -0
- package/dist/cli/util.js +43 -0
- package/dist/config.d.ts +17 -0
- package/dist/config.js +90 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +51 -0
- package/dist/native/api.d.ts +46 -0
- package/dist/native/api.js +185 -0
- package/dist/native/build.d.ts +47 -0
- package/dist/native/build.js +407 -0
- package/dist/native/elf.d.ts +15 -0
- package/dist/native/elf.js +200 -0
- package/dist/native/types.d.ts +9 -0
- package/dist/native/types.js +91 -0
- package/dist/native/wsl.d.ts +7 -0
- package/dist/native/wsl.js +29 -0
- package/dist/plugin/app-entry.d.ts +6 -0
- package/dist/plugin/app-entry.js +115 -0
- package/dist/plugin/assets.d.ts +4 -0
- package/dist/plugin/assets.js +84 -0
- package/dist/plugin/config.d.ts +14 -0
- package/dist/plugin/config.js +80 -0
- package/dist/plugin/context.d.ts +58 -0
- package/dist/plugin/context.js +255 -0
- package/dist/plugin/fallback.d.ts +4 -0
- package/dist/plugin/fallback.js +81 -0
- package/dist/plugin/image-service.d.ts +17 -0
- package/dist/plugin/image-service.js +138 -0
- package/dist/plugin/jsapi.d.ts +6 -0
- package/dist/plugin/jsapi.js +82 -0
- package/dist/plugin/output.d.ts +4 -0
- package/dist/plugin/output.js +75 -0
- package/dist/plugin/preview.d.ts +34 -0
- package/dist/plugin/preview.js +459 -0
- package/dist/plugin/sanitize.d.ts +5 -0
- package/dist/plugin/sanitize.js +48 -0
- package/dist/plugin/security.d.ts +4 -0
- package/dist/plugin/security.js +18 -0
- package/dist/plugin/sfc.d.ts +6 -0
- package/dist/plugin/sfc.js +181 -0
- package/dist/plugin/shim.d.ts +4 -0
- package/dist/plugin/shim.js +68 -0
- package/dist/plugin/state.d.ts +31 -0
- package/dist/plugin/state.js +61 -0
- package/dist/plugin/string-table.d.ts +7 -0
- package/dist/plugin/string-table.js +324 -0
- package/dist/plugin/style.d.ts +15 -0
- package/dist/plugin/style.js +114 -0
- package/dist/plugin/template.d.ts +7 -0
- package/dist/plugin/template.js +39 -0
- package/dist/steps/manifest.d.ts +9 -0
- package/dist/steps/manifest.js +60 -0
- package/dist/steps/pack.d.ts +3 -0
- package/dist/steps/pack.js +46 -0
- package/dist/steps/qjsc.d.ts +9 -0
- package/dist/steps/qjsc.js +186 -0
- package/dist/steps/report.d.ts +2 -0
- package/dist/steps/report.js +50 -0
- package/dist/steps/security.d.ts +17 -0
- package/dist/steps/security.js +310 -0
- package/dist/types.d.ts +158 -0
- package/dist/types.js +1 -0
- package/dist/util/class-tokenize.d.ts +2 -0
- package/dist/util/class-tokenize.js +56 -0
- package/dist/util/component-name.d.ts +6 -0
- package/dist/util/component-name.js +120 -0
- package/dist/util/fs.d.ts +14 -0
- package/dist/util/fs.js +70 -0
- package/dist/util/imports.d.ts +2 -0
- package/dist/util/imports.js +173 -0
- package/dist/util/log.d.ts +8 -0
- package/dist/util/log.js +24 -0
- package/package.json +61 -4
- package/template/README.md +38 -0
- package/template/catchword.config.ts +18 -0
- package/template/native/CMakeLists.txt +33 -0
- package/template/native/README.md +34 -0
- package/template/native/api.override.json +7 -0
- package/template/native/include/jsapi_demo.h +10 -0
- package/template/native/lib/README.md +10 -0
- package/template/native/sdk/README.md +6 -0
- package/template/native/src/bridge.cpp +9 -0
- package/template/native/src/jsapi_demo.cpp +11 -0
- package/template/package.json +38 -0
- package/template/src/app.js +45 -0
- package/template/src/app.json +11 -0
- package/template/src/base-page.js +150 -0
- package/template/src/components/HelloWorld.vue +20 -0
- package/template/src/mocks/$jsapi.test.js +5 -0
- package/template/src/mocks/test-module.js +5 -0
- package/template/src/pages/index/index.vue +41 -0
- package/template/src/pages/page/page.vue +63 -0
- package/template/src/shims.d.ts +21 -0
- package/template/src/styles/base.less +23 -0
- package/template/src/styles/mixin.less +32 -0
- package/template/src/styles/var.less +15 -0
- package/template/tsconfig.json +14 -0
- package/vendor/README.md +16 -0
- package/vendor/falcon-styler/.jscsrc +31 -0
- package/vendor/falcon-styler/LICENSE +21 -0
- package/vendor/falcon-styler/README.md +124 -0
- package/vendor/falcon-styler/index.js +273 -0
- package/vendor/falcon-styler/lib/shorthand-parser.js +78 -0
- package/vendor/falcon-styler/lib/util.js +30 -0
- package/vendor/falcon-styler/lib/validator.js +846 -0
- package/vendor/falcon-styler/package.json +32 -0
- package/vendor/licenses/falcon-styler.LICENSE +21 -0
- package/vendor/licenses/weex-template-compiler.README.md +3 -0
- package/vendor/package.json +5 -0
- package/vendor/qjsc/SHA256SUMS +11 -0
- package/vendor/qjsc/bin/darwin/arm64/qjsc20200705 +0 -0
- package/vendor/qjsc/bin/darwin/x64/qjsc20200412 +0 -0
- package/vendor/qjsc/bin/darwin/x64/qjsc20200705 +0 -0
- package/vendor/qjsc/bin/darwin/x64/qjsc20200906 +0 -0
- package/vendor/qjsc/bin/darwin/x64/qjscbn20200412 +0 -0
- package/vendor/qjsc/bin/darwin/x64/qjscbn20200705 +0 -0
- package/vendor/qjsc/bin/darwin/x64/qjscbn20200906 +0 -0
- package/vendor/qjsc/bin/linux/qjsc20200705 +0 -0
- package/vendor/qjsc/bin/win32/libgcc_s_dw2-1.dll +0 -0
- package/vendor/qjsc/bin/win32/pthreadGC-3.dll +0 -0
- package/vendor/qjsc/bin/win32/qjsc20200705.exe +0 -0
- package/vendor/web/falcon-js-web.js +863 -0
- package/vendor/web/falcon-jsapi-web.js +479 -0
- package/vendor/web/falcon-vue-render.js +11323 -0
- package/vendor/web/falcon-web-boot.js +4 -0
- package/vendor/web/falcon-web-hud.js +119 -0
- package/vendor/web/falcon-web-style.js +235 -0
- package/vendor/web/vue.runtime.common.dev.js +8467 -0
- package/vendor/web/vue.runtime.common.js +5 -0
- package/vendor/weex-template-compiler/README.md +3 -0
- package/vendor/weex-template-compiler/build.js +4824 -0
- package/vendor/weex-template-compiler/index.js +17 -0
- package/vendor/weex-template-compiler/package.json +25 -0
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { md5File, toUnix, walkSync } from '../util/fs.js';
|
|
4
|
+
import { log } from '../util/log.js';
|
|
5
|
+
export const DEFAULT_SECRET_PATTERNS = [
|
|
6
|
+
{ name: 'private-key', re: /-----BEGIN (?:RSA |EC |OPENSSH |DSA |PGP )?PRIVATE KEY-----/ },
|
|
7
|
+
{ name: 'aws-access-key', re: /\bAKIA[0-9A-Z]{16}\b/ },
|
|
8
|
+
{
|
|
9
|
+
name: 'aws-secret-key',
|
|
10
|
+
re: /(?:aws_secret_access_key|AWS_SECRET_ACCESS_KEY)\s*[:=]\s*["']?[A-Za-z0-9/+=]{40}\b/
|
|
11
|
+
},
|
|
12
|
+
{ name: 'github-token', re: /\bgh[pousr]_[A-Za-z0-9]{30,}\b/ },
|
|
13
|
+
{ name: 'gitlab-token', re: /\bglpat-[A-Za-z0-9_-]{20,}\b/ },
|
|
14
|
+
{
|
|
15
|
+
name: 'openai-anthropic-key',
|
|
16
|
+
re: /\bsk-(?:ant-|proj-|svcacct-|admin-)?[A-Za-z0-9_-]{20,}\b/
|
|
17
|
+
},
|
|
18
|
+
{ name: 'google-api-key', re: /\bAIza[0-9A-Za-z_-]{35}\b/ },
|
|
19
|
+
{ name: 'stripe-key', re: /\b[rs]k_(?:live|test)_[0-9A-Za-z]{16,}\b/ },
|
|
20
|
+
{ name: 'npm-token', re: /\bnpm_[A-Za-z0-9]{36}\b/ },
|
|
21
|
+
{ name: 'aliyun-access-key', re: /\bLTAI[0-9A-Za-z]{12,20}\b/ },
|
|
22
|
+
{ name: 'tencent-secret-id', re: /\bAKID[0-9A-Za-z]{32}\b/ },
|
|
23
|
+
{ name: 'slack-token', re: /\bxox[baprs]-[A-Za-z0-9-]{10,}\b/ },
|
|
24
|
+
{
|
|
25
|
+
name: 'jwt',
|
|
26
|
+
re: /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/
|
|
27
|
+
},
|
|
28
|
+
{ name: 'url-basic-auth', re: /https?:\/\/[^\s/:@]+:[^\s/@]+@/ }
|
|
29
|
+
];
|
|
30
|
+
const TEXT_EXTENSIONS = new Set([
|
|
31
|
+
'.js',
|
|
32
|
+
'.mjs',
|
|
33
|
+
'.cjs',
|
|
34
|
+
'.json',
|
|
35
|
+
'.bin',
|
|
36
|
+
'.map',
|
|
37
|
+
'.txt',
|
|
38
|
+
'.html',
|
|
39
|
+
'.svg',
|
|
40
|
+
'.xml',
|
|
41
|
+
'.yml',
|
|
42
|
+
'.yaml',
|
|
43
|
+
'.toml',
|
|
44
|
+
'.ini',
|
|
45
|
+
'.cfg',
|
|
46
|
+
'.conf',
|
|
47
|
+
'.md',
|
|
48
|
+
'.pem',
|
|
49
|
+
'.key',
|
|
50
|
+
'.crt',
|
|
51
|
+
'.cer',
|
|
52
|
+
'.css',
|
|
53
|
+
'.less',
|
|
54
|
+
'.vue',
|
|
55
|
+
'.ts'
|
|
56
|
+
]);
|
|
57
|
+
const SENSITIVE_FILE_RE = /^\.(?:env(?:\..*)?|npmrc|yarnrc|netrc|git|gitignore|gitattributes|gitconfig|aws|ssh|docker|pypirc|vscode|editorconfig)$/i;
|
|
58
|
+
const TEXT_BASENAMES = /^(?:env|config|secrets?|credentials?|password|token)$/i;
|
|
59
|
+
function isScannable(file) {
|
|
60
|
+
if (TEXT_BASENAMES.test(path.basename(file)))
|
|
61
|
+
return true;
|
|
62
|
+
return TEXT_EXTENSIONS.has(path.extname(file).toLowerCase());
|
|
63
|
+
}
|
|
64
|
+
function redact(value) {
|
|
65
|
+
if (value.length <= 8)
|
|
66
|
+
return '***';
|
|
67
|
+
return value.slice(0, 4) + '***' + value.slice(-2);
|
|
68
|
+
}
|
|
69
|
+
export function assertNoSourceMaps(ctx) {
|
|
70
|
+
if (!fs.existsSync(ctx.buildDir))
|
|
71
|
+
return;
|
|
72
|
+
const files = walkSync(ctx.buildDir);
|
|
73
|
+
const maps = files.filter((file) => file.endsWith('.map'));
|
|
74
|
+
if (maps.length > 0) {
|
|
75
|
+
const list = maps.map((file) => path.relative(ctx.buildDir, file)).join('\n ');
|
|
76
|
+
throw new Error(`[catchword] 产物中发现 sourcemap,禁止发布:\n ${list}`);
|
|
77
|
+
}
|
|
78
|
+
const inline = [];
|
|
79
|
+
const sourceMapRef = /sourceMappingURL=data:/;
|
|
80
|
+
for (const file of files) {
|
|
81
|
+
if (!/\.(?:js|mjs|cjs|css|html)$/i.test(file))
|
|
82
|
+
continue;
|
|
83
|
+
if (sourceMapRef.test(fs.readFileSync(file, 'utf8'))) {
|
|
84
|
+
inline.push(path.relative(ctx.buildDir, file).replace(/\\/g, '/'));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (inline.length > 0) {
|
|
88
|
+
throw new Error(`[catchword] 产物中发现内联 sourcemap 引用,禁止发布:\n ${inline.join('\n ')}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export function assertNoSensitiveFiles(ctx) {
|
|
92
|
+
if (!fs.existsSync(ctx.buildDir))
|
|
93
|
+
return;
|
|
94
|
+
const hits = walkSync(ctx.buildDir)
|
|
95
|
+
.map((file) => path.relative(ctx.buildDir, file).replace(/\\/g, '/'))
|
|
96
|
+
.filter((rel) => rel.split('/').some((segment) => SENSITIVE_FILE_RE.test(segment)));
|
|
97
|
+
if (hits.length === 0)
|
|
98
|
+
return;
|
|
99
|
+
throw new Error(`[catchword] 产物中发现不应发布的配置文件,已终止构建:\n ${hits.join('\n ')}\n` +
|
|
100
|
+
`请确认这些文件不是工程依赖,或把运行时确实需要的资源改放到非隐藏文件名。`);
|
|
101
|
+
}
|
|
102
|
+
export function assertNoBuildPathLeak(ctx, state) {
|
|
103
|
+
if (!fs.existsSync(ctx.buildDir))
|
|
104
|
+
return;
|
|
105
|
+
const roots = new Set();
|
|
106
|
+
for (const dir of [ctx.root, ctx.srcDir]) {
|
|
107
|
+
roots.add(toUnix(dir));
|
|
108
|
+
try {
|
|
109
|
+
roots.add(toUnix(fs.realpathSync(dir)));
|
|
110
|
+
}
|
|
111
|
+
catch { }
|
|
112
|
+
}
|
|
113
|
+
const needles = [...roots].filter((dir) => dir.length > 10).map((dir) => dir.toLowerCase());
|
|
114
|
+
if (needles.length === 0)
|
|
115
|
+
return;
|
|
116
|
+
const hits = [];
|
|
117
|
+
for (const file of walkSync(ctx.buildDir)) {
|
|
118
|
+
if (!isScannable(file))
|
|
119
|
+
continue;
|
|
120
|
+
const content = fs.readFileSync(file, 'utf8').replace(/\\/g, '/').toLowerCase();
|
|
121
|
+
const needle = needles.find((item) => content.includes(item));
|
|
122
|
+
if (!needle)
|
|
123
|
+
continue;
|
|
124
|
+
const rel = path.relative(ctx.buildDir, file).replace(/\\/g, '/');
|
|
125
|
+
hits.push({ file: rel, sample: redact(needle) });
|
|
126
|
+
}
|
|
127
|
+
for (const value of state?.encodedStrings ?? []) {
|
|
128
|
+
const content = value.replace(/\\/g, '/').toLowerCase();
|
|
129
|
+
const needle = needles.find((item) => content.includes(item));
|
|
130
|
+
if (needle)
|
|
131
|
+
hits.push({ file: '<string-table>', sample: redact(needle) });
|
|
132
|
+
}
|
|
133
|
+
if (hits.length === 0)
|
|
134
|
+
return;
|
|
135
|
+
const lines = hits.map((hit) => ` ${hit.file} ${hit.sample}`);
|
|
136
|
+
throw new Error(`[catchword] 产物中发现构建机绝对路径,禁止发布:\n${lines.join('\n')}\n` +
|
|
137
|
+
`这通常来自未被压缩/剥离的构建注释或调试信息。`);
|
|
138
|
+
}
|
|
139
|
+
const SOURCE_EXTENSIONS = new Set([
|
|
140
|
+
'.vue',
|
|
141
|
+
'.ts',
|
|
142
|
+
'.tsx',
|
|
143
|
+
'.js',
|
|
144
|
+
'.jsx',
|
|
145
|
+
'.less',
|
|
146
|
+
'.scss',
|
|
147
|
+
'.sass',
|
|
148
|
+
'.styl',
|
|
149
|
+
'.css',
|
|
150
|
+
'.json'
|
|
151
|
+
]);
|
|
152
|
+
const NATIVE_EXTENSIONS = new Set(['.so', '.dylib', '.dll']);
|
|
153
|
+
const MAX_NATIVE_SCAN_BYTES = 32 * 1024 * 1024;
|
|
154
|
+
function escapeRegExp(value) {
|
|
155
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
156
|
+
}
|
|
157
|
+
export function assertNoSourcePathLeak(ctx, state) {
|
|
158
|
+
if (ctx.toolOptions.keepMetaPaths === true)
|
|
159
|
+
return;
|
|
160
|
+
if (!fs.existsSync(ctx.buildDir) || !fs.existsSync(ctx.srcDir))
|
|
161
|
+
return;
|
|
162
|
+
const rels = walkSync(ctx.srcDir)
|
|
163
|
+
.filter((file) => SOURCE_EXTENSIONS.has(path.extname(file).toLowerCase()))
|
|
164
|
+
.map((file) => path.relative(ctx.srcDir, file).replace(/\\/g, '/'))
|
|
165
|
+
.filter((rel) => rel.includes('/') && !rel.includes('..'));
|
|
166
|
+
if (rels.length === 0)
|
|
167
|
+
return;
|
|
168
|
+
const matcher = new RegExp(rels.map(escapeRegExp).join('|'));
|
|
169
|
+
const hits = new Set();
|
|
170
|
+
const check = (content) => {
|
|
171
|
+
const match = matcher.exec(content.replace(/\\/g, '/'));
|
|
172
|
+
if (match)
|
|
173
|
+
hits.add(match[0]);
|
|
174
|
+
};
|
|
175
|
+
for (const file of walkSync(ctx.buildDir)) {
|
|
176
|
+
if (!isScannable(file))
|
|
177
|
+
continue;
|
|
178
|
+
check(fs.readFileSync(file, 'utf8'));
|
|
179
|
+
}
|
|
180
|
+
for (const value of state?.encodedStrings ?? [])
|
|
181
|
+
check(value);
|
|
182
|
+
if (hits.size === 0)
|
|
183
|
+
return;
|
|
184
|
+
throw new Error(`[catchword] 产物中发现源文件路径,禁止发布:\n ${[...hits].join('\n ')}\n` +
|
|
185
|
+
`App.meta 中的 pages/services/providers 已默认清洗;` +
|
|
186
|
+
`如确需保留源路径请设置 catchword.keepMetaPaths: true。`);
|
|
187
|
+
}
|
|
188
|
+
export function warnNativeLeaks(ctx) {
|
|
189
|
+
if (!fs.existsSync(ctx.buildDir))
|
|
190
|
+
return;
|
|
191
|
+
const root = toUnix(ctx.root);
|
|
192
|
+
const issues = [];
|
|
193
|
+
for (const file of walkSync(ctx.buildDir)) {
|
|
194
|
+
if (!NATIVE_EXTENSIONS.has(path.extname(file).toLowerCase()))
|
|
195
|
+
continue;
|
|
196
|
+
const rel = path.relative(ctx.buildDir, file).replace(/\\/g, '/');
|
|
197
|
+
if (fs.statSync(file).size > MAX_NATIVE_SCAN_BYTES) {
|
|
198
|
+
issues.push(`${rel}: 文件过大,未扫描`);
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
const text = fs.readFileSync(file, 'latin1');
|
|
202
|
+
const reasons = [];
|
|
203
|
+
if (text.includes(ctx.root) || text.includes(root))
|
|
204
|
+
reasons.push('工程绝对路径');
|
|
205
|
+
if (text.includes('C:\\Users\\') || /\/mnt\/|\/home\/|\/tmp\/dl\//.test(text)) {
|
|
206
|
+
reasons.push('构建机路径');
|
|
207
|
+
}
|
|
208
|
+
if (text.includes('.debug_info') || text.includes('.debug_line'))
|
|
209
|
+
reasons.push('调试段');
|
|
210
|
+
if (text.includes('.symtab'))
|
|
211
|
+
reasons.push('.symtab 符号表');
|
|
212
|
+
if (reasons.length > 0)
|
|
213
|
+
issues.push(`${rel}: ${reasons.join('、')}`);
|
|
214
|
+
}
|
|
215
|
+
if (issues.length === 0)
|
|
216
|
+
return;
|
|
217
|
+
log.warn(`[catchword] 原生库可能泄漏构建信息:\n ${issues.join('\n ')}\n` +
|
|
218
|
+
`发布前建议用 strip --strip-unneeded / -s 去除符号与调试段,` +
|
|
219
|
+
`并用 -ffile-prefix-map 隐藏构建路径;JSAPI 的 .dynsym 导出需保留。`);
|
|
220
|
+
}
|
|
221
|
+
export function verifyManifestCert(ctx, cert) {
|
|
222
|
+
if (!fs.existsSync(ctx.buildDir)) {
|
|
223
|
+
throw new Error(`[catchword] 构建目录不存在,无法校验 manifest.cert: ${ctx.buildDir}`);
|
|
224
|
+
}
|
|
225
|
+
const problems = [];
|
|
226
|
+
const present = new Set();
|
|
227
|
+
for (const file of walkSync(ctx.buildDir)) {
|
|
228
|
+
const rel = path.relative(ctx.buildDir, file).replace(/\\/g, '/');
|
|
229
|
+
if (rel === 'manifest.json')
|
|
230
|
+
continue;
|
|
231
|
+
present.add(rel);
|
|
232
|
+
if (rel.split('/').some((segment) => segment.startsWith('.'))) {
|
|
233
|
+
problems.push(`未列入清单的隐藏文件: ${rel}`);
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
const entry = cert[rel];
|
|
237
|
+
if (!entry) {
|
|
238
|
+
problems.push(`未列入 manifest.cert 的文件: ${rel}`);
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
const stat = fs.statSync(file);
|
|
242
|
+
if (stat.size !== entry.size) {
|
|
243
|
+
problems.push(`文件大小与 manifest.cert 不一致: ${rel} (${stat.size} != ${entry.size})`);
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
const digest = md5File(file);
|
|
247
|
+
if (digest !== entry.md5) {
|
|
248
|
+
problems.push(`文件 md5 与 manifest.cert 不一致: ${rel}`);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
for (const rel of Object.keys(cert)) {
|
|
252
|
+
if (!present.has(rel))
|
|
253
|
+
problems.push(`manifest.cert 中的文件不存在: ${rel}`);
|
|
254
|
+
}
|
|
255
|
+
if (problems.length === 0)
|
|
256
|
+
return;
|
|
257
|
+
throw new Error(`[catchword] 产物与 manifest.cert 不一致,已终止打包:\n ${problems.join('\n ')}\n` +
|
|
258
|
+
`构建目录可能在生成清单后被修改,请重新构建。`);
|
|
259
|
+
}
|
|
260
|
+
export function scanSecrets(ctx, state) {
|
|
261
|
+
if (ctx.toolOptions.skipSecretScan === true)
|
|
262
|
+
return;
|
|
263
|
+
if (!fs.existsSync(ctx.buildDir))
|
|
264
|
+
return;
|
|
265
|
+
const patterns = [...DEFAULT_SECRET_PATTERNS];
|
|
266
|
+
const forbidden = (ctx.toolOptions.forbiddenStrings ?? []).filter(Boolean);
|
|
267
|
+
const hits = [];
|
|
268
|
+
for (const file of walkSync(ctx.buildDir)) {
|
|
269
|
+
if (!isScannable(file))
|
|
270
|
+
continue;
|
|
271
|
+
const content = fs.readFileSync(file, 'utf8');
|
|
272
|
+
const rel = path.relative(ctx.buildDir, file).replace(/\\/g, '/');
|
|
273
|
+
for (const pattern of patterns) {
|
|
274
|
+
const match = pattern.re.exec(content);
|
|
275
|
+
if (match) {
|
|
276
|
+
hits.push({ file: rel, rule: pattern.name, sample: redact(match[0]) });
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
for (const value of forbidden) {
|
|
280
|
+
if (content.includes(value)) {
|
|
281
|
+
hits.push({ file: rel, rule: 'forbidden-string', sample: redact(value) });
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
for (const value of state?.encodedStrings ?? []) {
|
|
286
|
+
for (const pattern of patterns) {
|
|
287
|
+
const match = pattern.re.exec(value);
|
|
288
|
+
if (match) {
|
|
289
|
+
hits.push({ file: '<string-table>', rule: pattern.name, sample: redact(match[0]) });
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
for (const item of forbidden) {
|
|
293
|
+
if (value.includes(item)) {
|
|
294
|
+
hits.push({
|
|
295
|
+
file: '<string-table>',
|
|
296
|
+
rule: 'forbidden-string',
|
|
297
|
+
sample: redact(item)
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (hits.length === 0) {
|
|
303
|
+
log.success('敏感字符串扫描通过');
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
const lines = hits.map((hit) => ` ${hit.file} [${hit.rule}] ${hit.sample}`);
|
|
307
|
+
throw new Error(`[catchword] 产物中发现疑似密钥/敏感字符串,已终止构建:\n${lines.join('\n')}\n` +
|
|
308
|
+
`请把这些值移到服务端,或在 package.json 的 catchword.forbiddenStrings 中补充规则;` +
|
|
309
|
+
`确认误报时可设置 catchword.skipSecretScan: true。`);
|
|
310
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
export interface StyleOptions {
|
|
2
|
+
theme?: string;
|
|
3
|
+
themeCustom?: string;
|
|
4
|
+
themes?: string[];
|
|
5
|
+
lessPaths?: string[];
|
|
6
|
+
}
|
|
7
|
+
export interface AppMeta {
|
|
8
|
+
pages: Record<string, string>;
|
|
9
|
+
services?: Record<string, string>;
|
|
10
|
+
providers?: Record<string, string>;
|
|
11
|
+
options?: {
|
|
12
|
+
style?: StyleOptions;
|
|
13
|
+
alias?: Record<string, string>;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
};
|
|
16
|
+
meta?: Record<string, unknown>;
|
|
17
|
+
props?: Record<string, unknown>;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
export interface AppToolOptions {
|
|
21
|
+
forbiddenStrings?: string[];
|
|
22
|
+
skipSecretScan?: boolean;
|
|
23
|
+
keepMetadata?: boolean;
|
|
24
|
+
keepComponentNames?: boolean;
|
|
25
|
+
tokenizeClassNames?: boolean;
|
|
26
|
+
keepClassNames?: string[];
|
|
27
|
+
keepStrings?: boolean;
|
|
28
|
+
keepMetaPaths?: boolean;
|
|
29
|
+
keepKeyframeNames?: boolean;
|
|
30
|
+
skipQjscVerify?: boolean;
|
|
31
|
+
}
|
|
32
|
+
export interface AppPackageJson {
|
|
33
|
+
name?: string;
|
|
34
|
+
appid?: string;
|
|
35
|
+
version?: string;
|
|
36
|
+
icon?: string;
|
|
37
|
+
description?: string;
|
|
38
|
+
quickjs?: {
|
|
39
|
+
version?: string;
|
|
40
|
+
bigNum?: boolean;
|
|
41
|
+
};
|
|
42
|
+
'single-js-bundle'?: boolean;
|
|
43
|
+
dependencies?: Record<string, string>;
|
|
44
|
+
devDependencies?: Record<string, string>;
|
|
45
|
+
previewOptions?: Record<string, unknown>;
|
|
46
|
+
falcon?: AppToolOptions;
|
|
47
|
+
catchword?: AppToolOptions;
|
|
48
|
+
[key: string]: unknown;
|
|
49
|
+
}
|
|
50
|
+
export interface QjscOptions {
|
|
51
|
+
version?: string;
|
|
52
|
+
bigNum?: boolean;
|
|
53
|
+
concurrency?: number;
|
|
54
|
+
}
|
|
55
|
+
export type RuntimeMode = 'vue2';
|
|
56
|
+
export interface DeviceProfile {
|
|
57
|
+
name?: string;
|
|
58
|
+
logical?: {
|
|
59
|
+
width: number;
|
|
60
|
+
height: number;
|
|
61
|
+
};
|
|
62
|
+
panel?: {
|
|
63
|
+
width: number;
|
|
64
|
+
height: number;
|
|
65
|
+
rotation?: 0 | 1 | 2 | 3;
|
|
66
|
+
};
|
|
67
|
+
dpr?: number;
|
|
68
|
+
viewport?: number;
|
|
69
|
+
fontMetrics?: 'builtin' | 'dom';
|
|
70
|
+
}
|
|
71
|
+
export interface BuildConfigOptions {
|
|
72
|
+
compress?: boolean;
|
|
73
|
+
qjsc?: boolean;
|
|
74
|
+
pack?: boolean;
|
|
75
|
+
mock?: boolean;
|
|
76
|
+
env?: string[];
|
|
77
|
+
define?: Record<string, unknown>;
|
|
78
|
+
}
|
|
79
|
+
export interface PreviewConfigOptions {
|
|
80
|
+
host?: string;
|
|
81
|
+
port?: number;
|
|
82
|
+
open?: boolean;
|
|
83
|
+
mockDir?: string;
|
|
84
|
+
stage?: boolean;
|
|
85
|
+
stageScale?: number | 'fit';
|
|
86
|
+
shell?: Record<string, string>;
|
|
87
|
+
memory?: {
|
|
88
|
+
totalMB?: number;
|
|
89
|
+
baselineMB?: number;
|
|
90
|
+
show?: boolean;
|
|
91
|
+
};
|
|
92
|
+
proxy?: boolean;
|
|
93
|
+
}
|
|
94
|
+
export interface SimulatorConfigOptions {
|
|
95
|
+
path?: string;
|
|
96
|
+
page?: string;
|
|
97
|
+
}
|
|
98
|
+
export interface BuildHookContext {
|
|
99
|
+
root: string;
|
|
100
|
+
srcDir: string;
|
|
101
|
+
buildDir: string;
|
|
102
|
+
pkg: AppPackageJson;
|
|
103
|
+
appJson: AppMeta;
|
|
104
|
+
appid: string;
|
|
105
|
+
amrPath: string;
|
|
106
|
+
config: FalconUserConfig;
|
|
107
|
+
}
|
|
108
|
+
export interface FalconHooks {
|
|
109
|
+
beforeBuild?: (ctx: BuildHookContext) => void | Promise<void>;
|
|
110
|
+
afterBuild?: (ctx: BuildHookContext, success: boolean) => void | Promise<void>;
|
|
111
|
+
}
|
|
112
|
+
export interface NativeConfigOptions {
|
|
113
|
+
dir?: string;
|
|
114
|
+
sourceDir?: string;
|
|
115
|
+
registry?: string;
|
|
116
|
+
api?: string;
|
|
117
|
+
types?: string;
|
|
118
|
+
overrides?: string;
|
|
119
|
+
buildEnv?: string;
|
|
120
|
+
copyTo?: string;
|
|
121
|
+
targets?: Record<string, NativeTargetConfig>;
|
|
122
|
+
}
|
|
123
|
+
export interface NativeTargetConfig {
|
|
124
|
+
platform?: string;
|
|
125
|
+
out?: string;
|
|
126
|
+
buildDir?: string;
|
|
127
|
+
deps?: string;
|
|
128
|
+
cross?: string;
|
|
129
|
+
toolchainRoot?: string;
|
|
130
|
+
maxGlibc?: string;
|
|
131
|
+
elfClass?: 32 | 64;
|
|
132
|
+
arch?: string;
|
|
133
|
+
archFlags?: string;
|
|
134
|
+
allowedNeeded?: string[];
|
|
135
|
+
cmakeArgs?: string[];
|
|
136
|
+
strip?: boolean;
|
|
137
|
+
}
|
|
138
|
+
export interface FalconUserConfig {
|
|
139
|
+
native?: NativeConfigOptions;
|
|
140
|
+
build?: BuildConfigOptions;
|
|
141
|
+
device?: DeviceProfile;
|
|
142
|
+
preview?: PreviewConfigOptions;
|
|
143
|
+
simulator?: SimulatorConfigOptions;
|
|
144
|
+
hooks?: FalconHooks;
|
|
145
|
+
}
|
|
146
|
+
export interface FalconOptions {
|
|
147
|
+
dir?: string;
|
|
148
|
+
mock?: boolean;
|
|
149
|
+
env?: string[];
|
|
150
|
+
minify?: boolean;
|
|
151
|
+
runtime?: RuntimeMode;
|
|
152
|
+
qjsc?: boolean;
|
|
153
|
+
pack?: boolean;
|
|
154
|
+
qjscConfig?: QjscOptions;
|
|
155
|
+
config?: FalconUserConfig;
|
|
156
|
+
}
|
|
157
|
+
export type CatchwordUserConfig = FalconUserConfig;
|
|
158
|
+
export type CatchwordOptions = FalconOptions;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { parseSync } from 'oxc-parser';
|
|
2
|
+
export function tokenizeStaticClasses(code, tokens) {
|
|
3
|
+
let parsed;
|
|
4
|
+
try {
|
|
5
|
+
parsed = parseSync('render.js', code);
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
return code;
|
|
9
|
+
}
|
|
10
|
+
const replacements = [];
|
|
11
|
+
const walk = (node, parent, key) => {
|
|
12
|
+
if (!node || typeof node !== 'object')
|
|
13
|
+
return;
|
|
14
|
+
if (Array.isArray(node)) {
|
|
15
|
+
for (const item of node)
|
|
16
|
+
walk(item, parent, key);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (node.type === 'Property' &&
|
|
20
|
+
!node.computed &&
|
|
21
|
+
node.key?.type === 'Identifier' &&
|
|
22
|
+
node.key.name === 'staticClass' &&
|
|
23
|
+
node.value?.type === 'ArrayExpression') {
|
|
24
|
+
for (const element of node.value.elements ?? []) {
|
|
25
|
+
if (element?.type === 'Literal' &&
|
|
26
|
+
typeof element.value === 'string' &&
|
|
27
|
+
typeof element.start === 'number' &&
|
|
28
|
+
typeof element.end === 'number') {
|
|
29
|
+
replacements.push({
|
|
30
|
+
start: element.start,
|
|
31
|
+
end: element.end,
|
|
32
|
+
value: tokens.tokenFor(element.value)
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
for (const childKey of Object.keys(node)) {
|
|
39
|
+
if (['type', 'start', 'end', 'loc', 'range', 'parent'].includes(childKey))
|
|
40
|
+
continue;
|
|
41
|
+
walk(node[childKey], node, childKey);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
walk(parsed.program, null, null);
|
|
45
|
+
if (replacements.length === 0)
|
|
46
|
+
return code;
|
|
47
|
+
replacements.sort((a, b) => b.start - a.start);
|
|
48
|
+
let output = code;
|
|
49
|
+
for (const replacement of replacements) {
|
|
50
|
+
output =
|
|
51
|
+
output.slice(0, replacement.start) +
|
|
52
|
+
JSON.stringify(replacement.value) +
|
|
53
|
+
output.slice(replacement.end);
|
|
54
|
+
}
|
|
55
|
+
return output;
|
|
56
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { parseSync } from 'oxc-parser';
|
|
2
|
+
function unwrap(node, vars, depth = 0) {
|
|
3
|
+
if (!node || depth > 8)
|
|
4
|
+
return null;
|
|
5
|
+
switch (node.type) {
|
|
6
|
+
case 'ObjectExpression':
|
|
7
|
+
return node;
|
|
8
|
+
case 'ParenthesizedExpression':
|
|
9
|
+
case 'TSAsExpression':
|
|
10
|
+
case 'TSSatisfiesExpression':
|
|
11
|
+
case 'TSNonNullExpression':
|
|
12
|
+
return unwrap(node.expression ?? node.argument, vars, depth + 1);
|
|
13
|
+
case 'Identifier':
|
|
14
|
+
return unwrap(vars.get(node.name), vars, depth + 1);
|
|
15
|
+
case 'CallExpression': {
|
|
16
|
+
const callee = node.callee;
|
|
17
|
+
const isDefineComponent = callee?.type === 'Identifier' && callee.name === 'defineComponent';
|
|
18
|
+
const isVueExtend = callee?.type === 'MemberExpression' &&
|
|
19
|
+
!callee.computed &&
|
|
20
|
+
callee.object?.type === 'Identifier' &&
|
|
21
|
+
callee.object.name === 'Vue' &&
|
|
22
|
+
callee.property?.name === 'extend';
|
|
23
|
+
if (isDefineComponent || isVueExtend) {
|
|
24
|
+
return unwrap(node.arguments?.[0], vars, depth + 1);
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
default:
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function findDefaultObject(program) {
|
|
33
|
+
const vars = new Map();
|
|
34
|
+
for (const statement of program.body ?? []) {
|
|
35
|
+
if (statement.type !== 'VariableDeclaration')
|
|
36
|
+
continue;
|
|
37
|
+
for (const declaration of statement.declarations ?? []) {
|
|
38
|
+
if (declaration.id?.type === 'Identifier') {
|
|
39
|
+
vars.set(declaration.id.name, declaration.init);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
for (const statement of program.body ?? []) {
|
|
44
|
+
if (statement.type === 'ExportDefaultDeclaration') {
|
|
45
|
+
return unwrap(statement.declaration, vars);
|
|
46
|
+
}
|
|
47
|
+
if (statement.type === 'ExportNamedDeclaration') {
|
|
48
|
+
const specifier = statement.specifiers?.find((item) => (item.exported?.name ?? item.exported?.value) === 'default');
|
|
49
|
+
if (specifier?.local?.name) {
|
|
50
|
+
return unwrap(vars.get(specifier.local.name), vars);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
function escapeRegExp(value) {
|
|
57
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
58
|
+
}
|
|
59
|
+
function kebabCase(value) {
|
|
60
|
+
return value
|
|
61
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
|
62
|
+
.replace(/_/g, '-')
|
|
63
|
+
.toLowerCase();
|
|
64
|
+
}
|
|
65
|
+
export function stripStaticComponentName(code, template = '', fileName = 'component.ts') {
|
|
66
|
+
let parsed;
|
|
67
|
+
try {
|
|
68
|
+
parsed = parseSync(fileName, code);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return { code, removed: false };
|
|
72
|
+
}
|
|
73
|
+
const object = findDefaultObject(parsed.program);
|
|
74
|
+
if (!object)
|
|
75
|
+
return { code, removed: false };
|
|
76
|
+
for (const property of object.properties ?? []) {
|
|
77
|
+
if (property.type !== 'Property' || property.computed)
|
|
78
|
+
continue;
|
|
79
|
+
const key = property.key?.type === 'Identifier'
|
|
80
|
+
? property.key.name
|
|
81
|
+
: property.key?.type === 'Literal'
|
|
82
|
+
? property.key.value
|
|
83
|
+
: null;
|
|
84
|
+
if (key !== 'name')
|
|
85
|
+
continue;
|
|
86
|
+
const value = property.value;
|
|
87
|
+
if (!value || value.type !== 'Literal' || typeof value.value !== 'string')
|
|
88
|
+
continue;
|
|
89
|
+
const name = value.value;
|
|
90
|
+
const selfReference = new RegExp(`<\\s*(?:${escapeRegExp(name)}|${escapeRegExp(kebabCase(name))})(?=[\\s/>])`, 'i');
|
|
91
|
+
if (name && selfReference.test(template)) {
|
|
92
|
+
return { code, removed: false, name };
|
|
93
|
+
}
|
|
94
|
+
if (/\$options\s*\.\s*name/.test(code)) {
|
|
95
|
+
return { code, removed: false, name };
|
|
96
|
+
}
|
|
97
|
+
let start = property.start;
|
|
98
|
+
let end = property.end;
|
|
99
|
+
if (typeof start !== 'number' || typeof end !== 'number') {
|
|
100
|
+
return { code, removed: false, name };
|
|
101
|
+
}
|
|
102
|
+
const after = code.slice(end);
|
|
103
|
+
const afterComma = /^\s*,/.exec(after);
|
|
104
|
+
if (afterComma) {
|
|
105
|
+
end += afterComma[0].length;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
const before = code.slice(0, start);
|
|
109
|
+
const beforeComma = /,\s*$/.exec(before);
|
|
110
|
+
if (beforeComma)
|
|
111
|
+
start -= beforeComma[0].length;
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
code: code.slice(0, start) + code.slice(end),
|
|
115
|
+
removed: true,
|
|
116
|
+
name
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return { code, removed: false };
|
|
120
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare function ensureDir(dir: string): void;
|
|
2
|
+
export declare function exists(p: string): boolean;
|
|
3
|
+
export declare function walkSync(dir: string, opts?: {
|
|
4
|
+
skipDotFiles?: boolean;
|
|
5
|
+
filter?: (file: string) => boolean;
|
|
6
|
+
}): string[];
|
|
7
|
+
export declare function md5File(file: string): string;
|
|
8
|
+
export declare function sha256File(file: string): string;
|
|
9
|
+
export declare function md5(data: string | Buffer): string;
|
|
10
|
+
export declare function copyDirSync(src: string, dest: string): void;
|
|
11
|
+
export declare function rmrf(target: string): void;
|
|
12
|
+
export declare function readJson<T = any>(file: string): T | null;
|
|
13
|
+
export declare function writeFile(file: string, content: string): void;
|
|
14
|
+
export declare function toUnix(p: string): string;
|