Skip to content

Commit 53bdc35

Browse files
committed
Merge pull request #347 from bitcraftCoLtd/master
Improvement on Content-Disposition parsing
2 parents b7ef166 + af6bd44 commit 53bdc35

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lib/incoming_form.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,11 @@ IncomingForm.prototype._initMultipart = function(boundary) {
352352
headerField = headerField.toLowerCase();
353353
part.headers[headerField] = headerValue;
354354

355-
var m = headerValue.match(/\bname="([^"]+)"/i);
355+
// matches either a quoted-string or a token (RFC 2616 section 19.5.1)
356+
var m = headerValue.match(/\bname=("([^"]*)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))/i);
356357
if (headerField == 'content-disposition') {
357358
if (m) {
358-
part.name = m[1];
359+
part.name = m[2] || m[3] || '';
359360
}
360361

361362
part.filename = self._fileName(headerValue);
@@ -421,10 +422,12 @@ IncomingForm.prototype._initMultipart = function(boundary) {
421422
};
422423

423424
IncomingForm.prototype._fileName = function(headerValue) {
424-
var m = headerValue.match(/\bfilename="(.*?)"($|; )/i);
425+
// matches either a quoted-string or a token (RFC 2616 section 19.5.1)
426+
var m = headerValue.match(/\bfilename=("(.*?)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))($|;\s)/i);
425427
if (!m) return;
426428

427-
var filename = m[1].substr(m[1].lastIndexOf('\\') + 1);
429+
var match = m[2] || m[3] || '';
430+
var filename = match.substr(match.lastIndexOf('\\') + 1);
428431
filename = filename.replace(/%22/g, '"');
429432
filename = filename.replace(/&#([\d]{4});/g, function(m, code) {
430433
return String.fromCharCode(code);

0 commit comments

Comments
 (0)