Unsanitized dynamic input in file path

  • Rule ID: javascript_lang_path_traversal
  • Languages: javascript
  • Source: path_traversal.yml

Description

Allowing unsanitized dynamic input in path resolution methods means an attacker could gain access to files and folders outside of the intended scope.

Remediations

❌ Avoid wherever possible

✅ Sanitize dynamic input when resolving paths, for example:

  • Use replace() to mitigate against unwanted patterns in the path (such as \..\..)
  • Actively guard against paths that end in "%00" (poison NULL byte attacks)
  • Use path concatenation to ensure the intended scope is respected
const path = require("path");

function(target) {
if (target.indexOf('\0')) !== -1 {
// prevent access
}

var folder = target.replace(/^(\.\.(\/|\\|$))+/, '')

var pathname = path.join("/public/", folder)
if pathname.indexOf("/public/") !== 0 {
// prevent access
}

path.resolve(pathname)
})

Resources

Associated CWE

OWASP Top 10

Configuration

To skip this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --skip-rule=javascript_lang_path_traversal

To run only this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --only-rule=javascript_lang_path_traversal