fix: validate IP addresses from Cloudflare trace endpoint #12
No reviewers
Labels
No labels
bug
feature request
improvement
low-priority
priority
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
leon/New-CFUpdater!12
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "cloudy/New-CFUpdater:fix/validate-ip-address"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
This PR fixes issue #11 by adding validation for IP addresses returned from Cloudflare's
/cdn-cgi/traceendpoint.Problem
Cloudflare has a bug where they may return their own IPs from the trace endpoint instead of the client's actual IP. This causes the updater to set DNS records to Cloudflare's IPs, rendering the target unreachable.
Solution
Added comprehensive IP validation in
externalip/utils.go:Key Changes
validateIPv4()/validateIPv6(): Validate returned IP addressesisPublicIP(): Check if IP is public (not private/reserved)isCloudflareIPv4()/isCloudflareIPv6(): Check against Cloudflare's IP rangesTesting
Added comprehensive unit tests covering:
Fixes #11
@cloudy Nice work! But can you change
isCloudflareIPv4andisCloudflareIPv6to use a for-loop instead of writing all IPs in there manually? Would be nicer to maintain in the future. Please add a new function that is called only during startup, which fetches Cloudflares current IP lists (https://www.cloudflare.com/ips-v4 and https://www.cloudflare.com/ips-v6) and stores them in global string arrays, which the isCloudIPv4/6 can iterate over.@leon Got it! I'll refactor the code to:
https://www.cloudflare.com/ips-v4andhttps://www.cloudflare.com/ips-v6isCloudflareIPv4andisCloudflareIPv6to iterate over these arrays instead of hardcoding all IPsStarting work on this now.
✅ Done! Refactored the code:
New file:
cloudflare_ips.goFetchCloudflareIPRanges()- fetches IP lists at startupCloudflareIPRangesV4andCloudflareIPRangesV6slicesUpdated:
utils.goisCloudflareIPv4()andisCloudflareIPv6()now iterate over the fetched rangesUpdated:
main.goFetchCloudflareIPRanges()during startupUpdated:
utils_test.goTestMainto load IP ranges before running testsAll tests pass. The IP lists are now fetched dynamically from Cloudflare's official endpoints, making future maintenance much easier.