Fix 500 when page[limit]=0 is requested#4775
Open
linkrobins wants to merge 1 commit into
Open
Conversation
`RequestUtil::extractLimit()` used a loose `! $limit` check that also matched the string "0", returning null. That null was then assigned to `OffsetPagination`'s non-nullable `int $limit`, throwing a TypeError and producing a 500 on any offset-paginated endpoint. Use a strict `=== null` check so a genuinely absent limit still returns null (the "no limit" path), while an explicit `page[limit]=0` falls through to the existing `< 1` guard and returns a 400, consistent with how negative limits are already handled. Fixes flarum#4773
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #4773.
Problem
Any request to an offset-paginated endpoint with
page[limit]=0returns a 500:This is what was recorded on discuss in #4773. It reproduces on a clean install with no extensions.
Cause
RequestUtil::extractLimit()short-circuits withnullon a falsy limit:The string
"0"is falsy, so it returnsnullbefore reaching the< 1validation just below. Thatnullis then assigned toOffsetPagination::$limit, which is a non-nullableint, so PHP throws.The boundary confirms the mechanism — only the literal
0hits this.-5,00,0.0are all rejected with a clean 400, because they are truthy strings that reach the< 1guard.Fix
Use a strict
=== nullcheck. A genuinely absent limit (nopage[limit]and no default) still returnsnull, but an explicitpage[limit]=0now falls through to the existing< 1guard and returns a 400, consistent with how negative limits are already handled.Added unit tests in
RequestUtilTestcovering an explicit limit, capping at max, the default fallback, the no-limitnullpath, and rejection of0and negative values.