Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter upload #716

Merged
merged 37 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
777a04f
feat: allow to use filename option without keepExtension
GrosSacASac Feb 18, 2021
5531a57
feat: can use uploadDir option and filename together
GrosSacASac Feb 18, 2021
33dd6c4
refactor: rename
GrosSacASac Feb 18, 2021
ebb7a18
feat: by default prevent directory traversal attacks
GrosSacASac Feb 18, 2021
cb3d359
refactor: make octet stream less divergent
GrosSacASac Feb 18, 2021
aafebea
refactor: pass through, avoid renaming variable names
GrosSacASac Feb 18, 2021
0a5bad4
refactor: prefer Object.assign
GrosSacASac Feb 18, 2021
032e66b
feat: pass newName
GrosSacASac Feb 18, 2021
ad1664a
feat: give createFileWriteStream more, including newName
GrosSacASac Feb 18, 2021
c178080
docs: update
GrosSacASac Feb 18, 2021
f600f32
docs: update examples
GrosSacASac Feb 18, 2021
3ca6382
fix: fix missing variables
GrosSacASac Feb 18, 2021
df66b4f
feat: remove duplicate fix tests
GrosSacASac Feb 18, 2021
e0576e1
lint: lint
GrosSacASac Feb 18, 2021
59a9b2b
refactor: rename mime into mimetype
GrosSacASac Feb 19, 2021
2ca0b78
refactor: explicit this.options.keepExtensions !== true
GrosSacASac Feb 19, 2021
e4b7fe6
tests: reverse expectation order
GrosSacASac Feb 19, 2021
3481057
tests: rename to xname to avoid confusion
GrosSacASac Feb 19, 2021
bef35f2
refactor: inline old _uploadPath
GrosSacASac Feb 19, 2021
45c9213
refactor: rename newName into newFileName
GrosSacASac Feb 19, 2021
7edf4bb
refactor: rename filename into originalFilename
GrosSacASac Feb 19, 2021
8340df2
refactor: direcly filepath
GrosSacASac Feb 26, 2021
63402a1
fix: test
GrosSacASac Feb 26, 2021
9e54b19
refactor: split hash into hashAlgorithm and hash
GrosSacASac Feb 26, 2021
4f09883
feat: this.lastModifiedDate = null remains
GrosSacASac Feb 26, 2021
2d85616
refactor: finalpath: filepath
GrosSacASac Feb 26, 2021
48f25fc
fix: change order
GrosSacASac Feb 26, 2021
3c14493
refactor: better be explicit
GrosSacASac Feb 26, 2021
58e4a61
feat: display more in toString
GrosSacASac Feb 26, 2021
4ff8447
docs: update changelog
GrosSacASac Mar 2, 2021
fec82d3
chore: update version
GrosSacASac Mar 2, 2021
738ff5c
Merge branch 'master' into safe-default-filename
GrosSacASac Mar 3, 2021
4bce5f7
fix: revert, renamed too much
GrosSacASac Mar 4, 2021
97181fe
fix: _flush is more appropriate
GrosSacASac Mar 5, 2021
7700225
feat: add upload filter
GrosSacASac Mar 11, 2021
a206d9a
docs: document options.filter
GrosSacASac Mar 11, 2021
86cac82
Merge branch 'master' into filter-upload
GrosSacASac Mar 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ See it's defaults in [src/Formidable.js DEFAULT_OPTIONS](./src/Formidable.js)
- `options.filename` **{function}** - default `undefined` Use it to control
newFilename. Must return a string. Will be joined with options.uploadDir.

- `options.filter` **{function}** - default function that always returns true.
Use it to filter files before they are uploaded. Must return a boolean.


#### `options.filename` **{function}** function (name, ext, part, form) -> string

_**Note:** If this size of combined fields, or size of some file is exceeded, an
Expand All @@ -372,6 +376,20 @@ form.bytesReceived;
form.bytesExpected;
```

#### `options.filter` **{function}** function ({name, originalFilename, mimetype}) -> boolean

**Note:** use an outside variable to cancel all uploads upon the first error

```js
const options {
filter: function ({name, originalFilename, mimetype}) {
// keep only images
return mimetype && mimetype.includes("image");
}
};
```


### .parse(request, callback)

Parses an incoming Node.js `request` containing form data. If `callback` is
Expand Down
25 changes: 15 additions & 10 deletions examples/with-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ const server = http.createServer((req, res) => {
multiples: true,
uploadDir: `uploads`,
keepExtensions: true,
filename(/*name, ext, part, form*/) {
/* name basename of the http originalFilename
ext with the dot ".txt" only if keepExtension is true
*/
// slugify to avoid invalid filenames
// substr to define a maximum length
// return `${slugify(name).${slugify(ext, separator: '')}`.substr(0, 100);
return 'yo.txt'; // or completly different name
// return 'z/yo.txt'; // subdirectory
},
// filename(/*name, ext, part, form*/) {
// /* name basename of the http originalFilename
// ext with the dot ".txt" only if keepExtension is true
// */
// // slugify to avoid invalid filenames
// // substr to define a maximum length
// // return `${slugify(name).${slugify(ext, separator: '')}`.substr(0, 100);
// return 'yo.txt'; // or completly different name
// // return 'z/yo.txt'; // subdirectory
// },
filter: function ({name, originalFilename, mimetype}) {
// keep only images
return mimetype && mimetype.includes("image");
}

});

form.parse(req, (err, fields, files) => {
Expand Down
7 changes: 7 additions & 0 deletions src/Formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const DEFAULT_OPTIONS = {
enabledPlugins: ['octetstream', 'querystring', 'multipart', 'json'],
fileWriteStreamHandler: null,
defaultInvalidName: 'invalid-name',
filter: function () {
return true;
},
};

const PersistentFile = require('./PersistentFile');
Expand Down Expand Up @@ -316,6 +319,10 @@ class IncomingForm extends EventEmitter {
return;
}

if (!this.options.filter(part)) {
return;
}

this._flushing += 1;

const newFilename = this._getNewName(part);
Expand Down