Skip to content

Commit

Permalink
💥 End requests with an error when parsing posted body contents fail
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Oct 10, 2024
1 parent 6cbdd95 commit f2aa9c4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.4.0-alpha.7 (WIP)

* End requests with an error when parsing posted body contents fail

## 1.4.0-alpha.6 (2024-09-11)

* Don't flatten objects when updating records, it causes too many issues
Expand Down
94 changes: 54 additions & 40 deletions lib/core/alchemy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1750,7 +1750,7 @@ Alchemy.setMethod(function addAppcacheEntry(entry) {
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.1.0
* @version 1.3.18
* @version 1.4.0
*
* @param {IncomingMessage} req
* @param {OutgoingMessage} res Optional
Expand Down Expand Up @@ -1787,14 +1787,24 @@ Alchemy.setMethod(function parseRequestBody(req, res, callback) {

form.parse(req, function parsedMultipart(err, form_fields, form_files) {

var fields = {},
files = {},
key;
if (err) {

if (err && req.conduit && req.conduit.aborted) {
return callback(null);
// Ignore the error if the request was already aborted
if (conduit?.aborted) {
return callback(null);
}

if (conduit) {
return conduit.error(err);
}

return callback(err);
}

let fields = {},
files = {},
key;

// Since formidable v3, all the fields are now arrays.
// We already had a lot of logic to deal with this,
// so we just have to un-array everything
Expand All @@ -1811,18 +1821,12 @@ Alchemy.setMethod(function parseRequestBody(req, res, callback) {
Object.setFormPath(files, key, form_files[key]);
}

if (err) {
log.error('Error parsing multipart POST', {err: err});
req.body = {};
req.files = {};
} else {
req.body = fields;
req.files = files;
req.body = fields;
req.files = files;

if (conduit) {
conduit.setRequestBody(fields);
conduit.setRequestFiles(files);
}
if (conduit) {
conduit.setRequestBody(fields);
conduit.setRequestFiles(files);
}

callback(null, fields);
Expand All @@ -1836,22 +1840,27 @@ Alchemy.setMethod(function parseRequestBody(req, res, callback) {

this.url_form_body(req, res, function parsedBody(err) {

if (err && req.conduit && req.conduit.aborted) {
return callback(null);
if (err) {

// Ignore the error if the request was already aborted
if (conduit?.aborted) {
return callback(null);
}

if (conduit) {
return conduit.error(err);
}

return callback(err);
}

// You can't send files using a regular post
req.files = {};

if (err) {
log.error('Error parsing x-www-form-urlencoded body data', {err: err});
req.body = {};
} else {
req.body = req.body;
req.body = req.body;

if (conduit) {
conduit.setRequestBody(req.body);
}
if (conduit) {
conduit.setRequestBody(req.body);
}

callback(null, req.body);
Expand All @@ -1861,25 +1870,30 @@ Alchemy.setMethod(function parseRequestBody(req, res, callback) {
}

// Any other encoded data (like JSON)
this.any_body(req, function parsedBody(err, body) {
this.any_body(req, res, function parsedBody(err, body) {

function handleResponse(err, body) {
if (err && req.conduit && req.conduit.aborted) {
return callback(null);

if (err) {

// Ignore the error if the request was already aborted
if (conduit?.aborted) {
return callback(null);
}

if (conduit) {
return conduit.error(err);
}

return callback(err);
}

// You can't send files using a regular post
req.files = {};
req.body = body;

if (err) {
log.error('Error parsing body data', {err: err});
req.body = {};
} else {
req.body = body;

if (conduit) {
conduit.setRequestBody(body);
}
if (conduit) {
conduit.setRequestBody(body);
}

callback(null, req.body);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "alchemymvc",
"description": "MVC framework for Node.js",
"version": "1.4.0-alpha.6",
"version": "1.4.0-alpha.7",
"author": "Jelle De Loecker <jelle@elevenways.be>",
"keywords": [
"alchemy",
Expand Down

0 comments on commit f2aa9c4

Please sign in to comment.