@@ -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.
2546execSync ( `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` ) ;
2647execFileSync ( "unzip" , [ "-o" , `${ jsDist } /jd-cli.zip` , "-d" , jsDist ] ) ;
@@ -29,16 +50,23 @@ const newJarPath = path.join(jsDist, "replay-stubs.jar");
2950fs . copyFileSync ( "packages/core/android/libs/replay-stubs.jar" , newJarPath ) ;
3051
3152const 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 - z A - Z 0 - 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` ) ;
3361fs . 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
4371if ( newListing !== oldListing ) {
4472 warn ( `⚠️ replay-stubs.jar changes detected. Directory listing diff:\n\`\`\`\n${ oldListing } \n---\n${ newListing } \n\`\`\`` ) ;
0 commit comments