chore: reject oversized sniff reads before filling the buffer

This commit is contained in:
wwqgtxx committed 2026-07-28 22:53:06 +08:00
1 parent 669db1ba22
commit 94a89fb70f
2 files changed
+25 -9

No files matched your search

+6 -7
View File
@@ -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)
+19 -2
View File
@@ -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")