Skip to content

Commit 002b9b0

Browse files
committed
[Fix] append: avoid a crash on nullish values
Fixes #577
1 parent 70bbaa0 commit 002b9b0

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

lib/form_data.js

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

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

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

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

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

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

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

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

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)