-
-
Notifications
You must be signed in to change notification settings - Fork 747
perf(urlencoded): optimize parameter counting for better memory efficiency #652
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
base: master
Are you sure you want to change the base?
Conversation
…iency
The previous implementation used �ody.split('&') which always
processed the entire request body and allocated a full array,
regardless of the parameter limit.
The new implementation:
- Counts '&' characters iteratively without array allocation
- Exits immediately when the limit is reached
- Handles edge case of empty/null body
- Reduces time complexity from O(n) worst-case always to O(min(n, limit))
This particularly improves resilience against malicious requests
with thousands of parameters attempting to exhaust server resources
6c95239 to
10958e6
Compare
|
Thank you for the contribution, this has already been resolved in our recent security patch. |
Hey @bjohansebas , just wanted to flag something about the I had opened PR #652 back on Oct 29 to fix this exact issue - basically the same DoS vulnerability you patched. My version kept the original behavior while adding the early exit logic. The thing is, the implementation that got merged (commit Quick example with
Or for
The issue is the Not trying to reopen my PR or anything, just thought someone should know since this changes behavior that might be relied on elsewhere. Could cause weird issues down the line even though it's technically internal. |
|
Hey @Ayoub-Mabrouk, you are right. The security fix implementation is off by 1 compared to the original implementation. But it is actually more correct then the old implementation with some small exceptions: This actually is one parameter so both the old 1.x and the split based version were wrong. Same here 1.x and split based are wrong All our tests are written without the leading "&" so don't know if this is valid? This is the case that needs some fixup i think. But it is not critical as arrayLimit is set to I already talked to @UlisesGascon and he suggested opening a PR which adds tests and documentation for the expected behaviour of this function. |
- Fix parameterCount to correctly count parameters (ampersands + 1) - Handle empty string edge case (returns 0, not 1) - Optimize using indexOf instead of character iteration - Add comprehensive tests documenting expected behavior - Address edge cases: leading/trailing ampersands, consecutive ampersands
|
Hi @UlisesGascon, I’ve added the fix for parameterCount along with tests covering all edge cases |
|
Should it just defer to |
|
@bjohansebas @Phillip9587 as you can see I've implemented a fix again by removing the doWhile and added tests for it. |
The previous implementation used
body.split('&')which always processed the entire request body and allocated a full array, regardless of the parameter limit.The new implementation:
parameterLimitoption.This particularly improves resilience against malicious requests with thousands of parameters attempting to exhaust server resources.