11const fs = require ( 'fs' ) ;
22const path = require ( 'path' ) ;
3- /* eslint-enable */
3+
4+ // To remove the relative path
5+ function sanitizePath ( str ) {
6+ return str ? str . replace ( / ^ ( \. \. ( \/ | \\ | $ ) ) + / , '' ) : str ;
7+ }
8+
9+
10+ function validateAndSanitize ( input ) {
11+ // Allow only alphanumeric characters, dashes, underscores, and dots for file extensions
12+ return input . replace ( / [ ^ a - z A - Z 0 - 9 -_ \. ] / g, '' ) ;
13+ }
14+
15+ function ensureSafePath ( basePath , targetPath ) {
16+ const resolvedBase = path . resolve ( basePath ) ;
17+ const resolvedTarget = path . resolve ( basePath , targetPath ) ;
18+
19+ // console.log('Base Path:', resolvedBase);
20+ // console.log('Target Path:', resolvedTarget);
21+
22+ if ( resolvedTarget . indexOf ( resolvedBase ) !== 0 ) {
23+ throw new Error ( `Unsafe path detected: ${ resolvedTarget } is not within ${ resolvedBase } ` ) ;
24+ }
25+
26+ return resolvedTarget ;
27+ }
428
529const deleteFolderRecursive = ( _path ) => {
30+ // console.log('Attempting to delete:', _path);
31+
632 if ( fs . existsSync ( _path ) ) {
7- fs . readdirSync ( _path ) . forEach ( ( file ) => {
8- const curPath = path . join ( _path , file ) ;
33+ const sanitizedPath = sanitizePath ( _path ) ;
34+ fs . readdirSync ( sanitizedPath ) . forEach ( ( file ) => {
35+ const sanitizedFile = validateAndSanitize ( file ) ;
36+ const curPath = ensureSafePath ( _path , sanitizedFile ) ;
37+
38+ // console.log('Deleting:', curPath);
39+
940 if ( fs . lstatSync ( curPath ) . isDirectory ( ) ) {
1041 deleteFolderRecursive ( curPath ) ;
1142 } else {
1243 fs . unlinkSync ( curPath ) ;
1344 }
1445 } ) ;
1546 fs . rmdirSync ( _path ) ;
47+ } else {
48+ console . log ( 'Path does not exist:' , _path ) ;
1649 }
1750} ;
1851
52+ const rootDir = path . resolve ( __dirname , '..' ) ; // Set the base path to the root of the project
1953const folder = process . argv . slice ( 2 ) [ 0 ] ;
54+ const sanitizedFolder = folder ? validateAndSanitize ( folder ) : null ;
2055
21- if ( folder ) {
22- deleteFolderRecursive ( path . join ( __dirname , '../dist' , folder ) ) ;
56+ if ( sanitizedFolder ) {
57+ // console.log('Sanitized folder:', sanitizedFolder);
58+ deleteFolderRecursive ( ensureSafePath ( rootDir , path . join ( 'dist' , sanitizedFolder ) ) ) ;
2359} else {
24- deleteFolderRecursive ( path . join ( __dirname , '../dist/cjs' ) ) ;
25- deleteFolderRecursive ( path . join ( __dirname , '../dist/esm' ) ) ;
26- deleteFolderRecursive ( path . join ( __dirname , '../dist/umd' ) ) ;
27- deleteFolderRecursive ( path . join ( __dirname , '../dist/types' ) ) ;
28- }
60+ // console.log('No folder specified, deleting default directories...');
61+ deleteFolderRecursive ( ensureSafePath ( rootDir , 'dist/cjs' ) ) ;
62+ deleteFolderRecursive ( ensureSafePath ( rootDir , 'dist/esm' ) ) ;
63+ deleteFolderRecursive ( ensureSafePath ( rootDir , 'dist/umd' ) ) ;
64+ deleteFolderRecursive ( ensureSafePath ( rootDir , 'dist/types' ) ) ;
65+ }
0 commit comments