Repository navigation
fix(normalizer): validate numeric float64 inputs in typed read normalizer (#3762) - #3785
yunusarda-sudo wants to merge 6 commits into
Conversation
…izer (github#3762) The typed-read argument normalizer only checked string values for out-of-range and non-integer values, letting raw float64 numbers bypass validation and fail in json.Unmarshal with a raw Go struct decode error (leaking internal types). Validate both string and float64 number inputs uniformly against inSignedIntRange and integers, rejecting out-of-range numbers cleanly. Reported-by: Rafe Walker Signed-off-by: Yunus Ardaguney <yunusarda-sudo@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The numeric range check can accept an out-of-range page value after float64 rounding, requiring lossless token validation and a regression test for -2^63-1.
1 open issue
What changed in this PR
This PR updates the typed-read argument normalizer to validate raw numeric pagination inputs alongside numeric strings, addressing validation and decode-error handling reported in #3762.
Changes:
- Applies shared integer and range checks to string and numeric
page/perPageinputs. - Serializes validated values as integers while preserving zero-value handling.
- Adds regression cases for raw out-of-range numbers and fractional input.
| File | Description |
|---|---|
pkg/github/typed_search_outputs_test.go |
Adds numeric boundary and fractional-input rejection tests. |
pkg/github/typed_read_normalizers.go |
Unifies pagination validation and integer serialization. |
Review effort: Balanced (auto)
| if math.IsNaN(parsed) || math.IsInf(parsed, 0) || parsed != math.Trunc(parsed) || !inSignedIntRange(parsed, strconv.IntSize) { | ||
| return nil, fmt.Errorf("parameter %s is not a valid number", field) | ||
| } | ||
| if field == "perPage" { | ||
| if parsed, ok := number.(float64); ok && parsed == 0 { | ||
| delete(args, field) | ||
| continue | ||
| } | ||
| if parsed, ok := number.(int); ok && parsed == 0 { | ||
| delete(args, field) | ||
| continue | ||
| } | ||
| intVal := int(parsed) |
…c-pagination-bounds
…sion Decode with UseNumber and parse integer tokens exactly, so values such as -9223372036854775809 are rejected instead of rounding into range. Decimal and exponent forms are checked as exact rationals after bounding token length and exponent.
…c-pagination-bounds
SoulmatelynchVFX
left a comment
There was a problem hiding this comment.
The case string branch still goes through strconv.ParseFloat and parsed != math.Trunc(parsed) (line 82), and intVal = int(parsed) (line 85). That check catches a fraction, not a lost integer digit: binary64 cannot hold integers above 2^53, so {"perPage":"9007199254740993"} parses to 9007199254740992, passes both the trunc and the range check, and is serialized as 9007199254740992 without an error. The same value as a bare number token is kept exactly by exactIntegerToken, and your new TestNormalizeTypedReadArgumentsPreservesExactNumbers lists 9007199254740993 for perPage, so the two input forms now disagree.
I checked the rounding rather than the parser: float("9007199254740993") == 9007199254740992.0 and p == math.trunc(p) is true, float("9007199254740995") rounds up to 9007199254740996. Python's float() and Go's ParseFloat are both correctly rounded binary64, so the same token gives the same bits. I did not run go test (no Go toolchain here), so inSignedIntRange's boundary form and the actual perPage ceiling on the server side are unverified; I only confirmed that the 2^53 band passes the code path above without an error.
Not fixed by this PR and probably out of scope, but worth deciding: if the string branch is meant to keep tolerating "25.0", one option is to try ParseInt first and fall back to exactIntegerToken for the decimal/exponent forms, which would leave a bare float path only where it is deliberate.
AI-assisted review draft; the finding comes from reading the branch and IEEE-754 rounding, not from running the suite.

Resolves #3762
Summary of Changes
In
normalizeTypedReadArguments, pagination parameters (pageandperPage) were previously only range-checked when provided as JSON strings (if text, ok := number.(string)). When arguments were supplied as JSON numbers, they were unmarshaled intonumber.(float64)and bypassed the integer and range validation ininSignedIntRange.When an out-of-range integer (e.g.
9223372036854775808or numbers below-2^63) or a floating-point value was provided as a raw JSON number, it bypassed the normalizer and subsequently failed injson.Unmarshalwith a raw Go decode error (leaking internal type names such asgithub.ListCommitsInput.page of type int).Unified validation (
pkg/github/typed_read_normalizers.go):stringandfloat64input types.math.IsNaN,math.IsInf, non-integers (parsed != math.Trunc(parsed)), and out-of-range values (!inSignedIntRange(parsed, strconv.IntSize)) with a cleanparameter <field> is not a valid numbererror.Regression tests (
pkg/github/typed_search_outputs_test.go):1.5) inTestNormalizeTypedReadArgumentsRejectsOutOfRangeIntegers.Reported-by: Rafe Walker
Verification
go test -v -run TestNormalizeTypedReadArguments ./pkg/github(all passed).go test ./pkg/github(all package tests passed).