fix(node): bound what a node's answers can cost the master

Node responses are untrusted input on the master, and a few shapes still
cost it without bound under the 64 MiB body cap:
- a success=false message became an error (logged, stored as lastError)
  in full; it is now cut to the 8 KiB the non-200 diagnostic already uses;
- each "{}" of a reported list decodes into a whole struct, so lists are
  now decoded entry by entry with per-endpoint caps (1024 descendants,
  65536 inbounds and host groups, 1M client-IP rows), and the GUID-keyed
  trees and maps are capped at 1M values;
- a TLS handshake the node never answers kept a socket and goroutines
  alive forever, since the transport keeps dialing for its pool after the
  request gives up; node transports now give up after 10 s.
This commit is contained in:
MHSanaei
2026-10-31 09:19:51 +00:00
parent 08482154c2
commit 5a1ea42785
5 changed files with 172 additions and 26 deletions
+37
View File
@@ -16,6 +16,7 @@ import (
"sync"
"sync/atomic"
"testing"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/util/crypto"
@@ -693,3 +694,39 @@ func TestNodeClientsDoNotFollowARedirectToAnotherHost(t *testing.T) {
t.Fatalf("a node's redirect reached another host %d time(s)", otherHit.Load())
}
}
// The transport keeps dialing for its pool after a request gives up, so a node that
// never answers the TLS handshake would hold a socket and two goroutines forever.
func TestNodeTransportGivesUpOnAStalledTLSHandshake(t *testing.T) {
previous := nodeTLSHandshakeTimeout
nodeTLSHandshakeTimeout = 200 * time.Millisecond
t.Cleanup(func() { nodeTLSHandshakeTimeout = previous })
lis, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer lis.Close()
closed := make(chan struct{})
go func() {
conn, err := lis.Accept()
if err != nil {
return
}
defer conn.Close()
_, _ = io.Copy(io.Discard, conn) // returns once the master hangs up
close(closed)
}()
client := &http.Client{Transport: newNodeTransport(&tls.Config{InsecureSkipVerify: true})}
ctx, cancel := context.WithTimeout(netsafe.ContextWithAllowPrivate(context.Background(), true), 50*time.Millisecond)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "https://"+lis.Addr().String()+"/", nil)
if resp, err := client.Do(req); err == nil {
_ = resp.Body.Close()
}
select {
case <-closed:
case <-time.After(3 * time.Second):
t.Fatal("the stalled handshake's connection is still open")
}
}