Fix three silent corruptions in the absence path - #39
Open
VSN2015 wants to merge 1 commit into
Open
Conversation
A second-pass audit of the absence/default/unknown-key path, in the places the open PRs don't reach. All three are the same shape: the contract accepted something it documents as impossible. A mutable `default:` was shared by every request. Field declarations are frozen data, but the value an author wrote was not, and HashWithIndifferentAccess hands a non-frozen Array — and any String — to the result by reference. So `array :tags, default: []` gave every request the same Array, and one request appending to `permitted_params[:tags]` corrupted the default for the life of the process. Authored `default:`/`example:` values are now deep-copied and frozen at class load; the copy is the point, so an object the host app passed in is never frozen behind its back. `normalize:` could manufacture an empty value that walked past `required`. "" is documented as absent, but normalization ran after absence had been decided, so `normalize: :squish` rejected "" as missing and accepted " " as "". It now runs first, as its own stage, which keeps exactly one reading of absence and still calls a host's proc once per value. A `default:` is also stored in the form it was validated in. `unknown: :error` rejected ordinary form submissions. Only the router's controller/action/format were exempt, but Rails also merges authenticity_token, `_method`, utf8 and commit into a form POST, so the strictest setting failed on four of the framework's own keys. Those are now exempt from the check at the top level only — still flagged inside a root: or a nested hash, still flagged by a standalone Contract, and still passed through by monitor mode, whose promise is an untouched params hash. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This was referenced Sep 12, 2026
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.
A second-pass audit of the absence/default/unknown-key path — the places the 21 open PRs don't reach. All three findings are the same shape: the contract accepted something it documents as impossible. Each was reproduced before it was fixed, and each spec is red on
master.1. A mutable
default:was shared by every requestField declarations are frozen data, but the value an author wrote was not — and
HashWithIndifferentAccesshands a non-frozen Array (and any String) to the result by reference.array :tags, default: []is the common spelling, andModel.new(tags: …)assigns that same object, sorecord.tags << xwas enough to trigger it. The corruption outlived the request and lasted for the life of the process.Authored
default:/example:values are now deep-copied and frozen at class load. The copy is the point: the object the host app passed in is never frozen behind its back, in case it is still using it. A String default is copied again on delivery, so every value in the result stays the app's own to mutate.2.
normalize:could manufacture an empty value that walked pastrequired""is documented as absent, so a required field must violate — but normalization ran after absence had already been decided:An empty string went into the column — the silent corruption strict coercion exists to refuse, delivered by the gem's own preset.
normalize:is now its own stage, running before the absence rule, so a value that normalizes to empty takes thenullable:/default:/missingbranch like any other empty value. There is still exactly one reading of absence, and anormalize:Proc is still called exactly once per value (spec'd, by counting). Relatedly, adefault:is now stored in the form it was validated in:default: " free "withnormalize: :squishwas checked as"free"and handed to requests as" free ".3.
unknown: :errorrejected ordinary form submissionsOnly the router's
controller/action/formatwere exempt from the top-level check, but Rails also mergesauthenticity_token,_method,utf8andcommitinto a form POST:The strictest setting was unusable outside a JSON API, and it failed on four of the framework's own keys rather than on anything the client got wrong.
Those four are now exempt from the check, at the top level only. Deliberately unchanged: a form key smuggled inside a
root:or a nested hash is stillunknown; a standalonePermittable::Contractstill exempts nothing (it has neither a router nor a form); and monitor mode still passes them through in its raw hash, where behaving exactly like the pre-contract app is the whole promise and a legacy action may read_methoditself. The two lists answer different questions, soFORM_KEYSsits besideROUTING_KEYSrather than joining it.Verification
264 examples, 0 failures; rubocop clean; coverage 97.09%.masterfor the right reasons.benchmark/overhead.rbunchanged at 1.64x faster thanparams.permit— normalization moved, it did not get duplicated.default:, nonormalize:, and nounknown: :errorare unaffected.Also found, not in this PR
Five more findings came out of the same audit and are reproduced but deliberately left out to keep this reviewable:
sensitive:silently fails to redact non-String values (a proc filter can only mutate Strings — stack behind #33); exported OpenAPI documents omit theparametersentry for the path templates they emit, so member-route documents fail 3.1 validation (behind #29);ERROR_SCHEMAhas drifted from themessage:key the server actually sends and omits thedepthcode; aPATCH|PUTroute is documented for one verb; and impossible declarations still pass class load (length: 0on a required field, reversed ranges likein: 65..18, and a block array'sdefault:, which skips element validation entirely). Two further members of this same family also remain: adefault:is delivered uncast (default: "42"on an:integeryields the String"42") and untransformed where a sent value is transformed.🤖 Generated with Claude Code