Skip to content

Commit

Permalink
Ensure contentDisposition.parse is not parsing undefined values
Browse files Browse the repository at this point in the history
Signed-off-by: Jeremy Ho <jujaga@gmail.com>
  • Loading branch information
jujaga committed Aug 9, 2023
1 parent 7b30ccf commit 07f9fa1
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions app/src/middleware/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ const currentUpload = (strict = false) => {
if (!contentLength) return new Problem(411, { detail: 'Content-Length must be greater than 0' }).send(res);

// Check Content-Disposition Header
const disposition = req.get('Content-Disposition');
let filename;
if (strict && !disposition) return new Problem(415, { detail: 'Content-Disposition header missing' }).send(res);
try {
const { type, parameters } = contentDisposition.parse(disposition);
if (strict && !type || type !== 'attachment') return new Error('Disposition type is not \'attachment\'');
if (strict && !parameters?.filename) return new Error('Disposition missing \'filename\' parameter');
filename = parameters?.filename;
} catch (e) {
return new Problem(400, { detail: `Content-Disposition header error: ${e.message}` }).send(res);
const disposition = req.get('Content-Disposition');
if (disposition) {
try {
const { type, parameters } = contentDisposition.parse(disposition);
if (strict && !type || type !== 'attachment') return new Error('Disposition type is not \'attachment\'');
if (strict && !parameters?.filename) return new Error('Disposition missing \'filename\' parameter');
filename = parameters?.filename;
} catch (e) {
return new Problem(400, { detail: `Content-Disposition header error: ${e.message}` }).send(res);
}
} else {
if (strict) return new Problem(415, { detail: 'Content-Disposition header missing' }).send(res);
}

// Check Content-Type Header
Expand Down

0 comments on commit 07f9fa1

Please sign in to comment.