Skip to content

Commit

Permalink
httpnet: add allowlist option
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan committed Jan 15, 2025
1 parent f7ba5ec commit 6d063e0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
33 changes: 31 additions & 2 deletions bitswap/network/httpnet/httpnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var (
// Option allows to configure the Network.
type Option func(net *httpnet)

// WithUserAgents sets the user agent when making requests.
// WithUserAgent sets the user agent when making requests.
func WithUserAgent(agent string) Option {
return func(net *httpnet) {
net.userAgent = agent
Expand Down Expand Up @@ -92,6 +92,17 @@ func WithInsecureSkipVerify(b bool) Option {
}
}

// WithAllowlist sets the hostnames that we are allowed to connect to via
// HTTP.
func WithAllowlist(hosts []string) Option {
return func(net *httpnet) {
net.allowlist = make(map[string]struct{})
for _, h := range hosts {
net.allowlist[h] = struct{}{}
}

Check warning on line 102 in bitswap/network/httpnet/httpnet.go

View check run for this annotation

Codecov / codecov/patch

bitswap/network/httpnet/httpnet.go#L97-L102

Added lines #L97 - L102 were not covered by tests
}
}

type httpnet struct {
// NOTE: Stats must be at the top of the heap allocation to ensure 64bit
// alignment.
Expand All @@ -117,6 +128,7 @@ type httpnet struct {
maxIdleConns int
supportsHave bool
insecureSkipVerify bool
allowlist map[string]struct{}
}

// New returns a BitSwapNetwork supported by underlying IPFS host.
Expand Down Expand Up @@ -284,9 +296,26 @@ func (ht *httpnet) Connect(ctx context.Context, p peer.AddrInfo) error {
if len(htaddrs.Addrs) == 0 {
return ErrNoHTTPAddresses
}

Check warning on line 298 in bitswap/network/httpnet/httpnet.go

View check run for this annotation

Codecov / codecov/patch

bitswap/network/httpnet/httpnet.go#L294-L298

Added lines #L294 - L298 were not covered by tests
ht.host.Peerstore().AddAddrs(p.ID, htaddrs.Addrs, peerstore.PermanentAddrTTL)

urls := network.ExtractURLsFromPeer(htaddrs)
if len(ht.allowlist) > 0 {
var filteredURLs []*url.URL
for _, u := range urls {
host, _, err := net.SplitHostPort(u.Host)
if err != nil {
return err
}
if _, ok := ht.allowlist[host]; !ok {
filteredURLs = append(filteredURLs, u)
}

Check warning on line 310 in bitswap/network/httpnet/httpnet.go

View check run for this annotation

Codecov / codecov/patch

bitswap/network/httpnet/httpnet.go#L300-L310

Added lines #L300 - L310 were not covered by tests
}
urls = filteredURLs

Check warning on line 312 in bitswap/network/httpnet/httpnet.go

View check run for this annotation

Codecov / codecov/patch

bitswap/network/httpnet/httpnet.go#L312

Added line #L312 was not covered by tests
}
// if filteredURLs == 0 nothing will happen below and we will return
// an error.

ht.host.Peerstore().AddAddrs(p.ID, htaddrs.Addrs, peerstore.PermanentAddrTTL)

rand.Shuffle(len(urls), func(i, j int) {
urls[i], urls[j] = urls[j], urls[i]
})

Check warning on line 321 in bitswap/network/httpnet/httpnet.go

View check run for this annotation

Codecov / codecov/patch

bitswap/network/httpnet/httpnet.go#L317-L321

Added lines #L317 - L321 were not covered by tests
Expand Down
3 changes: 1 addition & 2 deletions bitswap/network/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ func (rt *router) Connect(ctx context.Context, p peer.AddrInfo) error {
htaddrs, _ := SplitHTTPAddrs(p)
if len(htaddrs.Addrs) > 0 {
return rt.HTTP.Connect(ctx, p)
} else {
return rt.Bitswap.Connect(ctx, p)
}
return rt.Bitswap.Connect(ctx, p)

Check warning on line 74 in bitswap/network/router.go

View check run for this annotation

Codecov / codecov/patch

bitswap/network/router.go#L69-L74

Added lines #L69 - L74 were not covered by tests
}

func (rt *router) DisconnectFrom(ctx context.Context, p peer.ID) error {
Expand Down

0 comments on commit 6d063e0

Please sign in to comment.