Skip to content

Reject NaN and Inf in float query parameters - #1490

Merged
aaronbrethorst merged 3 commits into
OneBusAway:mainfrom
SajalDevX:fix/reject-non-finite-float-params
Sep 27, 2026
Merged

aaronbrethorst merged 3 commits into
OneBusAway:mainfrom
SajalDevX:fix/reject-non-finite-float-params

Conversation

@SajalDevX

@SajalDevX SajalDevX commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

strconv.ParseFloat accepts "NaN" and "Inf", so ParseFloatParam treated them as valid numbers. NaN then passes the range checks in ValidateLocationParams, since every comparison with NaN is false:

GET /api/where/stops-for-location.json?key=TEST&lat=NaN&lon=-122.426966
-> 200, empty list        (lat=91 gets a 400)

The same happens for radius, latSpan and lonSpan set to NaN, and in routes-for-location and trips-for-location, which share parseLocationParams.

Change

Both ParseFloatParam and ParseRequiredFloatParam now treat a non-finite result like any other unparseable value: they add the usual Invalid field value for field "x". error and return 0. That also catches Inf and values that overflow, like 1e400.

Before, radius=Inf got through and came back as a 200 with limitExceeded: true and no stops. Now it's a 400.

Testing

  • New cases for NaN, Inf and 1e400 in TestParseFloatParam and TestParseRequiredFloatParam, plus a parseLocationParams case with every parameter set to NaN. All of them fail on main.
  • go vet (both tag sets), go fmt and make test pass. The only exception is TestBuildApplicationWithConfigFile, which downloads a live GTFS feed my sandbox can't reach; it fails on main too.

I used Claude to help track this down and write the tests; I reviewed the change and ran everything locally.

Summary by CodeRabbit

  • Bug Fixes
    • Location inputs now reject NaN and infinite values, returning validation errors instead of producing invalid location results.

strconv.ParseFloat accepts "NaN" and "Inf", so ParseFloatParam and
ParseRequiredFloatParam passed them through as valid numbers. NaN then
slips past the range checks in ValidateLocationParams, because every
comparison with NaN is false, and stops-for-location,
routes-for-location and trips-for-location answered lat=NaN (or
radius/latSpan/lonSpan=NaN) with a 200 and an empty list instead of a
400.

Treat non-finite values like any other unparseable value: add the
"Invalid field value" error and return 0. This also covers "Inf" and
values that overflow, such as "1e400".
Cover NaN, Inf and an overflowing value (1e400) in ParseFloatParam,
and NaN/Inf/-Inf in ParseRequiredFloatParam. All of them used to parse
without an error.
With every location parameter set to NaN, parseLocationParams used to
return a location and no errors, which is how the location endpoints
ended up answering 200.
@CLAassistant

CLAassistant commented Sep 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 55a79e25-47f4-4474-bca5-98fa8fe3b5de

📥 Commits

Reviewing files that changed from the base of the PR and between 91d3439 and 2e911bb.

📒 Files selected for processing (3)
  • internal/restapi/location_params_test.go
  • internal/utils/api.go
  • internal/utils/api_test.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

Float parameter parsing now rejects NaN and infinite values, records invalid-value errors, and returns zero. Tests cover optional and required float parsing, including location parameters.

Changes

Float parameter validation

Layer / File(s) Summary
Reject non-finite float values
internal/utils/api.go, internal/utils/api_test.go, internal/restapi/location_params_test.go
Both parsing functions reject NaN and infinities, record invalid-value errors, and return zero. Tests cover these values and location parsing.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: arcoder181105

Merge Risk: ⚪ Minimal · up to 2e911

Non-finite float query values are rejected, and invalid location requests receive validation errors before route lookup. No actionable merge risk is evident in the supplied changes.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 2e911

Non-finite location values now receive the existing validation response instead of reaching location processing. No new security exposure was identified, but broader security coverage and client compatibility expectations remain unverified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The affected attacker-controlled input is the location query string. For the examined route path, rejection occurs before the downstream location lookup; no expanded privilege or cross-service reachability was identified.

Trust Boundaries and Controls

  • observed — Validation errors produced by the float helper are propagated through location parsing to the handler’s existing early-return control.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: rejecting NaN and Inf in float query parameters.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sonarqubecloud

Copy link
Copy Markdown

@aaronbrethorst aaronbrethorst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks good. Rejecting NaN, ±Inf and overflow values in ParseFloatParam/ParseRequiredFloatParam is consistent with how we already return 400 for out-of-range coordinates, and returning 0 alongside the error also fixes the old behavior of handing back +Inf on overflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants