# Unsanitized User input in HTTP Send file request

## Overview

* **Rule ID**: `javascript_express_external_file_upload`
* **Applicable Languages**: Javascript
* **Weakness ID**: CWE-73

## Description

Using unsanitized user input in the sendFile API call exposes your application to path manipulation attacks. This vulnerability allows attackers to access files and resources they shouldn't by manipulating file paths in requests.

## Remediation Guidelines

* **Set** the root option to an absolute path when using sendFile. This restricts accessible files to a specific directory, preventing attackers from accessing files outside of this directory.

  ```javascript
  app.post("/upload", (req, res) => {
     var options = {
       root: path.join(__dirname, "upload")
     };
     res.sendFile(req.params.filename, options);
  });
  ```

## References

* [**Express sendFile API reference**](http://expressjs.com/en/5x/api.html#res.sendFile)
* [**CWE-73: External Control of File Name or Path**](https://cwe.mitre.org/data/definitions/73.html)
* [**OWASP Top 10: A04:2021 - Insecure Design**](https://owasp.org/Top10/A04_2021-Insecure_Design/)

## Configuration

To omit this rule during a scan, and to provide you with continuous 24/7 code-level scanning, you can employ our [**SAST TOOL**](https://scopy.sec1.io/login)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sec1.io/user-docs/4-sast/3-javascript/unsanitized-user-input-in-http-send-file-request.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
