Reject underscore separators and whitespace in numeric coercion - #82
Merged
Merged
Conversation
Kernel#Integer/Float and BigDecimal() all treat underscore digit separators and surrounding whitespace as harmless formatting for a Ruby source literal — not how a client spells a number in a request body. Coercion.cast(:integer, "1_8") returned 18, and Coercion.cast(:integer, " 99 ") returned 99, silently accepting input that isn't a canonical numeric string, the same class of leniency the module already deliberately hardens against for NaN/Infinity and bare-significand "0e10" forms. :integer/:float/:decimal String casts now validate against a strict canonical numeric format before delegating to Integer()/Float()/ BigDecimal(), rejecting anything with underscores or surrounding whitespace while still accepting plain integers, decimals, negative numbers, and valid exponent notation. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.
The bug
Numeric scalar casts (
:integer/:float/:decimal) delegate straight toKernel#Integer/Float/BigDecimal, which silently accept underscore digit separators and surrounding whitespace — a convenience meant for a number literal in Ruby source code, not for a value arriving in a request body. This contradicts the module's documented "deliberately strict" coercion philosophy, and is the same class of leniencyCoercionalready deliberately hardens against for NaN/Infinity and bare-significand0e10forms — just slipping through a different door:"1_8"is not how a client spells eighteen, and" 99 "is not how one spells ninety-nine; a client sending either is more likely to have a formatting bug upstream than to mean the number that results.The fix
Before delegating to
Integer()/Float()/BigDecimal(), the String branch of each numeric cast (cast_integer,cast_float,cast_decimal) is now checked against a strict canonical numeric format — an optional sign, digits, an optional.digits, and an optional exponent, with no underscores and no surrounding whitespace:A String that doesn't match reports the same
"invalid_type"violation the module's other strictness checks already use, rejected before it ever reachesInteger/Float/BigDecimal. Legitimately valid numeric strings keep working exactly as before — plain integers, leading-zero integers ("01"), decimals, negative numbers, and valid exponent notation like"1.5e10"or the large exponents:decimalgenuinely represents ("1e400") — since none of those forms use underscores or whitespace.:decimal's existing NaN/Infinity string rejection is unaffected: those literals don't match the numeric format either, so they're still rejected with"invalid_type", just at the format-check gate instead of viaBigDecimal's own NaN/Infinity parsing.Verification
spec/permittable_spec.rb), confirmed to fail before the implementation change and pass afterbundle exec rspec— 853 examples, 0 failuresbundle exec rubocop lib/permittable.rb spec/permittable_spec.rb— no offenses🤖 Generated with Claude Code