Skip to content

Commit cdd4900

Browse files
code cleanup
1 parent 57f7f3b commit cdd4900

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

.github/workflows/replay-stub-check.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,7 @@ jobs:
2020
with:
2121
node-version: '20'
2222

23-
- run: npm i -g corepack
24-
25-
- name: Install Danger
26-
run: |
27-
yarn install
28-
yarn add --save-dev danger
29-
3023
- name: Check Replay Stubs
3124
env:
3225
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33-
run: npx danger ci --dangerfile scripts/check-replay-stubs.js
26+
run: npx danger@latest ci --dangerfile scripts/check-replay-stubs.js

scripts/check-replay-stubs.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,36 @@ if (!replayJarChanged) {
1212
return;
1313
}
1414

15+
function validatePath(dirPath) {
16+
const resolved = path.resolve(dirPath);
17+
const cwd = process.cwd();
18+
if (!resolved.startsWith(cwd)) {
19+
throw new Error(`Invalid path: ${dirPath} is outside working directory`);
20+
}
21+
return resolved;
22+
}
1523

16-
const jsDist = path.join(process.cwd(), "js-dist");
17-
const newSrc = path.join(process.cwd(), "replay-stubs-src");
18-
const oldSrc = path.join(process.cwd(), "replay-stubs-old-src");
24+
const jsDist = validatePath(path.join(process.cwd(), "js-dist"));
25+
const newSrc = validatePath(path.join(process.cwd(), "replay-stubs-src"));
26+
const oldSrc = validatePath(path.join(process.cwd(), "replay-stubs-old-src"));
1927

2028
[jsDist, newSrc, oldSrc].forEach(dir => {
2129
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
2230
});
2331

32+
// Cleanup handler for temporary files
33+
function cleanup() {
34+
[jsDist, newSrc, oldSrc].forEach(dir => {
35+
if (fs.existsSync(dir)) {
36+
fs.rmSync(dir, { recursive: true, force: true });
37+
}
38+
});
39+
}
40+
41+
process.on('exit', cleanup);
42+
process.on('SIGINT', cleanup);
43+
process.on('SIGTERM', cleanup);
44+
2445
// Tool for decompiling JARs.
2546
execSync(`curl -L -o ${jsDist}/jd-cli.zip https://github.com/intoolswetrust/jd-cli/releases/download/jd-cli-1.2.0/jd-cli-1.2.0-dist.zip`);
2647
execFileSync("unzip", ["-o", `${jsDist}/jd-cli.zip`, "-d", jsDist]);
@@ -29,16 +50,23 @@ const newJarPath = path.join(jsDist, "replay-stubs.jar");
2950
fs.copyFileSync("packages/core/android/libs/replay-stubs.jar", newJarPath);
3051

3152
const baseJarPath = path.join(jsDist, "replay-stubs-old.jar");
32-
const baseJarContent = execSync(`git show ${danger.github.pr.base.ref}:packages/core/android/libs/replay-stubs.jar`);
53+
54+
// Validate git ref to prevent command injection
55+
const baseRef = danger.github.pr.base.ref;
56+
if (!/^[a-zA-Z0-9/_-]+$/.test(baseRef)) {
57+
throw new Error(`Invalid git ref: ${baseRef}`);
58+
}
59+
60+
const baseJarContent = execSync(`git show ${baseRef}:packages/core/android/libs/replay-stubs.jar`);
3361
fs.writeFileSync(baseJarPath, baseJarContent);
3462

3563
// Decompile both JARs
36-
execSync(`java -jar ${jsDist}/jd-cli.jar -od ${newSrc} ${newJarPath}`);
37-
execSync(`java -jar ${jsDist}/jd-cli.jar -od ${oldSrc} ${baseJarPath}`);
64+
execFileSync("java", ["-jar", `${jsDist}/jd-cli.jar`, "-od", newSrc, newJarPath]);
65+
execFileSync("java", ["-jar", `${jsDist}/jd-cli.jar`, "-od", oldSrc, baseJarPath]);
3866

3967
// Compare directory listings
40-
const newListing = execSync(`ls -lR ${newSrc}`).toString();
41-
const oldListing = execSync(`ls -lR ${oldSrc}`).toString();
68+
const newListing = execFileSync("ls", ["-lR", newSrc]).toString();
69+
const oldListing = execFileSync("ls", ["-lR", oldSrc]).toString();
4270

4371
if (newListing !== oldListing) {
4472
warn(`⚠️ replay-stubs.jar changes detected. Directory listing diff:\n\`\`\`\n${oldListing}\n---\n${newListing}\n\`\`\``);

0 commit comments

Comments
 (0)