# file-uploads

Rule `sonarjs/file-uploads`. File uploads should be restricted

- Source: sonarjs
- Technologies: JavaScript
- Kind: correctness
- Needs type information: no
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1214 by reach, #515 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/file-uploads` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-2598/) |

## Code examples

### Incorrect: the rule reports this

```js
const Formidable = require('formidable');

const form = new Formidable(); // Noncompliant, this form is not safe
form.uploadDir = ""; // because upload dir is not defined (by default os temp dir: /var/tmp or /tmp)
form.keepExtensions = true; // and file extensions are kept
```

```js
const multer = require('multer');

let diskStorage = multer.diskStorage({ // Noncompliant: no destination specified
  filename: (req, file, cb) => {
    const buf = crypto.randomBytes(20);
    cb(null, buf.toString('hex'))
  }
});

// This upload is not safe as no destination specified, /var/tmp or /tmp will be used
let diskupload = multer({
  storage: diskStorage,
});
```

### Correct: the rule accepts this

```js
const Formidable = require('formidable');

const form = new Formidable(); // Compliant
form.uploadDir = "./uploads/";
form.keepExtensions = false;
```

```js
const multer = require('multer');

let diskStorage = multer.diskStorage({  // Compliant
  filename: (req, file, cb) => {
    const buf = crypto.randomBytes(20);
    cb(null, buf.toString('hex'))
  },
  destination: (req, file, cb) => {
    cb(null, './uploads/')
  }
});

let diskupload = multer({
  storage: diskStorage,
});
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/file-uploads": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/file-permissions](https://lintradar.dev/rules/sonarjs/file-permissions.md): File permissions should not be set to world-accessible values
- [sonarjs/fixme-tag](https://lintradar.dev/rules/sonarjs/fixme-tag.md): Track uses of "FIXME" tags
- [sonarjs/explicit-test-skip](https://lintradar.dev/rules/sonarjs/explicit-test-skip.md): Tests should be skipped explicitly
- [sonarjs/for-loop-increment-sign](https://lintradar.dev/rules/sonarjs/for-loop-increment-sign.md): A "for" loop update clause should move the counter in the right direction
- [sonarjs/existing-groups](https://lintradar.dev/rules/sonarjs/existing-groups.md): Replacement strings should reference existing regular expression groups
- [sonarjs/function-inside-loop](https://lintradar.dev/rules/sonarjs/function-inside-loop.md): Functions should not be defined inside loops

---

Web page: https://lintradar.dev/rules/sonarjs/file-uploads. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
