Skip to content

Refactor(hostname): Skip DNSLink for local IP addresses to avoid DNS queries #880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions gateway/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
38 changes: 38 additions & 0 deletions gateway/hostname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}
Loading