Content-Length: 413158 | pFad | https://github.com/form-data/form-data/commit/b70869dad20175aad0230ad5f1d030fb76232df8

2A [Fix] `append`: avoid a crash on nullish values · form-data/form-data@b70869d · GitHub
Skip to content

Commit b70869d

Browse files
committed
[Fix] append: avoid a crash on nullish values
Fixes #577
1 parent 131ae5e commit b70869d

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

lib/form_data.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ FormData.prototype.append = function (field, value, options) {
5757
var append = CombinedStream.prototype.append.bind(this);
5858

5959
// all that streamy business can't handle numbers
60-
if (typeof value === 'number') {
60+
if (typeof value === 'number' || value == null) {
6161
value = String(value);
6262
}
6363

@@ -231,14 +231,14 @@ FormData.prototype._getContentDisposition = function (value, options) {
231231
if (typeof options.filepath === 'string') {
232232
// custom filepath for relative paths
233233
filename = path.normalize(options.filepath).replace(/\\/g, '/');
234-
} else if (options.filename || value.name || value.path) {
234+
} else if (options.filename || (value && (value.name || value.path))) {
235235
/*
236236
* custom filename take precedence
237237
* formidable and the browser add a name property
238238
* fs- and request- streams have path property
239239
*/
240-
filename = path.basename(options.filename || value.name || value.path);
241-
} else if (value.readable && hasOwn(value, 'httpVersion')) {
240+
filename = path.basename(options.filename || (value && (value.name || value.path)));
241+
} else if (value && value.readable && hasOwn(value, 'httpVersion')) {
242242
// or try http response
243243
filename = path.basename(value.client._httpMessage.path || '');
244244
}
@@ -256,17 +256,17 @@ FormData.prototype._getContentType = function (value, options) {
256256
var contentType = options.contentType;
257257

258258
// or try `name` from formidable, browser
259-
if (!contentType && value.name) {
259+
if (!contentType && value && value.name) {
260260
contentType = mime.lookup(value.name);
261261
}
262262

263263
// or try `path` from fs-, request- streams
264-
if (!contentType && value.path) {
264+
if (!contentType && value && value.path) {
265265
contentType = mime.lookup(value.path);
266266
}
267267

268268
// or if it's http-reponse
269-
if (!contentType && value.readable && hasOwn(value, 'httpVersion')) {
269+
if (!contentType && value && value.readable && hasOwn(value, 'httpVersion')) {
270270
contentType = value.headers['content-type'];
271271
}
272272

@@ -276,7 +276,7 @@ FormData.prototype._getContentType = function (value, options) {
276276
}
277277

278278
// fallback to the default content type if `value` is not simple value
279-
if (!contentType && typeof value === 'object') {
279+
if (!contentType && value && typeof value === 'object') {
280280
contentType = FormData.DEFAULT_CONTENT_TYPE;
281281
}
282282

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
var common = require('../common');
4+
var assert = common.assert;
5+
6+
var FormData = require(common.dir.lib + '/form_data');
7+
8+
(function testSetUndefined() {
9+
var form = new FormData();
10+
11+
assert.doesNotThrow(function () {
12+
form.append('key', undefined);
13+
});
14+
15+
var buffer = form.getBuffer();
16+
17+
assert.deepEqual(buffer.toString().split(form.getBoundary()), [
18+
'--',
19+
'\r\nContent-Disposition: form-data; name="key"\r\n\r\nundefined\r\n--',
20+
'--\r\n',
21+
]);
22+
}());

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/form-data/form-data/commit/b70869dad20175aad0230ad5f1d030fb76232df8

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy