Skip to content

Commit 773dfa3

Browse files
Merge pull request #356 from RomanBurunkov/master
Fix issue #342:URI malformed error.
2 parents ee8e711 + 2ba446a commit 773dfa3

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/utilities.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,26 @@ const saveBufferToFile = (buffer, filePath, callback) => {
229229
* @returns {String}
230230
*/
231231
const uriDecodeFileName = (opts, fileName) => {
232-
return opts.uriDecodeFileNames ? decodeURIComponent(fileName) : fileName;
232+
const options = opts || {};
233+
if (!options.uriDecodeFileNames) {
234+
return fileName;
235+
}
236+
// Decode file name from URI with checking URI malformed errors.
237+
// See Issue https://github.com/richardgirges/express-fileupload/issues/342.
238+
try {
239+
return decodeURIComponent(fileName);
240+
} catch (err) {
241+
const matcher = /(%[a-f0-9]{2})/gi;
242+
return fileName.split(matcher)
243+
.map((str) => {
244+
try {
245+
return decodeURIComponent(str);
246+
} catch (err) {
247+
return '';
248+
}
249+
})
250+
.join('');
251+
}
233252
};
234253

235254
/**

test/utilities.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ describe('utilities: Test of the utilities functions', function() {
382382
const testData = [
383383
{ enc: 'test%22filename', dec: 'test"filename' },
384384
{ enc: 'test%60filename', dec: 'test`filename' },
385-
{ enc: '%3Fx%3Dtest%22filename', dec: '?x=test"filename'}
385+
{ enc: '%3Fx%3Dtest%22filename', dec: '?x=test"filename'},
386+
{ enc: 'bug_bounty_upload_%91%91and%92.txt', dec: 'bug_bounty_upload_and.txt'}
386387
];
387388

388389
// Test decoding if uriDecodeFileNames: true.

0 commit comments

Comments
 (0)