You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-08-10 22:11:50 +02:00
Server: Trying to fix a request parsing error that can potentially crash the error (#12860)
This commit is contained in:
@@ -75,29 +75,49 @@ export async function formParse(request: IncomingMessage): Promise<FormParseResu
|
|||||||
// headers
|
// headers
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
||||||
return new Promise((resolve: Function, reject: Function) => {
|
return new Promise((resolve: Function, reject: Function) => {
|
||||||
|
let promiseCompleted = false;
|
||||||
|
|
||||||
const form = formidable({
|
const form = formidable({
|
||||||
allowEmptyFiles: true,
|
allowEmptyFiles: true,
|
||||||
minFileSize: 0,
|
minFileSize: 0,
|
||||||
});
|
});
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
|
||||||
form.parse(req, (error: any, fields: Fields, files: Files) => {
|
|
||||||
if (error) {
|
|
||||||
error.message = `Could not parse form: ${error.message}`;
|
|
||||||
reject(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Formidable seems to be doing some black magic and once a request
|
try {
|
||||||
// has been parsed it cannot be parsed again. Doing so will do
|
form.on('error', (error) => {
|
||||||
// nothing, the code will just end there, or maybe wait
|
if (promiseCompleted) return;
|
||||||
// indefinitely. So we cache the result on success and return it if
|
promiseCompleted = true;
|
||||||
// some code somewhere tries again to parse the form.
|
const wrapped = new Error(`Could not parse form (1): ${error.message}`);
|
||||||
req.__parsed = {
|
reject(wrapped);
|
||||||
fields: isFormContentType ? convertFieldsToKeyValue(fields) : fields,
|
});
|
||||||
files: convertFieldsToKeyValue(files),
|
|
||||||
};
|
form.parse(req, (error: Error, fields: Fields, files: Files) => {
|
||||||
resolve(req.__parsed);
|
if (promiseCompleted) return;
|
||||||
});
|
promiseCompleted = true;
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
error.message = `Could not parse form (2): ${error.message}`;
|
||||||
|
reject(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Formidable seems to be doing some black magic and once a request
|
||||||
|
// has been parsed it cannot be parsed again. Doing so will do
|
||||||
|
// nothing, the code will just end there, or maybe wait
|
||||||
|
// indefinitely. So we cache the result on success and return it if
|
||||||
|
// some code somewhere tries again to parse the form.
|
||||||
|
req.__parsed = {
|
||||||
|
fields: isFormContentType ? convertFieldsToKeyValue(fields) : fields,
|
||||||
|
files: convertFieldsToKeyValue(files),
|
||||||
|
};
|
||||||
|
resolve(req.__parsed);
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (promiseCompleted) return;
|
||||||
|
promiseCompleted = true;
|
||||||
|
|
||||||
|
const wrapped = new Error(`Could not parse form (3): ${error.message}`);
|
||||||
|
reject(wrapped);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user