Avoid allowing access to unintended directories or files

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: javascript-express/path-traversal

Language: JavaScript

Severity: Warning

Category: Security

CWE: 22

Description

By not sanitizing user input prior to using it in path resolution methods you open your application’s access to unintended directories and files.

If you’re using replace on a user input, this rule will assume you’ve done so correctly and will not report a violation

Learn More

Non-Compliant Code Examples

const path = require("path");

app.get("/", (req, res) => {
  path.join("/user/", req.params.path)

  var pathname = path.join("/public/", req.body.foo)
  path.resolve(pathname)

  path.resolve(__dirname, req.body.foo)
  path.resolve(__dirname, `${req.body.foo}`)
})

Compliant Code Examples

const path = require("path");

app.get("/", (req, res) => {
  path.join("/user/", req.params.path.replace(/^(\.\.(\/|\\|$))+/, ''))

  var pathname = path.join("/public/", req.body.foo.replace(/^(\.\.(\/|\\|$))+/, ''))
  path.resolve(pathname)

  path.resolve(__dirname, req.body.foo.replace(/^(\.\.(\/|\\|$))+/, ''))
  path.resolve(__dirname, `${req.body.foo.replace(/^(\.\.(\/|\\|$))+/, '')}`)
})
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis