Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(lark): reject chunk resends with a mismatched sum (#11476)
T Mohan Aditya0394c5b6de82
diff --git a/server/internal/integrations/lark/ws_chunk_assembler.go b/server/internal/integrations/lark/ws_chunk_assembler.goindex f028826a56..1b68823f74 100644--- a/server/internal/integrations/lark/ws_chunk_assembler.go+++ b/server/internal/integrations/lark/ws_chunk_assembler.go@@ -11,9 +11,9 @@ import (// arrived. Lark splits large event payloads across multiple binary// Frames with the headers://-// sum — total number of chunks (>=2 means multi-frame)-// seq — 0-based index of THIS chunk within the message-// message_id — common key across the N chunks+// sum — total number of chunks (>=2 means multi-frame)+// seq — 0-based index of THIS chunk within the message+// message_id — common key across the N chunks//// The SDK reference (larksuite/oapi-sdk-go/v3/ws/client.go combine())// uses a 5-second TTL on partial state — anything older than that is@@ -87,6 +87,12 @@ func (a *chunkAssembler) admit(messageID string, sum, seq int, payload []byte) (deadline: a.now().Add(a.ttl),}a.buf[messageID] = entry+ } else if len(entry.chunks) != sum {+ // A pending message has a fixed chunk count. A resend with a+ // different sum is malformed: indexing outside the buffered slice+ // would panic, and an in-range index would corrupt the counter.+ // Ignore it and keep the original assembly intact.+ return nil, false}// Duplicate chunk (network retry / out-of-order Lark resend): we// silently overwrite. Lark guarantees the payload bytes are stablediff --git a/server/internal/integrations/lark/ws_chunk_assembler_test.go b/server/internal/integrations/lark/ws_chunk_assembler_test.goindex ac5ab5e9f8..d4c52e8ce0 100644--- a/server/internal/integrations/lark/ws_chunk_assembler_test.go+++ b/server/internal/integrations/lark/ws_chunk_assembler_test.go@@ -178,14 +178,34 @@ func TestChunkAssemblerRejectsBadInputs(t *testing.T) {}}+func TestChunkAssemblerRejectsSumMismatchForPendingMessage(t *testing.T) {+ t.Parallel()+ a := newChunkAssembler(time.Second, nil)+ if _, ok := a.admit("om-mismatch", 2, 0, []byte("A")); ok {+ t.Fatal("first chunk should be partial")+ }+ // A resend carrying a different sum is malformed: it must not index+ // outside the buffered slice or corrupt the original assembly.+ if out, ok := a.admit("om-mismatch", 5, 4, []byte("bad")); ok || out != nil {+ t.Fatalf("mismatched sum should be rejected; got (%q,%v)", out, ok)+ }+ if a.pendingCount() != 1 {+ t.Fatalf("pendingCount after mismatch = %d; want 1", a.pendingCount())+ }+ out, ok := a.admit("om-mismatch", 2, 1, []byte("B"))+ if !ok || !bytes.Equal(out, []byte("AB")) {+ t.Fatalf("assembly after mismatch = %q,%v; want AB,true", out, ok)+ }+}+func TestParseChunkHeaders(t *testing.T) {t.Parallel()tests := []struct {- name string- headers []FrameHeader- wantSum int- wantSeq int- wantMsgID string+ name string+ headers []FrameHeader+ wantSum int+ wantSeq int+ wantMsgID string}{{name: "all present",