ohos-playwright 0.2.5 → 0.2.6
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/dist/setup.mjs +17 -1
- package/package.json +1 -1
package/dist/setup.mjs
CHANGED
|
@@ -8,6 +8,16 @@ import { INFO_PATH } from "./info-path.mjs";
|
|
|
8
8
|
const HDC = process.env.OHOS_PW_HDC ?? '/data/service/hnp/bin/hdc';
|
|
9
9
|
const BUNDLE = process.env.OHOS_PW_BUNDLE ?? 'com.huawei.hmos.browser';
|
|
10
10
|
const LAUNCH_URL = process.env.OHOS_PW_LAUNCH_URL ?? 'about:blank';
|
|
11
|
+
// 校验环境变量,防止通过 hdc shell 注入恶意命令。
|
|
12
|
+
// BUNDLE 必须是点分隔的 Android 风格包名;LAUNCH_URL 必须是合法 URL。
|
|
13
|
+
const SAFE_BUNDLE_RE = /^[a-zA-Z][a-zA-Z0-9.]*$/;
|
|
14
|
+
const SAFE_URL_RE = /^[a-z][a-z0-9+.-]*:(?:\/\/)?\S+$/i;
|
|
15
|
+
if (!SAFE_BUNDLE_RE.test(BUNDLE) || BUNDLE.length > 256) {
|
|
16
|
+
throw new Error(`[ohos-playwright] OHOS_PW_BUNDLE "${BUNDLE}" 不是合法的包名(期望: com.example.app)`);
|
|
17
|
+
}
|
|
18
|
+
if (!SAFE_URL_RE.test(LAUNCH_URL) || LAUNCH_URL.length > 2048) {
|
|
19
|
+
throw new Error(`[ohos-playwright] OHOS_PW_LAUNCH_URL "${LAUNCH_URL}" 不是合法的 URL`);
|
|
20
|
+
}
|
|
11
21
|
const HDC_OPTS = { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] };
|
|
12
22
|
function hdc(args, opts) {
|
|
13
23
|
return String(execFileSync(HDC, args, { ...HDC_OPTS, ...opts })).trim();
|
|
@@ -208,7 +218,13 @@ export default async function globalSetup() {
|
|
|
208
218
|
const probe = await probeCdp(port);
|
|
209
219
|
if (!probe.ok)
|
|
210
220
|
throw new Error(`CDP probe failed: ${probe.err || probe.body}`);
|
|
211
|
-
|
|
221
|
+
let info;
|
|
222
|
+
try {
|
|
223
|
+
info = JSON.parse(probe.body);
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
throw new Error(`CDP response is not valid JSON (body preview: ${probe.body?.slice(0, 300) ?? '(empty)'})`);
|
|
227
|
+
}
|
|
212
228
|
console.log(`[ohos-playwright] CDP ready: ${info.Browser}`);
|
|
213
229
|
mkdirSync(dirname(INFO_PATH), { recursive: true });
|
|
214
230
|
writeFileSync(INFO_PATH, JSON.stringify({ port, pid, socket, endpoint: `http://127.0.0.1:${port}` }, null, 2));
|