{"record":{"id":"8bbe7af1d54f50a5","repo":"t8y2/dbx","slug":"zookeeper-frame-length-d-is-invalid","errorCode":null,"errorMessage":"ZooKeeper frame length %d is invalid","messagePattern":"ZooKeeper frame length (.+?) is invalid","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"agents/drivers/zookeeper/sasl.go","lineNumber":160,"sourceCode":"\t}\n\tif len(response) < 24 {\n\t\treturn nil, errors.New(\"ZooKeeper SASL token is truncated\")\n\t}\n\ttokenLength := int(int32(binary.BigEndian.Uint32(response[20:24])))\n\tif tokenLength < 0 || tokenLength > zooKeeperMaximumFrameLen || 24+tokenLength > len(response) {\n\t\treturn nil, fmt.Errorf(\"ZooKeeper SASL token length %d is invalid\", tokenLength)\n\t}\n\treturn append([]byte(nil), response[24:24+tokenLength]...), nil\n}\n\nfunc readZooKeeperFrame(reader io.Reader) ([]byte, error) {\n\theader := make([]byte, 4)\n\tif _, err := io.ReadFull(reader, header); err != nil {\n\t\treturn nil, err\n\t}\n\tlength := int(int32(binary.BigEndian.Uint32(header)))\n\tif length < 0 || length > zooKeeperMaximumFrameLen {\n\t\treturn nil, fmt.Errorf(\"ZooKeeper frame length %d is invalid\", length)\n\t}\n\tpayload := make([]byte, length+4)\n\tcopy(payload, header)\n\tif _, err := io.ReadFull(reader, payload[4:]); err != nil {\n\t\treturn nil, err\n\t}\n\treturn payload, nil\n}\n\nfunc writeZooKeeperFrame(writer io.Writer, payload []byte) error {\n\tif len(payload) > zooKeeperMaximumFrameLen {\n\t\treturn fmt.Errorf(\"ZooKeeper frame length %d exceeds maximum %d\", len(payload), zooKeeperMaximumFrameLen)\n\t}\n\theader := make([]byte, 4)\n\tbinary.BigEndian.PutUint32(header, uint32(len(payload)))\n\tif err := writeAll(writer, header); err != nil {\n\t\treturn err\n\t}","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/zookeeper/sasl.go#L142-L178","documentation":"readZooKeeperFrame reads a 4-byte big-endian length header and validates it before allocating. Lengths that are negative (as int32) or above zooKeeperMaximumFrameLen are rejected with this error, defending against absurd allocation sizes from corrupt or hostile input. The wire stream is then considered untrustworthy.","triggerScenarios":"Read or zooKeeperSASLRound calls readZooKeeperFrame; the first 4 bytes of the next frame decode to an int32 that is < 0 or > zooKeeperMaximumFrameLen (e.g. reading at a misaligned offset, or garbage from a plaintext server response to a binary probe).","commonSituations":"Connecting to a non-ZooKeeper service on the configured port, stream desynchronization after a prior partial read, TLS/proxy corruption, or a test fake writing malformed frames (TestZooKeeperFrameValidation exercises this).","solutions":["Verify the endpoint really is a ZooKeeper server speaking the frame protocol (right port, right service).","Close and reopen the connection — once framing is desynchronized the stream cannot recover.","Check for code reading from the same connection outside readZooKeeperFrame, which shifts the framing offset.","Ensure proxies/TLS termination are not corrupting the byte stream."],"exampleFix":"// before\nresp, _ := http.Get(\"http://host:2181/\") // hitting ZK port with HTTP, then reading frames\n// after\nconn, _ := net.Dial(\"tcp\", \"zk-host:2181\") // speak the binary protocol directly","handlingStrategy":"type-guard","validationCode":"func plausibleFrameHeader(b []byte) bool {\n\tif len(b) < 4 { return false }\n\tn := int(int32(binary.BigEndian.Uint32(b)))\n\treturn n >= 0 && n <= zooKeeperMaximumFrameLen\n}","typeGuard":"func isFrameLengthErr(err error) bool {\n\treturn err != nil && strings.Contains(err.Error(), \"frame length\") && strings.Contains(err.Error(), \"is invalid\")\n}","tryCatchPattern":"frame, err := readZooKeeperFrame(reader)\nif isFrameLengthErr(err) {\n\tconn.Close() // stream is desynchronized; a fresh connection is required\n\treturn reconnectAndRedial()\n}","preventionTips":["Only read frames from connections you exclusively own (single reader goroutine).","Confirm the target service is actually ZooKeeper before enabling the driver.","Reconnect on any framing error; never attempt to continue reading a misaligned stream."],"tags":["protocol","framing","zookeeper","validation"],"backgroundTag":"frame-length-invalid","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}