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
@@ -1,4 +1,8 @@
|
||||
import { test, expect } from "@playwright/test";
|
||||
|
||||
// 产物会遮蔽 Taro 运行时命名,选择器同时接受源码名称与遮蔽名称。
|
||||
const BUTTON_CORE = ":is(taro-button-core, appkit-button-core)";
|
||||
const TABBAR = ":is(.taro-tabbar__tabbar, .appkit-tabbar__tabbar)";
|
||||
test("真实 API 驱动首页、出摊和主理人视觉槽位", async ({ page }) => {
|
||||
const errors: string[] = [];
|
||||
page.on("pageerror", (error) => errors.push(error.message));
|
||||
@@ -57,7 +61,7 @@ test("餐品搜索、详情和咨询目标传递", async ({ page }) => {
|
||||
const name = await page.locator(".dish-name").first().textContent();
|
||||
await page.locator(".offering-card").first().click();
|
||||
await expect(page.locator(".hero-copy")).toContainText(name!);
|
||||
await page.locator(".bar taro-button-core").click();
|
||||
await page.locator(`.bar ${BUTTON_CORE}`).click();
|
||||
await expect(page.locator(".inquiry-form")).toBeVisible();
|
||||
await expect(page.locator(".half-field")).toHaveCount(4);
|
||||
});
|
||||
@@ -124,17 +128,17 @@ for (const width of [375, 430, 768, 1280]) {
|
||||
await page.goto("/");
|
||||
await expect(page.locator(".food-home .name")).toBeVisible();
|
||||
const expectedWidth = Math.min(width, 430);
|
||||
const bounds = await page.evaluate(() => {
|
||||
const bounds = await page.evaluate((tabSelector) => {
|
||||
const rect = (selector: string) => {
|
||||
const box = document.querySelector(selector)!.getBoundingClientRect();
|
||||
return { width: box.width, left: box.left };
|
||||
};
|
||||
return {
|
||||
page: rect(".food-home .page"),
|
||||
tab: rect(".taro-tabbar__tabbar"),
|
||||
tab: rect(tabSelector),
|
||||
overflow: document.documentElement.scrollWidth > window.innerWidth,
|
||||
};
|
||||
});
|
||||
}, TABBAR);
|
||||
expect(bounds.page.width).toBeCloseTo(expectedWidth, 0);
|
||||
expect(bounds.tab.width).toBeCloseTo(expectedWidth, 0);
|
||||
expect(bounds.tab.left).toBeCloseTo(bounds.page.left, 0);
|
||||
@@ -167,7 +171,7 @@ for (const width of [375, 1280]) {
|
||||
};
|
||||
const aboveNavigation = async (selector: string) => {
|
||||
const bottom = await page.locator(selector).last().boundingBox();
|
||||
const nav = await page.locator(".taro-tabbar__tabbar").boundingBox();
|
||||
const nav = await page.locator(TABBAR).boundingBox();
|
||||
expect(bottom!.y + bottom!.height).toBeLessThan(nav!.y - 8);
|
||||
};
|
||||
await page.goto("/");
|
||||
|
||||
@@ -2,6 +2,7 @@ import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { unmask } from "./mask-names.mjs";
|
||||
const root = path.resolve(import.meta.dirname, "../../..");
|
||||
const templateKeys = fs
|
||||
.readdirSync(path.join(root, "packages"))
|
||||
@@ -31,7 +32,7 @@ for (const key of templateKeys)
|
||||
);
|
||||
assert.ok(fs.existsSync(path.join(base, "weapp/project.config.json")));
|
||||
const weappStyles = fs.readFileSync(path.join(base, "weapp/common.wxss"), "utf8");
|
||||
const appStyles = fs.readFileSync(path.join(base, "weapp/app-origin.wxss"), "utf8");
|
||||
const appStyles = unmask(fs.readFileSync(path.join(base, "weapp/app-origin.wxss"), "utf8"));
|
||||
assert.ok(weappStyles.length > 1000);
|
||||
assert.match(appStyles, /(^|})button\{/);
|
||||
assert.match(appStyles, /--taro-button-height:\s*88rpx/);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
const configPath = path.join(import.meta.dirname, "../config/mask.config.json");
|
||||
export const maskConfig = JSON.parse(fs.readFileSync(configPath, "utf8"));
|
||||
|
||||
const entries = Object.entries(maskConfig.replacements).sort(([a], [b]) => b.length - a.length);
|
||||
const reverseEntries = entries
|
||||
.map(([from, to]) => [to, from])
|
||||
.sort(([a], [b]) => b.length - a.length);
|
||||
|
||||
/** 按遮蔽配置推导名称,测试与构建共用同一张表,避免两边写死而不同步。 */
|
||||
export function masked(name) {
|
||||
if (!maskConfig.enabled) return name;
|
||||
return entries.reduce((result, [from, to]) => result.split(from).join(to), name);
|
||||
}
|
||||
|
||||
/** 把制品中的名称还原成源码名称,让产物契约断言不依赖产物是否已经遮蔽。 */
|
||||
export function unmask(name) {
|
||||
if (!maskConfig.enabled) return name;
|
||||
return reverseEntries.reduce((result, [from, to]) => result.split(from).join(to), name);
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { createRequire } from "node:module";
|
||||
import { maskConfig, masked, unmask } from "./mask-names.mjs";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const { maskText, maskFileName, maskDirectory, findLeaks } = require("../config/mask-taro.cjs");
|
||||
|
||||
const replacements = maskConfig.replacements;
|
||||
/** 长度超过 40 的 base64/哈希片段,遮蔽时必须原样保留。 */
|
||||
const opaqueRun = `QWxhZGRpbjpvcGVuIHNlc2FtZQ${"taro"}VzYW1lQW5kUGFzc3dvcmQ`;
|
||||
|
||||
test("遮蔽表在测试与构建之间保持一致", () => {
|
||||
for (const name of [
|
||||
"taro-button-core",
|
||||
"taro_tmpl",
|
||||
"@tarojs/components",
|
||||
"taroWindowProvider",
|
||||
"--taro-button-height",
|
||||
".taro-tabbar__tabbar",
|
||||
])
|
||||
assert.equal(masked(name), maskText(name, replacements), name);
|
||||
assert.equal(masked("taro-button-core"), "appkit-button-core");
|
||||
assert.equal(masked(".taro-tabbar__tabbar"), ".appkit-tabbar__tabbar");
|
||||
assert.deepEqual(
|
||||
Object.keys(replacements).map((from) => unmask(masked(from))),
|
||||
Object.keys(replacements),
|
||||
);
|
||||
});
|
||||
|
||||
test("文本遮蔽改标识符但不损坏不透明数据段", () => {
|
||||
const content = `var tmpl="taro_tmpl";var key="${opaqueRun}";const modifyTaroEvent=1;`;
|
||||
const result = maskText(content, replacements);
|
||||
assert.ok(result.includes('"appkit_tmpl"'));
|
||||
assert.ok(result.includes(`"${opaqueRun}"`));
|
||||
assert.ok(result.includes("modifyAppKitEvent"), "camelCase 标识符必须一起遮蔽");
|
||||
assert.ok(!/taro/i.test(result.replace(opaqueRun, "")), "可替换区段不得残留指纹");
|
||||
});
|
||||
|
||||
test("文件名遮蔽与内容使用同一张表", () => {
|
||||
assert.equal(maskFileName("taro.js", replacements), "appkit.js");
|
||||
assert.equal(maskFileName("taro-tmpl.wxml", replacements), "appkit-tmpl.wxml");
|
||||
assert.equal(maskFileName("common.wxss", replacements), "common.wxss");
|
||||
});
|
||||
|
||||
test("遮蔽产物目录:文件名、引用与 project.config.json 同步更新且无残留指纹", () => {
|
||||
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "mask-taro-"));
|
||||
try {
|
||||
fs.mkdirSync(path.join(directory, "assets"));
|
||||
const binary = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x00, 0xff]);
|
||||
fs.writeFileSync(path.join(directory, "assets/logo.png"), binary);
|
||||
fs.writeFileSync(path.join(directory, "app.js"), 'require("./taro.js");\n');
|
||||
fs.writeFileSync(path.join(directory, "taro.js"), 'var tmpl="taro_tmpl";\n');
|
||||
fs.writeFileSync(path.join(directory, "taro-tmpl.wxml"), '<template name="taro_tmpl"/>\n');
|
||||
fs.writeFileSync(
|
||||
path.join(directory, "project.config.json"),
|
||||
`${JSON.stringify({ appid: "wx-test", projectname: "personal-brand-taro" }, null, 2)}\n`,
|
||||
);
|
||||
|
||||
const result = maskDirectory(directory, maskConfig);
|
||||
|
||||
assert.deepEqual(result.leaked, []);
|
||||
assert.deepEqual(findLeaks(directory, maskConfig), []);
|
||||
assert.ok(fs.existsSync(path.join(directory, "appkit.js")), "被引用的文件必须同步改名");
|
||||
assert.ok(fs.existsSync(path.join(directory, "appkit-tmpl.wxml")));
|
||||
assert.equal(
|
||||
fs.readFileSync(path.join(directory, "app.js"), "utf8"),
|
||||
'require("./appkit.js");\n',
|
||||
);
|
||||
assert.equal(fs.existsSync(path.join(directory, "taro.js")), false, "旧文件名不能残留");
|
||||
const projectConfig = JSON.parse(
|
||||
fs.readFileSync(path.join(directory, "project.config.json"), "utf8"),
|
||||
);
|
||||
assert.equal(projectConfig.projectname, maskConfig.project.projectname);
|
||||
assert.equal(projectConfig.appid, "wx-test");
|
||||
assert.deepEqual(fs.readFileSync(path.join(directory, "assets/logo.png")), binary);
|
||||
} finally {
|
||||
fs.rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user