Trust the bot's own LNURL and LNbits hosts in netguard
On nodes where the bot's public hostname resolves to loopback (hosts file, split DNS), the public address check refused every payment to a lightning address on the bot's own domain. The LNURL host and the LNbits host from the config are now exempt from the check; literal internal addresses stay refused. Co-Authored-By: Claude Mythos 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qy4RRoDyYEWTZWESFHp7dp (cherry picked from commit ba6c6a36293adf000c495cc81fdd18ecc96a69f6) Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
committed by
Michilis
parent
8d557f3043
commit
064f36889a
@@ -52,8 +52,41 @@ var dialer = &net.Dialer{
|
||||
},
|
||||
}
|
||||
|
||||
// DialContext dials like net.Dialer, but only connects to public addresses.
|
||||
// trusted holds hostnames that may resolve to a non-public address: the
|
||||
// bot's own LNURL and LNbits hosts often point at loopback through the hosts
|
||||
// file or split DNS. Filled once at startup by Trust.
|
||||
var trusted = map[string]bool{}
|
||||
|
||||
// Trust exempts hosts from the public address check.
|
||||
func Trust(hosts ...string) {
|
||||
for _, host := range hosts {
|
||||
if host != "" {
|
||||
trusted[normalizeHost(host)] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeHost(host string) string {
|
||||
return strings.TrimSuffix(strings.ToLower(host), ".")
|
||||
}
|
||||
|
||||
// isTrusted reports whether addr ("host:port") names a trusted host.
|
||||
func isTrusted(addr string) bool {
|
||||
host, _, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
host = addr
|
||||
}
|
||||
return trusted[normalizeHost(host)]
|
||||
}
|
||||
|
||||
var plainDialer = &net.Dialer{Timeout: 10 * time.Second}
|
||||
|
||||
// DialContext dials like net.Dialer, but only connects to public addresses,
|
||||
// unless addr names a trusted host.
|
||||
func DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
if isTrusted(addr) {
|
||||
return plainDialer.DialContext(ctx, network, addr)
|
||||
}
|
||||
return dialer.DialContext(ctx, network, addr)
|
||||
}
|
||||
|
||||
@@ -61,11 +94,14 @@ func DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
// 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 {
|
||||
if isTrusted(addr) {
|
||||
return nil
|
||||
}
|
||||
host, _, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
host = addr
|
||||
}
|
||||
host = strings.TrimSuffix(strings.ToLower(host), ".")
|
||||
host = normalizeHost(host)
|
||||
if host == "localhost" || strings.HasSuffix(host, ".localhost") {
|
||||
return fmt.Errorf("connection to %s refused", host)
|
||||
}
|
||||
|
||||
@@ -80,3 +80,26 @@ func TestCheckHost(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrustedHostBypassesCheck(t *testing.T) {
|
||||
defer func() { trusted = map[string]bool{} }()
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer l.Close()
|
||||
_, port, _ := net.SplitHostPort(l.Addr().String())
|
||||
Trust("LocalHost.")
|
||||
conn, err := DialContext(context.Background(), "tcp", "localhost:"+port)
|
||||
if err != nil {
|
||||
t.Fatalf("dial to trusted localhost refused: %v", err)
|
||||
}
|
||||
conn.Close()
|
||||
if err := CheckHost("localhost:" + port); err != nil {
|
||||
t.Errorf("CheckHost refused trusted host: %v", err)
|
||||
}
|
||||
if conn, err := DialContext(context.Background(), "tcp", l.Addr().String()); err == nil {
|
||||
conn.Close()
|
||||
t.Error("literal loopback address was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,17 @@ func directTransport() *http.Transport {
|
||||
return t
|
||||
}
|
||||
|
||||
// The bot's own hosts may resolve to loopback (hosts file, split DNS). Paying
|
||||
// a lightning address on them must keep working.
|
||||
func init() {
|
||||
if u := internal.Configuration.Bot.LNURLHostUrl; u != nil {
|
||||
netguard.Trust(u.Hostname())
|
||||
}
|
||||
if u, err := url.Parse(internal.Configuration.Lnbits.Url); err == nil {
|
||||
netguard.Trust(u.Hostname())
|
||||
}
|
||||
}
|
||||
|
||||
func GetClient(clientType ClientType) (*http.Client, error) {
|
||||
client := http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
|
||||
Reference in New Issue
Block a user