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
Support for checking empty JSON body. #374
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ var FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*(.)/ // eslint-disable-line no-cont | |
* @public | ||
*/ | ||
|
||
function json (options) { | ||
function json(options) { | ||
var opts = options || {} | ||
|
||
var limit = typeof opts.limit !== 'number' | ||
|
@@ -68,20 +68,24 @@ function json (options) { | |
? typeChecker(type) | ||
: type | ||
|
||
function parse (body) { | ||
if (body.length === 0) { | ||
// special-case empty json body, as it's a common client-side mistake | ||
// TODO: maybe make this configurable or part of "strict" option | ||
return {} | ||
} | ||
|
||
function parse(body) { | ||
if (strict) { | ||
var first = firstchar(body) | ||
// BEGIN - Check empty body | ||
if (body.length === 0) { | ||
debug('strict violation') | ||
throw createEmptyBodyError() | ||
} | ||
// END - Check empty body | ||
|
||
var first = firstchar(body) | ||
if (first !== '{' && first !== '[') { | ||
debug('strict violation') | ||
throw createStrictSyntaxError(body, first) | ||
} | ||
} else { | ||
if (body.length === 0) { | ||
return {} | ||
} | ||
} | ||
|
||
try { | ||
|
@@ -95,7 +99,7 @@ function json (options) { | |
} | ||
} | ||
|
||
return function jsonParser (req, res, next) { | ||
return function jsonParser(req, res, next) { | ||
if (req._body) { | ||
debug('body already parsed') | ||
next() | ||
|
@@ -104,12 +108,15 @@ function json (options) { | |
|
||
req.body = req.body || {} | ||
|
||
// BEGIN - Check empty body | ||
/* | ||
// skip requests without bodies | ||
if (!typeis.hasBody(req)) { | ||
debug('skip empty body') | ||
next() | ||
return | ||
} | ||
} */ | ||
// END - Check empty body | ||
|
||
debug('content-type %j', req.headers['content-type']) | ||
|
||
|
@@ -150,15 +157,34 @@ function json (options) { | |
* @private | ||
*/ | ||
|
||
function createStrictSyntaxError (str, char) { | ||
function createStrictSyntaxError(str, char) { | ||
var index = str.indexOf(char) | ||
var partial = str.substring(0, index) + '#' | ||
|
||
try { | ||
JSON.parse(partial); /* istanbul ignore next */ throw new SyntaxError('strict violation') | ||
} catch (e) { | ||
return normalizeJsonSyntaxError(e, { | ||
message: e.message.replace('#', char), | ||
message: e.message.replace('#', char) + ' NS ', | ||
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. I'm not sure I understand this message here. |
||
stack: e.stack | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* Create empty violation syntax error matching native error. | ||
* | ||
* @param {string} str | ||
* @param {string} char | ||
* @return {Error} | ||
* @private | ||
*/ | ||
function createEmptyBodyError() { | ||
try { | ||
throw Error() | ||
} catch (e) { | ||
return normalizeJsonSyntaxError(e, { | ||
message: 'Empty JSON body received.', | ||
stack: e.stack | ||
}) | ||
} | ||
|
@@ -172,7 +198,7 @@ function createStrictSyntaxError (str, char) { | |
* @private | ||
*/ | ||
|
||
function firstchar (str) { | ||
function firstchar(str) { | ||
return FIRST_CHAR_REGEXP.exec(str)[1] | ||
} | ||
|
||
|
@@ -183,7 +209,7 @@ function firstchar (str) { | |
* @api private | ||
*/ | ||
|
||
function getCharset (req) { | ||
function getCharset(req) { | ||
try { | ||
return (contentType.parse(req).parameters.charset || '').toLowerCase() | ||
} catch (e) { | ||
|
@@ -199,7 +225,7 @@ function getCharset (req) { | |
* @return {SyntaxError} | ||
*/ | ||
|
||
function normalizeJsonSyntaxError (error, obj) { | ||
function normalizeJsonSyntaxError(error, obj) { | ||
var keys = Object.getOwnPropertyNames(error) | ||
|
||
for (var i = 0; i < keys.length; i++) { | ||
|
@@ -223,8 +249,8 @@ function normalizeJsonSyntaxError (error, obj) { | |
* @return {function} | ||
*/ | ||
|
||
function typeChecker (type) { | ||
return function checkType (req) { | ||
function typeChecker(type) { | ||
return function checkType(req) { | ||
return Boolean(typeis(req, type)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I believe this has been left commented out on accident -- this is how all the parsers work, they only parse bodies and this does not parse when there is no body at all (this is different from a zero-length body, i.e. empty string, which AFAIK is that you are adding an error for).