{"record":{"id":"72f93d833c8911f6","repo":"txthinking/brook","slug":"packet-too-long","errorCode":null,"errorMessage":"packet too long","messagePattern":"packet too long","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"simplestreamserver.go","lineNumber":146,"sourceCode":"\t\t\t\treturn nil\n\t\t\t}\n\t\t}\n\t\tif s.network == \"tcp\" {\n\t\t\tl, err := s.Client.Read(s.RB)\n\t\t\tif err != nil {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tif _, err := remote.Write(s.RB[:l]); err != nil {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t}\n\t\tif s.network == \"udp\" {\n\t\t\tif _, err := io.ReadFull(s.Client, s.RB[:2]); err != nil {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tl := int(binary.BigEndian.Uint16(s.RB[:2]))\n\t\t\tif l > 65507-2 {\n\t\t\t\treturn errors.New(\"packet too long\")\n\t\t\t}\n\t\t\tif _, err := io.ReadFull(s.Client, s.RB[2:2+l]); err != nil {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t\tif _, err := remote.Write(s.RB[2 : 2+l]); err != nil {\n\t\t\t\treturn nil\n\t\t\t}\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc (s *SimpleStreamServer) Network() string {\n\treturn s.network\n}\n\nfunc (s *SimpleStreamServer) Src() string {\n\treturn s.src","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/txthinking/brook/blob/5cd13ef3b1fb574e88ebf2c1b5d95f2ebe1342c8/simplestreamserver.go#L128-L164","documentation":"Exchange reads a 2-byte big-endian length prefix for a UDP relay packet and rejects the packet if the length exceeds 65507-2 bytes, the theoretical maximum UDP datagram payload. This guards the fixed read buffer s.RB against oversized/invalid length prefixes. A corrupt or malicious stream can set the prefix to a value that cannot be a valid UDP datagram.","triggerScenarios":"Calling Exchange on a UDP-mode stream when the first 2 bytes read from the client decode (via binary.BigEndian.Uint16) to a length > 65505; this happens when the peer sends malformed framed data or the stream is desynchronized.","commonSituations":"A non-conforming client writes raw bytes without the 2-byte length framing; a middlebox corrupts the stream; a client implementation uses a wrong byte order or includes extra header bytes in the length.","solutions":["Verify the client frames each UDP payload with a 2-byte big-endian length prefix before sending","Check that the declared length includes/excludes the prefix consistently on both ends (server expects payload <= 65505)","Resynchronize or recreate the connection once the stream is desynchronized - the framing cannot recover mid-stream","Cap the datagram size on the client side to 65507 bytes total as required by UDP"],"exampleFix":"// before: client sends raw datagram\nconn.Write(payload)\n\n// after: frame with 2-byte big-endian length\nbuf := make([]byte, 2+len(payload))\nbinary.BigEndian.PutUint16(buf[:2], uint16(len(payload)))\ncopy(buf[2:], payload)\nconn.Write(buf)","handlingStrategy":"validation","validationCode":"func validUdpFrame(prefix []byte, payloadLen int) bool {\n\tl := int(binary.BigEndian.Uint16(prefix[:2]))\n\treturn l > 0 && l <= 65505 && payloadLen == l\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always frame UDP payloads with a 2-byte big-endian length prefix","Keep datagrams <= 65507 bytes total","Treat any desync as fatal and reconnect rather than resuming mid-stream"],"tags":["udp","protocol","framing"],"backgroundTag":"payload-too-large","analyzedSha":"5cd13ef3b1fb574e88ebf2c1b5d95f2ebe1342c8","analyzedAt":"2026-09-06T04:35:00.432Z","contentChangedAt":"2026-09-06T04:35:00.432Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}