Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

367
Views
How to resolve Path Traversal vulnerability for the below code
const downloadFile = blobstoreRouter.get('/blobstore/download/:filename', (req, res) => {
var localFile = path.join(__dirname, '..', escape(req.params.filename));
var file = require('fs').createWriteStream(localFile);
try {
    s3.getObject({
        Bucket: process.env.BUCKET,
        Key: req.params.filename
    }).createReadStream().pipe(file);
    fs.readdir('src', (_err, files) => {
        files.forEach(file => {
            console.log(file);
            logger.info(file);
        });
    });
    res.setHeader('Strict-Transport-Security',
    'max-age=31536000; includeSubDomains');
    res.sendFile(file);
} catch (err) {
    logger.error('Error downloading the file ' + err);
    res.send('Failed');
}
});

Checkmarx gives warning at the line res.sendFile(file);

I've added the HSTS header also for the same but not sure if that works as well. Please guide me through both. Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Neither adding HSTS or escaping the req.params.filename will mitigate the Path Traversal vulnerability. To understand what Path Traversal is, here's a blog post that you might want to read:

https://nodejs.org/en/knowledge/file-system/security/introduction/

There are plenty of ways to resolve this security finding, but Checkmarx recognizes the use of sanitization methods such as replace()

Remove potentially malicious characters that will allow an attacker to traverse on different paths of your S3 bucket:

var localFile = path.join(__dirname, '..', escape(req.params.filename.replace(/^(\.\.(\/|\\|$))+/, '')));
var file = require('fs').createWriteStream(localFile);
try {
    s3.getObject({
        Bucket: process.env.BUCKET,
        Key: req.params.filename.replace(/^(\.\.(\/|\\|$))+/, '')
    }).createReadStream().pipe(file);
    fs.readdir('src', (_err, files) => {
        files.forEach(file => {
            console.log(file);
            logger.info(file);
        });
    });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!