Refuse non-public addresses in the LNURL HTTP client
The clients from network.GetClient fetch URLs that come from users and from remote LNURL servers. Without a proxy they now only connect to public addresses. The check runs at connect time on the resolved address, so it also covers redirects and hostnames that resolve to an internal address. With a SOCKS proxy, literal internal addresses and localhost names are refused. (cherry picked from commit c3bcae870ec6018e8809d96f86b0d091ab124a5a) Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
committed by
Michilis
parent
34f2e562e2
commit
5ff6e841e1
@@ -0,0 +1,61 @@
|
||||
// Package netguard keeps outgoing connections for user-supplied URLs away from
|
||||
// the bot's own host and network.
|
||||
package netguard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 100.64.0.0/10 is carrier-grade NAT space, also used by overlay networks.
|
||||
var cgnat = netip.MustParsePrefix("100.64.0.0/10")
|
||||
|
||||
// IsPublic reports whether ip is an address on the public internet.
|
||||
func IsPublic(ip netip.Addr) bool {
|
||||
ip = ip.Unmap()
|
||||
return ip.IsGlobalUnicast() && !ip.IsPrivate() && !cgnat.Contains(ip)
|
||||
}
|
||||
|
||||
// The check runs at connect time on the resolved address, so it also covers
|
||||
// redirects and hostnames that resolve to an internal address.
|
||||
var dialer = &net.Dialer{
|
||||
Timeout: 10 * time.Second,
|
||||
Control: func(_, address string, _ syscall.RawConn) error {
|
||||
ap, err := netip.ParseAddrPort(address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !IsPublic(ap.Addr()) {
|
||||
return fmt.Errorf("connection to non-public address %s refused", ap.Addr())
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// DialContext dials like net.Dialer, but only connects to public addresses.
|
||||
func DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return dialer.DialContext(ctx, network, addr)
|
||||
}
|
||||
|
||||
// CheckHost rejects addr ("host:port") when the host is a literal non-public
|
||||
// IP or a localhost name. It does not resolve other hostnames, so use it only
|
||||
// where the connection is made by a proxy and DialContext cannot be used.
|
||||
func CheckHost(addr string) error {
|
||||
host, _, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
host = addr
|
||||
}
|
||||
host = strings.TrimSuffix(strings.ToLower(host), ".")
|
||||
if host == "localhost" || strings.HasSuffix(host, ".localhost") {
|
||||
return fmt.Errorf("connection to %s refused", host)
|
||||
}
|
||||
if ip, err := netip.ParseAddr(host); err == nil && !IsPublic(ip) {
|
||||
return fmt.Errorf("connection to non-public address %s refused", ip)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package netguard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/netip"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsPublic(t *testing.T) {
|
||||
for addr, want := range map[string]bool{
|
||||
"1.1.1.1": true,
|
||||
"2606:4700:4700::1111": true,
|
||||
"127.0.0.1": false,
|
||||
"::1": false,
|
||||
"0.0.0.0": false,
|
||||
"10.1.2.3": false,
|
||||
"172.16.0.1": false,
|
||||
"192.168.1.1": false,
|
||||
"169.254.169.254": false,
|
||||
"100.64.0.1": false,
|
||||
"fe80::1": false,
|
||||
"fd00::1": false,
|
||||
"::ffff:127.0.0.1": false,
|
||||
"::ffff:192.168.1.1": false,
|
||||
"224.0.0.1": false,
|
||||
"255.255.255.255": false,
|
||||
} {
|
||||
if got := IsPublic(netip.MustParseAddr(addr)); got != want {
|
||||
t.Errorf("IsPublic(%s) = %v, want %v", addr, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDialContextRefusesLoopback(t *testing.T) {
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer l.Close()
|
||||
conn, err := DialContext(context.Background(), "tcp", l.Addr().String())
|
||||
if err == nil {
|
||||
conn.Close()
|
||||
t.Fatal("dial to loopback succeeded")
|
||||
}
|
||||
// a hostname that resolves to loopback is refused as well
|
||||
_, port, _ := net.SplitHostPort(l.Addr().String())
|
||||
if conn, err := DialContext(context.Background(), "tcp", "localhost:"+port); err == nil {
|
||||
conn.Close()
|
||||
t.Fatal("dial to localhost succeeded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckHost(t *testing.T) {
|
||||
for addr, ok := range map[string]bool{
|
||||
"example.com:443": true,
|
||||
"abc.onion:80": true,
|
||||
"1.1.1.1:443": true,
|
||||
"localhost:6060": false,
|
||||
"LOCALHOST.:6060": false,
|
||||
"foo.localhost:80": false,
|
||||
"127.0.0.1:6060": false,
|
||||
"[::1]:6060": false,
|
||||
"192.168.0.10:5000": false,
|
||||
"0.0.0.0:80": false,
|
||||
} {
|
||||
if err := CheckHost(addr); (err == nil) != ok {
|
||||
t.Errorf("CheckHost(%s) = %v, want ok=%v", addr, err, ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/LightningTipBot/LightningTipBot/internal"
|
||||
"github.com/LightningTipBot/LightningTipBot/internal/netguard"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
@@ -38,9 +39,19 @@ func GetClientForScheme(url *url.URL) (*http.Client, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// directTransport is used when no proxy is configured. The URLs fetched with
|
||||
// these clients come from users and remote servers, so it only connects to
|
||||
// public addresses.
|
||||
var directTransport = func() *http.Transport {
|
||||
t := http.DefaultTransport.(*http.Transport).Clone()
|
||||
t.DialContext = netguard.DialContext
|
||||
return t
|
||||
}()
|
||||
|
||||
func GetClient(clientType ClientType) (*http.Client, error) {
|
||||
client := http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
Timeout: 10 * time.Second,
|
||||
Transport: directTransport,
|
||||
}
|
||||
var cfg *internal.SocksConfiguration
|
||||
switch clientType {
|
||||
@@ -74,6 +85,11 @@ func GetClient(clientType ClientType) (*http.Client, error) {
|
||||
return &client, nil
|
||||
}
|
||||
specialTransport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
// The proxy resolves hostnames, so only literal addresses and
|
||||
// localhost names can be refused here.
|
||||
if err := netguard.CheckHost(addr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return d.Dial(network, addr)
|
||||
}
|
||||
client.Transport = specialTransport
|
||||
|
||||
Reference in New Issue
Block a user