Read returns a truncated fragmented message as complete when the connection closes mid-message
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 62/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- go
- Lĩnh vực
- networking
Hướng nghiên cứu
Start in msgReader.read and msgReader.Read in read.go, and check how readLoop and readFramePayload surface transport EOF. Run the reproduction test, which should fail because a truncated fragmented message returns nil error. Done when a connection closed mid-message yields io.ErrUnexpectedEOF and go test ./... passes. The compressed final-frame case mentioned in the issue still needs handling.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
If the peer closes the connection in the middle of a fragmented message, Conn.Read (and the io.Reader from Conn.Reader) returns the frames received so far as a complete message with a nil error, instead of failing. The caller can't tell that the message is truncated. The next read then fails with previous message not read to completion.
Version: v1.8.15 (also on master).
Cause
msgReader.read returns the transport error as is when the connection closes while it waits for the next continuation frame's header (readLoop) or reads a frame payload (readFramePayload). Those errors wrap io.EOF. msgReader.Read then treats any io.EOF as a normal end of message:
// read.go
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) && mr.fin && mr.flate {
mr.putFlateReader()
return n, io.EOF
}
So io.ReadAll / Conn.Read see a clean end of message.
Reproduction
A server that accepts a connection and calls Read; a raw client sends a text frame with FIN=0 and then closes its side.
package wsrepro
import (
"bufio"
"context"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/coder/websocket"
)
func TestTruncatedFragmentedMessageIsReturnedAsComplete(t *testing.T) {
type result struct {
typ websocket.MessageType
data []byte
err error
}
results := make(chan result, 1)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
if err != nil {
results <- result{err: err}
return
}
typ, data, err := c.Read(context.Background())
results <- result{typ, data, err}
}))
defer srv.Close()
conn, err := net.Dial("tcp", strings.TrimPrefix(srv.URL, "http://"))
if err != nil {
t.Fatal(err)
}
_, _ = conn.Write([]byte("GET / HTTP/1.1\r\nHost: x\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n" +
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n"))
resp, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil || resp.StatusCode != http.StatusSwitchingProtocols {
t.Fatalf("handshake: %v %v", resp, err)
}
payload := []byte("hello")
frame := []byte{0x01, 0x80 | byte(len(payload)), 0, 0, 0, 0} // text, FIN=0, masked with a zero key
frame = append(frame, payload...)
_, _ = conn.Write(frame)
_ = conn.(*net.TCPConn).CloseWrite()
got := <-results
t.Logf("type=%v data=%q err=%v", got.typ, got.data, got.err)
if got.err == nil {
t.Fatalf("got a complete message %q from a fragmented message whose final frame never arrived", got.data)
}
}
Output on v1.8.15:
main_test.go:51: type=MessageText data="hello" err=<nil>
main_test.go:53: got a complete message "hello" from a fragmented message whose final frame never arrived
The same happens if the connection closes after a frame header but before its payload, since io.ReadFull returns io.EOF when it reads no bytes.
Possible fix
Parenthesizing the condition in msgReader.Read isn't enough: without compression, msgReader.read's own end-of-message signal is also a bare io.EOF, and several existing tests rely on it. Converting a transport EOF inside a message to io.ErrUnexpectedEOF in msgReader.read fixes the reproduction above and keeps go test ./... passing:
@@ func (mr *msgReader) read(p []byte) (int, error) {
h, err := mr.c.readLoop(mr.ctx)
if err != nil {
+ if errors.Is(err, io.EOF) {
+ return 0, fmt.Errorf("connection closed before the message was complete: %w", io.ErrUnexpectedEOF)
+ }
return 0, err
}
@@
n, err := mr.c.readFramePayload(mr.ctx, p)
if err != nil {
+ if errors.Is(err, io.EOF) {
+ return n, fmt.Errorf("connection closed before the message was complete: %w", io.ErrUnexpectedEOF)
+ }
return n, err
}
One thing I didn't check: with compression, a cut inside the final frame's payload still matches io.ErrUnexpectedEOF && mr.fin && mr.flate and would still be reported as io.EOF. That case probably needs to tell the flate tail apart from a transport error too.
- Ngôn ngữ chính
- Go
- Star
- 5.5k
- Fork
- 379
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của coder/websocket
-
Must not wrap io.EOFCó thể đã có người làm @Yanhu007 đã nhận 189 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
export wstestĐang mởenhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 22/100
-
wasm: Close with codes other than 1000 throws, so CloseNow never closesCó thể đã có người làm @0pcom đã nhận 15 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 20/100
Tất cả issue của coder/websocket
Issue tương tự
-
[Low] CSI plugin builds staging directories from storage_volume_id without a containment checkĐang mởarea/security kind/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
agent-substrate/substrate#2935 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
automation models
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Guardian health server binds :8080 and collides with the Voltron proxyCó thể đã có người làm @denislemire đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
projectcalico/calico#14341 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
blinklabs-io/dingo#5368 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
OpenAI compat: assistant tool_calls attached to previous message when content is an empty arrayCó thể đã có người làm @ShryukGrandhi đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày