From 94a89fb70fa596c91a4572d8b498207f3398e178 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Tue, 28 Jul 2026 22:53:06 +0800 Subject: [PATCH] chore: reject oversized sniff reads before filling the buffer --- component/sniffer/dispatcher.go | 13 ++++++------- component/sniffer/dispatcher_test.go | 21 +++++++++++++++++++-- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/component/sniffer/dispatcher.go b/component/sniffer/dispatcher.go index 4f1aedda..3f5180ea 100644 --- a/component/sniffer/dispatcher.go +++ b/component/sniffer/dispatcher.go @@ -1,6 +1,7 @@ package sniffer import ( + "bufio" "errors" "net/netip" "time" @@ -237,14 +238,12 @@ func (sd *Dispatcher) sniffDomain(conn *N.BufferedConn, metadata *C.Metadata) (s deadline := time.Now().Add(1 * time.Second) want := conn.Buffered() for len(candidates) > 0 { - // Preserve want even when it exceeds the allocation budget. Growing only - // to the limit makes Peek return ErrBufferFull instead of trusting an - // unbounded length advertised by the input protocol. - growTo := want - if growTo > maxSniffBufferSize { - growTo = maxSniffBufferSize + // Reject an unmet oversized request before Grow or Peek. Otherwise Peek + // fills the bounded buffer before reporting that the request cannot fit. + if want > maxSniffBufferSize && want > conn.Buffered() { + return "", SnifferConfig{}, bufio.ErrBufferFull } - conn.Grow(growTo) + conn.Grow(want) _ = conn.SetReadDeadline(deadline) _, err = conn.Peek(want) diff --git a/component/sniffer/dispatcher_test.go b/component/sniffer/dispatcher_test.go index 87b9b98b..04e5f2cb 100644 --- a/component/sniffer/dispatcher_test.go +++ b/component/sniffer/dispatcher_test.go @@ -5,6 +5,7 @@ package sniffer // parsing itself (a complete buffer in, a domain out) belongs in sniff_test.go. import ( + "bufio" "bytes" "io" "net" @@ -124,6 +125,9 @@ func TestDispatcherFeedLoop(t *testing.T) { // rounds and how much they grew by are pinned down seen []int bufferSize int + buffered int + chunksLeft int + errorIs error }{ { // a sniffer that discovers its needs incrementally (HTTP/2: preface, @@ -181,15 +185,19 @@ func TestDispatcherFeedLoop(t *testing.T) { seen: []int{10}, }, { - // Peek cannot return more than the buffer holds + // An oversized request must be rejected before the reader grows or + // consumes more data while trying to fill its buffer. name: "stops when the request exceeds the peek limit", chunks: threeSegments, reply: func(data []byte) (string, error) { return "", needAtLeast(1 << 20) }, wantErr: true, + errorIs: bufio.ErrBufferFull, seen: []int{10}, - bufferSize: maxSniffBufferSize, + bufferSize: 4096, + buffered: 10, + chunksLeft: 2, }, { name: "gives up when the data never completes", @@ -220,10 +228,19 @@ func TestDispatcherFeedLoop(t *testing.T) { assert.NoError(t, err) assert.Equal(t, test.host, host) } + if test.errorIs != nil { + assert.ErrorIs(t, err, test.errorIs) + } assert.Equal(t, test.seen, s.seen) if test.bufferSize != 0 { assert.Equal(t, test.bufferSize, conn.Reader().Size()) } + if test.buffered != 0 { + assert.Equal(t, test.buffered, conn.Buffered()) + } + if test.chunksLeft != 0 { + assert.Len(t, raw.chunks, test.chunksLeft) + } // sniffing must not leave a deadline behind for the relay that follows assert.True(t, raw.deadline.IsZero(), "read deadline was not cleared") assert.GreaterOrEqual(t, len(raw.deadlines), 2, "initial and shared deadlines must both be set")