@@ -25,27 +25,30 @@ async function generateNativePackage({
2525 platform,
2626 version,
2727 outputDir,
28+ outputBaseDir,
2829 packagePrefix = "turbo" ,
2930 description
3031} : {
3132 platform : Platform ;
3233 version : string ;
3334 outputDir : string ;
35+ outputBaseDir : string ;
3436 packagePrefix ?: string ;
3537 description ?: string ;
3638} ) {
3739 const { os, arch } = platform ;
40+ const safeOutputDir = resolveOutputDir ( outputDir , outputBaseDir ) ;
3841 console . log ( `Generating native package for ${ os } -${ arch } ...` ) ;
3942
40- console . log ( `Cleaning output directory: ${ outputDir } ` ) ;
41- await rm ( outputDir , { recursive : true , force : true } ) ;
42- await mkdir ( path . join ( outputDir , "bin" ) , { recursive : true } ) ;
43+ console . log ( `Cleaning output directory: ${ safeOutputDir } ` ) ;
44+ await rm ( safeOutputDir , { recursive : true , force : true } ) ;
45+ await mkdir ( path . join ( safeOutputDir , "bin" ) , { recursive : true } ) ;
4346
4447 const copyFromTemplate = async ( part : string , ...parts : Array < string > ) => {
4548 console . log ( "Copying " , path . join ( part , ...parts ) ) ;
4649 await copyFile (
4750 path . join ( templateDir , part , ...parts ) ,
48- path . join ( outputDir , part , ...parts )
51+ path . join ( safeOutputDir , part , ...parts )
4952 ) ;
5053 } ;
5154
@@ -77,11 +80,32 @@ async function generateNativePackage({
7780 packageJson . publishConfig = { access : "public" } ;
7881 }
7982 await writeFile (
80- path . join ( outputDir , "package.json" ) ,
83+ path . join ( safeOutputDir , "package.json" ) ,
8184 JSON . stringify ( packageJson , null , 2 )
8285 ) ;
8386
84- console . log ( `Native package generated successfully in ${ outputDir } ` ) ;
87+ console . log ( `Native package generated successfully in ${ safeOutputDir } ` ) ;
88+ }
89+
90+ function resolveOutputDir ( outputDir : string , outputBaseDir : string ) {
91+ const resolvedOutputDir = path . resolve ( outputDir ) ;
92+ const resolvedOutputBaseDir = path . resolve ( outputBaseDir ) ;
93+ const relativeOutputDir = path . relative (
94+ resolvedOutputBaseDir ,
95+ resolvedOutputDir
96+ ) ;
97+
98+ if (
99+ relativeOutputDir === "" ||
100+ relativeOutputDir . startsWith ( ".." ) ||
101+ path . isAbsolute ( relativeOutputDir )
102+ ) {
103+ throw new Error (
104+ `Refusing to clean output directory outside package base: ${ outputDir } `
105+ ) ;
106+ }
107+
108+ return resolvedOutputDir ;
85109}
86110
87111// Exported asn an object instead of export keyword, so that these functions
0 commit comments