feat: 遮蔽产物中的 Taro 指纹
H5 预览与小程序产物会带出 taro_tmpl、taro-*-core、taro.js、--taro-button-height、 project.config.json 的 projectname 等指纹,而 Taro 4.3 没有隐藏这些名称的配置。 改为在 build:h5、build:weapp、build:preview 之后按 apps/taro/config/mask.config.json 做构建后处理: - 同一张替换表同时作用于文件名与文本内容,重命名后引用保持同步,长度 ≥ 40 的 base64/哈希片段原样保留; - 遮蔽后仍命中 forbidden 词表时构建失败,并把 project.config.json 的 projectname/description 中性化; - dev:h5、dev:preview 保持原始命名,便于本地调试;改架构名只改 mask.config.json; - 产物契约测试先用 unmask() 还原再断言,浏览器测试选择器写成 :is(<源码名>, <遮蔽名>)。 取舍与迁移见 docs/adr/0025-build-output-stack-masking.md。
This commit is contained in:
1 parent
53713e5935
commit
9ad2776515
13 files changed
+382
-10
No files matched your search
@@ -29,7 +29,7 @@ fs.cpSync(path.join(template, "miniapp/assets"), path.resolve(__dirname, "../src
|
||||
recursive: true,
|
||||
});
|
||||
export default defineConfig({
|
||||
projectName: "personal-brand-taro",
|
||||
projectName: "personal-brand-miniapp",
|
||||
date: "2026-10-08",
|
||||
designWidth: 750,
|
||||
deviceRatio: { 750: 1 },
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
/**
|
||||
* 构建后处理:把产物里的 Taro 指纹替换成中性标识。
|
||||
*
|
||||
* 同一张替换表同时作用于「文件名」和「文本内容」,因此 taro.js 会连文件名带引用
|
||||
* 一起变成 appkit.js,模板名 taro_tmpl 与页面里的引用也会同步,无需分别维护。
|
||||
*
|
||||
* 用法:
|
||||
* node config/mask-taro.cjs weapp 处理后 dist/<template>/weapp
|
||||
* node config/mask-taro.cjs h5 处理后 dist/<template>/h5
|
||||
* node config/mask-taro.cjs h5 --preview 处理后 dist/template-preview/h5
|
||||
* node config/mask-taro.cjs weapp --check 只检查是否仍有指纹,不修改
|
||||
*/
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
const taroRoot = path.resolve(__dirname, "..");
|
||||
const configPath = path.join(__dirname, "mask.config.json");
|
||||
|
||||
/** 需要做文本替换的扩展名,二进制文件一律跳过。 */
|
||||
const textExtensions = new Set([
|
||||
".js",
|
||||
".mjs",
|
||||
".cjs",
|
||||
".json",
|
||||
".wxml",
|
||||
".wxss",
|
||||
".wxs",
|
||||
".css",
|
||||
".html",
|
||||
]);
|
||||
|
||||
function loadConfig() {
|
||||
return JSON.parse(fs.readFileSync(configPath, "utf8"));
|
||||
}
|
||||
|
||||
/** 长键优先,避免 `@tarojs` 被 `taro` 抢先替换成 `@appkitjs` 这种半成品。 */
|
||||
function orderedReplacements(replacements) {
|
||||
return Object.entries(replacements ?? {}).sort(([a], [b]) => b.length - a.length);
|
||||
}
|
||||
|
||||
function escapeRegExp(value) {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
/** 连续的长 base64/哈希片段:可能是内嵌图片、密钥或摘要,替换进去会损坏数据。 */
|
||||
const opaqueRun = /[A-Za-z0-9+/=]{40,}/;
|
||||
const opaqueRunSplit = /([A-Za-z0-9+/=]{40,})/;
|
||||
|
||||
/**
|
||||
* 把内容切成「可替换区段」和「受保护区段」,只在前者做替换。
|
||||
* 这样既能命中边界处的 `taro_tmpl`,也能命中 camelCase 的 `modifyTaroEvent`,
|
||||
* 又不会动到内嵌的 base64 数据。
|
||||
*/
|
||||
function maskText(content, replacements) {
|
||||
const entries = orderedReplacements(replacements);
|
||||
return content
|
||||
.split(opaqueRunSplit)
|
||||
.map((segment) =>
|
||||
opaqueRun.test(segment)
|
||||
? segment
|
||||
: entries.reduce(
|
||||
(result, [from, to]) => result.replace(new RegExp(escapeRegExp(from), "g"), to),
|
||||
segment,
|
||||
),
|
||||
)
|
||||
.join("");
|
||||
}
|
||||
|
||||
function maskFileName(name, replacements) {
|
||||
return orderedReplacements(replacements).reduce(
|
||||
(result, [from, to]) => result.replace(new RegExp(escapeRegExp(from), "g"), to),
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
||||
function resolveTargets(envType, preview) {
|
||||
if (preview) return [path.join(taroRoot, "dist/template-preview/h5")];
|
||||
const templateKey = process.env.TEMPLATE_KEY;
|
||||
if (!templateKey) throw new Error("TEMPLATE_KEY is required for non-preview masking");
|
||||
return [path.join(taroRoot, "dist", templateKey, envType)];
|
||||
}
|
||||
|
||||
function walk(directory) {
|
||||
const files = [];
|
||||
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
||||
const full = path.join(directory, entry.name);
|
||||
if (entry.isDirectory()) files.push(...walk(full));
|
||||
else if (entry.isFile()) files.push(full);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
function maskProjectConfig(directory, config) {
|
||||
const target = path.join(directory, "project.config.json");
|
||||
if (!fs.existsSync(target)) return;
|
||||
const current = JSON.parse(fs.readFileSync(target, "utf8"));
|
||||
const next = { ...current, ...(config.project ?? {}) };
|
||||
fs.writeFileSync(target, `${JSON.stringify(next, null, 2)}\n`);
|
||||
}
|
||||
|
||||
function renameFiles(directory, files, replacements) {
|
||||
const renamed = [];
|
||||
for (const file of files) {
|
||||
const currentName = path.basename(file);
|
||||
const nextName = maskFileName(currentName, replacements);
|
||||
if (nextName === currentName) continue;
|
||||
const next = path.join(path.dirname(file), nextName);
|
||||
if (fs.existsSync(next)) throw new Error(`遮蔽后文件名冲突:${next}`);
|
||||
fs.renameSync(file, next);
|
||||
renamed.push([currentName, nextName]);
|
||||
}
|
||||
return renamed;
|
||||
}
|
||||
|
||||
function maskDirectory(directory, config, { check = false } = {}) {
|
||||
if (!fs.existsSync(directory)) throw new Error(`产物目录不存在:${directory}`);
|
||||
const replacements = config.replacements ?? {};
|
||||
const files = walk(directory);
|
||||
let rewritten = 0;
|
||||
|
||||
for (const file of files) {
|
||||
if (!textExtensions.has(path.extname(file))) continue;
|
||||
const content = fs.readFileSync(file, "utf8");
|
||||
const masked = maskText(content, replacements);
|
||||
if (masked === content) continue;
|
||||
rewritten += 1;
|
||||
if (!check) fs.writeFileSync(file, masked);
|
||||
}
|
||||
|
||||
const renamed = check ? [] : renameFiles(directory, files, replacements);
|
||||
if (!check) maskProjectConfig(directory, config);
|
||||
|
||||
return {
|
||||
directory,
|
||||
scanned: files.length,
|
||||
rewritten,
|
||||
renamed,
|
||||
leaked: findLeaks(directory, config),
|
||||
};
|
||||
}
|
||||
|
||||
/** 扫描整个产物目录,返回仍然含有指纹的位置,用于守卫。 */
|
||||
function findLeaks(directory, config) {
|
||||
const forbidden = config.forbidden ?? [];
|
||||
if (!forbidden.length) return [];
|
||||
const leaks = [];
|
||||
for (const file of walk(directory)) {
|
||||
const name = path.basename(file);
|
||||
if (forbidden.some((token) => name.includes(token))) {
|
||||
leaks.push(`${path.relative(directory, file)} :: 文件名`);
|
||||
continue;
|
||||
}
|
||||
if (!textExtensions.has(path.extname(file))) continue;
|
||||
const content = fs.readFileSync(file, "utf8");
|
||||
for (const token of forbidden) {
|
||||
if (content.includes(token)) {
|
||||
leaks.push(`${path.relative(directory, file)} :: 内容包含 ${token}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return leaks;
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = process.argv.slice(2);
|
||||
const envType = args.find((arg) => !arg.startsWith("--")) ?? "weapp";
|
||||
const preview = args.includes("--preview");
|
||||
const check = args.includes("--check");
|
||||
const config = loadConfig();
|
||||
|
||||
if (!config.enabled && !check) {
|
||||
console.log("[mask] 配置为 enabled: false,跳过遮蔽");
|
||||
return;
|
||||
}
|
||||
|
||||
let failed = false;
|
||||
for (const directory of resolveTargets(envType, preview)) {
|
||||
const result = maskDirectory(directory, config, { check });
|
||||
const relative = path.relative(taroRoot, result.directory);
|
||||
console.log(
|
||||
`[mask] ${relative}:扫描 ${result.scanned} 个文件,改写 ${result.rewritten} 个` +
|
||||
(result.renamed.length ? `,重命名 ${result.renamed.length} 个` : ""),
|
||||
);
|
||||
for (const [from, to] of result.renamed) console.log(`[mask] 重命名 ${from} -> ${to}`);
|
||||
if (result.leaked.length) {
|
||||
failed = true;
|
||||
console.error(`[mask] ${relative} 仍存在技术栈指纹:`);
|
||||
for (const leak of result.leaked.slice(0, 20)) console.error(` - ${leak}`);
|
||||
}
|
||||
}
|
||||
if (failed) process.exit(1);
|
||||
}
|
||||
|
||||
module.exports = { maskText, maskFileName, maskDirectory, findLeaks, loadConfig };
|
||||
|
||||
if (require.main === module) main();
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"enabled": true,
|
||||
"replacements": {
|
||||
"@tarojs": "@app-kit",
|
||||
"taro": "appkit",
|
||||
"Taro": "AppKit",
|
||||
"TARO": "APPKIT"
|
||||
},
|
||||
"project": {
|
||||
"projectname": "personal-brand-miniapp",
|
||||
"description": "个人品牌小程序发布产物"
|
||||
},
|
||||
"forbidden": ["taro", "Taro", "TARO", "tarojs", "@tarojs"]
|
||||
}
|
||||
Reference in new issue
Block a user