fix: validate that limit option is non-negative#706
fix: validate that limit option is non-negative#706abhu85 wants to merge 1 commit intoexpressjs:masterfrom
Conversation
|
Related to #698. I think we should also throw an error/warn the user when using a zero limit because the middleware would return a |
|
Thanks for the feedback @Phillip9587! You make a good point about zero limits - they would effectively make the middleware unusable since every request with a body would get HTTP 413. I see you have a more comprehensive fix in #698. Would you prefer:
Happy to go whichever direction works best for the project! |
UlisesGascon
left a comment
There was a problem hiding this comment.
I think is a good addition, but semver-major at this stage probably someone is relaying on this bug. I agree with the @Phillip9587 concerns 👍
|
@Phillip9587 Any idea when #698 might be merged? Happy to rebase this PR once that lands. |
|
@abhu85 I'm sorry. Still waiting for reviews and a descicion from the team. |
|
We've had a discussion recently at the express level about how we feel about adding runtime validation like this. We're still evolving our stance, but this stands out to me as a good example. I'm wary of this kind of validation for a few reasons. One is the simple "opening a can of worms" idea, or stated another way, if we validate the content of the options beyond their types, when do we stop validating? The second is, why do we even need to do this validation? As in, what abstraction or design decision has led us to accept a valid input type that has unclear user intent but a unique outcome (see below)? In this case, it is very clear that a user would have done this by mistake. The result of passing a negative limit is that all requests with a body (even That is a bug IMO in raw-body. A negative limit is mathematically nonsense here. I think raw-body should validate the input itself as it actually uses it, and it already validates its other inputs. That way every consumer gets the fix, not just body-parser. However, a major rev to raw-body with a throw for negatives is functionally the same as a major rev here and doing the same. |
|
I dont think we should throw on zero btw. It is a choice that seems silly, but there is no other way to express "accept only Content-Length: 0" which is spec valid. |
|
Semver on this is eh, major bc of the throw but no extremely clear broken state. But even moreso, this is low enough priority that it can just wait for a major rev |
|
Thanks for the review feedback @jonchurch! I understand this should target the next major branch. Since there isn't one pushed yet, should I:
Happy to do whatever works best for the project's workflow. |
Add validation to reject negative limit values like '-100kb' that were previously silently accepted. This prevents configuration errors from going unnoticed. Fixes expressjs#705
f9c7082 to
284d6e0
Compare
Summary
This PR adds validation to reject negative
limitvalues like'-100kb'that were previously silently accepted.Problem
Currently,
body-parseraccepts negative limit values without validation:This is problematic because:
Solution
Added validation in
normalizeOptions()to throw aTypeErrorwhen a negative limit is provided:This applies to all parsers (
json,urlencoded,raw,text) since they all usenormalizeOptions().Test Plan
'-100kb')-1024)0and'0kb')Fixes #705