Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ByteBaker
Copy link

Fixes #4389

Summary

This PR fixes a bug where Dex fails to initialize Kubernetes storage when the API server uses an IPv4 address. The issue occurs in Go 1.25.2+ and 1.24.8+ due to stricter RFC 3986 enforcement introduced as part of CVE-2025-47912.

Problem

The inClusterConfig() function unconditionally wrapped all IP addresses in square brackets when constructing the Kubernetes API server URL:

host = "[" + host + "]"
cluster.Server = "https://" + host + ":" + port

This violated RFC 3986, which specifies that only IPv6 addresses should be enclosed in brackets. While older Go versions accepted this, newer versions (1.25.2+, 1.24.8+) now correctly reject it:

parse "https://[172.20.0.1]:443/version": invalid IPv6 host

Solution

Use net.ParseIP() to detect the address type and only wrap IPv6 addresses:

if parsedIP := net.ParseIP(host); parsedIP != nil && parsedIP.To4() == nil {
    host = "[" + host + "]"
}

Results:

  • IPv4: https://172.20.0.1:443 (unwrapped, RFC compliant)
  • IPv6: https://[2001:db8::1]:443 (wrapped, RFC compliant)

Changes

  • ✅ Fixed bracket wrapping logic in storage/kubernetes/client.go
  • ✅ Added comprehensive test coverage for IPv4, IPv6, and edge cases
  • ✅ All existing tests pass

Testing

Added TestInClusterConfigIPv4IPv6 covering:

  • IPv4 addresses (not wrapped)
  • IPv6 addresses (wrapped)
  • IPv6 loopback (wrapped)
  • IPv4 loopback (not wrapped)
  • IPv4-mapped IPv6 addresses (not wrapped, treated as IPv4)
  • Error cases

Backward Compatibility

  • ✅ IPv6 clusters: No change (still wrapped)
  • ✅ IPv4 clusters: Now works with Go 1.25.2+/1.24.8+
  • ✅ Older Go versions: Still works correctly

Related

  • Original commit that introduced unconditional wrapping: 3a3a2bc (2020)
  • CVE-2025-47912: Go URL parsing security fix
  • RFC 3986 Section 3.2.2: Host component specification

Fixes a bug where IPv4 addresses were incorrectly wrapped in square
brackets when constructing the Kubernetes API server URL in
inClusterConfig(). This causes URL parsing failures in Go 1.25.2+
due to stricter RFC 3986 enforcement introduced in CVE-2025-47912.

The previous implementation (added in commit 3a3a2bc) unconditionally
wrapped all IP addresses in brackets under the assumption that "IPv4
also works with square brackets". However, RFC 3986 specifies that only
IPv6 addresses should be enclosed in brackets, and recent Go versions
now enforce this requirement.

Changes:
- Use net.ParseIP() to detect IP address type
- Only wrap IPv6 addresses (when To4() returns nil) in brackets
- Leave IPv4 addresses unwrapped for RFC 3986 compliance
- Add comprehensive test coverage for IPv4, IPv6, and edge cases

Error before fix:
  parse "https://[172.20.0.1]:443/version": invalid IPv6 host

After fix:
  IPv4: https://172.20.0.1:443 (unwrapped)
  IPv6: https://[2001:db8::1]:443 (wrapped)

Signed-off-by: ByteBaker <[email protected]>
@ByteBaker ByteBaker force-pushed the fix/ipv4-url-parsing branch from 3d1cf8d to 3327d1a Compare October 22, 2025 07:44
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.

Kubernetes storage fails with IPv4 addresses in Go 1.25.2+

1 participant