Skip to content

Commit 5b2762a

Browse files
committed
Validate directory names in getLocalReportsPaths to prevent path traversal
1 parent fb038db commit 5b2762a

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

bin/utils.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,27 @@ function getLocalReportsPaths() {
205205
throw 'LOCAL_REPORTS_PATH env variable is not set';
206206
}
207207

208+
if ( ! fs.existsSync( reportsPath ) ) {
209+
return [];
210+
}
211+
208212
return fs
209213
.readdirSync( reportsPath, { withFileTypes: true } )
210-
.filter( d => d.isDirectory() )
211-
.map( d => path.join( reportsPath, d.name ) );
214+
.filter( dirent => {
215+
if ( ! dirent.isDirectory() ) {
216+
return false;
217+
}
218+
219+
// Validate directory name to prevent path traversal
220+
// Only allow alphanumeric, hyphens, and underscores
221+
if ( ! /^[A-Za-z0-9_-]+$/.test( dirent.name ) ) {
222+
console.warn( `Skipping invalid directory name: ${ dirent.name }` );
223+
return false;
224+
}
225+
226+
return true;
227+
} )
228+
.map( dirent => path.join( reportsPath, dirent.name ) );
212229
}
213230

214231
module.exports = {

0 commit comments

Comments
 (0)