diff --git a/CHANGELOG.md b/CHANGELOG.md index 9700470d4..b313dd520 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ The following emojis are used to highlight certain changes: ### Fixed +- `gateway`: Skip DNSLink lookup for IP addresses to avoid unnecessary DNS queries [#880](https://github.com/ipfs/boxo/pull/880) - `bitswap/client`: Fix unintentional ignoring `DontHaveTimeoutConfig` [#872](https://github.com/ipfs/boxo/pull/872) ### Security diff --git a/gateway/hostname.go b/gateway/hostname.go index 29c0b7f81..4c078cec1 100644 --- a/gateway/hostname.go +++ b/gateway/hostname.go @@ -258,6 +258,11 @@ func isDomainNameAndNotPeerID(hostname string) bool { func hasDNSLinkRecord(ctx context.Context, backend IPFSBackend, host string) bool { dnslinkName := stripPort(host) + // Skip DNSLink lookup for IP addresses + if net.ParseIP(dnslinkName) != nil { + return false + } + if !isDomainNameAndNotPeerID(dnslinkName) { return false } diff --git a/gateway/hostname_test.go b/gateway/hostname_test.go index 1bfb07c42..b6ad366a9 100644 --- a/gateway/hostname_test.go +++ b/gateway/hostname_test.go @@ -369,3 +369,41 @@ func BenchmarkInlineDNSLink(b *testing.B) { _, _ = InlineDNSLink(testDNSLinkC) } } + +// Test function for hasDNSLinkRecord with local IP addresses +func TestHasDNSLinkRecordWithLocalIP(t *testing.T) { + t.Parallel() + + // Create test environment + backend, _ := newMockBackend(t, "fixtures.car") + // Add some DNSLink records to mock backend + testCID2, _ := cid.Decode("QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn") + backend.namesys["/ipns/example.com"] = newMockNamesysItem(path.FromCid(testCID2), 0) + + ctx := httptest.NewRequest(http.MethodGet, "http://example.com", nil).Context() + + // Test local IP addresses + localIPs := []string{ + "127.0.0.1", + "8.8.8.8", + "192.168.100.22:8080", + "::1", + "[::1]:8080", + "0:0:0:0:0:0:0:1", + "fe80::a89c:baff:fece:8c94", + } + + for _, ip := range localIPs { + t.Run(ip, func(t *testing.T) { + // For local IP addresses, hasDNSLinkRecord should always return false + result := hasDNSLinkRecord(ctx, backend, ip) + require.False(t, result, "Local IP %s should not attempt DNSLink lookup", ip) + }) + } + + // Test valid domain name + t.Run("example.com", func(t *testing.T) { + result := hasDNSLinkRecord(ctx, backend, "example.com") + require.True(t, result, "example.com should have a valid DNSLink record") + }) +}