-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid non-blob webresources fields on multipart requests #746
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,7 @@ export const getWebresourceHandler = (): WebResourceHandler | undefined => { | |
const isFileInValidPath = async ( | ||
fieldname: string, | ||
req: Express.Request, | ||
odataRequest: uriParser.ParsedODataRequest, | ||
): Promise<boolean> => { | ||
if (req.method !== 'POST' && req.method !== 'PATCH') { | ||
return false; | ||
|
@@ -77,10 +78,6 @@ const isFileInValidPath = async ( | |
return false; | ||
} | ||
const model = getModel(apiRoot); | ||
const odataRequest = uriParser.parseOData({ | ||
url: req.url, | ||
method: req.method, | ||
}); | ||
const sqlResourceName = sbvrUtils.resolveSynonym(odataRequest); | ||
|
||
const table = model.abstractSql.tables[sqlResourceName]; | ||
|
@@ -124,6 +121,15 @@ export const getUploaderMiddlware = ( | |
const bb = busboy({ headers: req.headers }); | ||
let isAborting = false; | ||
|
||
const parsedOdataRequest = uriParser.parseOData({ | ||
otaviojacobi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
url: req.url, | ||
method: req.method, | ||
}); | ||
const webResourcesFieldNames = getWebResourceFields( | ||
parsedOdataRequest, | ||
false, | ||
); | ||
|
||
const finishFileUpload = () => { | ||
req.unpipe(bb); | ||
req.on('readable', req.read.bind(req)); | ||
|
@@ -151,7 +157,9 @@ export const getUploaderMiddlware = ( | |
completeUploads.push( | ||
(async () => { | ||
try { | ||
if (!(await isFileInValidPath(fieldname, req))) { | ||
if ( | ||
!(await isFileInValidPath(fieldname, req, parsedOdataRequest)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a later improvement there's a lot of work done in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in my todo list for webresources |
||
) { | ||
filestream.resume(); | ||
return; | ||
} | ||
|
@@ -183,6 +191,14 @@ export const getUploaderMiddlware = ( | |
// This receives the form fields and transforms them into a standard JSON body | ||
// This is a similar behavior as previous multer library did | ||
bb.on('field', (name, val) => { | ||
if (webResourcesFieldNames.includes(name)) { | ||
isAborting = true; | ||
bb.emit( | ||
'error', | ||
new errors.BadRequestError('WebResource field must be a blob.'), | ||
); | ||
return; | ||
} | ||
req.body[name] = val; | ||
}); | ||
|
||
|
@@ -207,17 +223,17 @@ export const getUploaderMiddlware = ( | |
} | ||
}); | ||
|
||
bb.on('error', async (err) => { | ||
bb.on('error', async (err: Error) => { | ||
await clearFiles(); | ||
finishFileUpload(); | ||
next(err); | ||
sbvrUtils.handleHttpErrors(req, res, err); | ||
}); | ||
req.pipe(bb); | ||
}; | ||
}; | ||
|
||
const getWebResourceFields = ( | ||
request: uriParser.ODataRequest, | ||
request: uriParser.ODataRequest | uriParser.ParsedODataRequest, | ||
useTranslations = true, | ||
): string[] => { | ||
// Translations will use modifyFields(translated) rather than fields(original) so we need to | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we allow multipart requests on requests that aren't POST/PATCH?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Page- We don't do anything, basically. Whatever is the content of multpart/body in those requests is just dropped. IMHO it is the expected behaviour as it is the same thing of a GET request with a body (it works, but the body is just ignored as it should not be there).