Reject params nested deeper than the array scope declares - #2838
Open
ericproulx wants to merge 1 commit into
Open
Reject params nested deeper than the array scope declares#2838ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
`AttributesIterator#do_each` recursed into any element that was an Array.
That recursion is required when the declaration itself nests array scopes —
`map_params` adds one level of nesting per Array-typed scope on the chain, so
the params for the inner scope really are an array of arrays — but nothing
compared the incoming depth against the declared one.
A request could therefore wrap its elements in extra arrays and have them
silently unwrapped:
params do
requires :lines, type: Array do
requires :book_id, type: String
requires :qty, type: Integer
end
end
`{"lines":[[{"book_id":"x","qty":1}]]}` passed validation, and `params[:lines]`
/ `declared` then handed the endpoint `[[{...}]]`. Any ordinary body assuming
the declared shape (`params[:lines].sum { |l| l[:qty] }`) died with a
`TypeError`, i.e. a 500 on malformed input. `[[]]` passed the same way.
Each scope now records, at definition time, how many Array-typed scopes sit on
its chain; the iterator descends that many levels less the one `Array.wrap`
already consumes, and yields anything deeper as-is. The attribute validators
then see a non-hash and fail it exactly as they do for any other unexpected
element type, so these requests get the 400 they always should have.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
ericproulx
force-pushed
the
fix/nested-array-validation-bypass
branch
from
July 29, 2026 20:53
0238fd0 to
b03b7a7
Compare
Danger ReportNo issues found. |
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.
Summary
AttributesIterator#do_eachrecursed into any element that was anArray. That recursion is genuinely required when the declaration nests array scopes —map_paramsadds one level of nesting perArray-typed scope on the chain, so the params for the inner scope really are an array of arrays — but nothing compared the incoming depth against the declared one.A request could therefore wrap its elements in extra arrays and have them silently unwrapped:
{"lines":[{"book_id":"x","qty":1}]}{"lines":[[{"book_id":"x","qty":1}]]}TypeErrorin the endpoint{"lines":[[[{"book_id":"x","qty":1}]]]}TypeError{"lines":[[]]}TypeErrorValidation passed and
params[:lines]/declaredhanded the endpoint[[{...}]], so any ordinary body assuming the declared shape died withTypeError: no implicit conversion of Symbol into Integer— a 500 on malformed input. The same held for an array scope declared inside a hash (requires :outer, type: Hash do requires :inner, type: Array do ... end end).Array[Integer]andtype: Hashscopes already rejected the equivalent shape; only the block form was affected.Approach
Each scope now records at definition time how many
Array-typed scopes sit on its chain (ParamsScope#array_depth). The iterator descends that many levels, less the oneArray.wrapalready consumes in#each, and yields anything deeper as-is. The attribute validators then see a non-hash and fail it exactly as they do for any other unexpected element type (hash_like?isrespond_to?(:key?), which anArrayis not), so these requests get the 400 they always should have — no new error path.Declared nesting is unaffected:
requires :a, type: Array do requires :b, type: Array do ... end endstill descends both levels and still reportsa[0][b][0][c] is missing.Backward compatibility
Requests that were previously accepted through the silent unwrap are now rejected with 400. In practice those requests could not be served — the endpoint received a shape it never declared — so this converts a 500 into the correct 4xx rather than removing working behaviour.
Not a regression; the behaviour is present in 3.3.4 and earlier.
Test plan
params_scope_spec.rbcovering the flat, hash-wrapped and declared-nested cases; verified they fail without thelib/change.🤖 Generated with Claude Code