Skip to content

Commit 07734ab

Browse files
fern4lvarezclaude
andcommitted
Keep out-of-range and overflowing coordinates out of client.geo
Related to https://app.basecamp.com/2914079/buckets/21350690/card_tables/cards/10115825721 Float("1e999") is Infinity, which JSON can't generate, so a bad CF-IPLatitude or CF-IPLongitude header broke the request's log line. Log a location only when both coordinates are in range. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 629ac67 commit 07734ab

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎saas/lib/fizzy/saas/client_geo_logging.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def client_geo_location
3232
latitude = Float(cloudflare_header("CF-IPLatitude"), exception: false)
3333
longitude = Float(cloudflare_header("CF-IPLongitude"), exception: false)
3434

35-
if latitude && longitude
35+
# The range check also keeps out Infinity ("1e999"), which JSON can't generate.
36+
if latitude&.between?(-90, 90) && longitude&.between?(-180, 180)
3637
{ lat: latitude, lon: longitude }
3738
end
3839
end

‎saas/test/integration/client_geo_logging_test.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ class Fizzy::Saas::ClientGeoLoggingTest < ActionDispatch::IntegrationTest
2525
assert_nil logged_client_geo(headers: CLOUDFLARE_HEADERS.merge("CF-IPLatitude" => "nowhere"))["location"]
2626
end
2727

28+
test "leaves out a location that overflows or is out of range, and still logs the request" do
29+
assert_nil logged_client_geo(headers: CLOUDFLARE_HEADERS.merge("CF-IPLatitude" => "1e999"))["location"]
30+
assert_nil logged_client_geo(headers: CLOUDFLARE_HEADERS.merge("CF-IPLongitude" => "-1e999"))["location"]
31+
assert_nil logged_client_geo(headers: CLOUDFLARE_HEADERS.merge("CF-IPLatitude" => "91"))["location"]
32+
assert_nil logged_client_geo(headers: CLOUDFLARE_HEADERS.merge("CF-IPLongitude" => "180.5"))["location"]
33+
end
34+
2835
test "logs no geo without Cloudflare's headers" do
2936
assert_nil logged_client_geo(headers: {})
3037
end

0 commit comments

Comments
 (0)