Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

feat(mq): read thrift binary message payloads

colabhoangbui21106a99d99250de
diff --git a/agents/drivers/rabbitmq/mapping_test.go b/agents/drivers/rabbitmq/mapping_test.go
index 91e0045d3c..00e42e1992 100644
--- a/agents/drivers/rabbitmq/mapping_test.go
+++ b/agents/drivers/rabbitmq/mapping_test.go
@@ -342,3 +342,43 @@ func TestGetTopicConfigPreservesArgumentTypes(t *testing.T) {
t.Fatalf("unexpected argument values %#v", configs)
}
}
+
+func TestDecodeDeliveryPayloadTextDecodesThriftBinaryProtocol(t *testing.T) {
+ // Strict TBinaryProtocol v1 CALL: method="ping", seqId=7, field 1 (string)="ok", field 2 (i32)=42, STOP
+ strictCall := []byte{
+ 0x80, 0x01, 0x00, 0x01,
+ 0x00, 0x00, 0x00, 0x04, 'p', 'i', 'n', 'g',
+ 0x00, 0x00, 0x00, 0x07,
+ 0x0b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 'o', 'k',
+ 0x08, 0x00, 0x02, 0x00, 0x00, 0x00, 0x2a,
+ 0x00,
+ }
+ decoded, ok := decodeDeliveryPayloadText(strictCall)
+ if !ok {
+ t.Fatal("expected strict TBinaryProtocol payload to decode")
+ }
+ parsed := mustObject(t, decoded)
+ if parsed["method"] != "ping" || parsed["type"] != "CALL" {
+ t.Fatalf("unexpected decoded thrift envelope %#v", parsed)
+ }
+ fields := objectOrNil(parsed, "fields")
+ if fields["1"] != "ok" || fields["2"].(json.Number).String() != "42" {
+ t.Fatalf("unexpected decoded thrift fields %#v", fields)
+ }
+
+ // Raw TSerializer struct (valid UTF-8 bytes < 0x80 with NUL/control bytes): field 1 (string)="hello", STOP
+ rawStruct := []byte{0x0b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 'h', 'e', 'l', 'l', 'o', 0x00}
+ decodedStruct, ok := decodeDeliveryPayloadText(rawStruct)
+ if !ok {
+ t.Fatal("expected raw TSerializer struct to decode")
+ }
+ structObj := mustObject(t, decodedStruct)
+ if structObj["1"] != "hello" {
+ t.Fatalf("unexpected decoded struct %#v", structObj)
+ }
+
+ // Plain text stays untouched.
+ if plain, ok := decodeDeliveryPayloadText([]byte("hello world")); !ok || plain != "hello world" {
+ t.Fatalf("unexpected plain payload %q (%v)", plain, ok)
+ }
+}
diff --git a/agents/drivers/rabbitmq/operations.go b/agents/drivers/rabbitmq/operations.go
index 87205d5cd5..54d6b35d91 100644
--- a/agents/drivers/rabbitmq/operations.go
+++ b/agents/drivers/rabbitmq/operations.go
@@ -11,7 +11,6 @@ import (
"regexp"
"sort"
"strings"
- "unicode/utf8"
amqp "github.com/rabbitmq/amqp091-go"
)
@@ -1276,8 +1275,8 @@ func peekedMessageFromDelivery(queue string, index int64, delivery amqp.Delivery
if !delivery.Timestamp.IsZero() {
message["timestamp"] = delivery.Timestamp.UnixMilli()
}
- if utf8.Valid(delivery.Body) {
- message["payloadText"] = string(delivery.Body)
+ if text, ok := decodeDeliveryPayloadText(delivery.Body); ok {
+ message["payloadText"] = text
}
return message
}
diff --git a/agents/drivers/rabbitmq/thrift_binary.go b/agents/drivers/rabbitmq/thrift_binary.go
new file mode 100644
index 0000000000..88213ccdb5
--- /dev/null
+++ b/agents/drivers/rabbitmq/thrift_binary.go
@@ -0,0 +1,436 @@
+package main
+
+import (
+ "bytes"
+ "encoding/binary"
+ "encoding/hex"
+ "encoding/json"
+ "errors"
+ "math"
+ "strconv"
+ "strings"
+ "unicode/utf8"
+)
+
+const (
+ thriftVersionMask uint32 = 0xffff0000
+ thriftVersion1 uint32 = 0x80010000
+ thriftTypeMask uint32 = 0x000000ff
+ thriftMaxDepth = 32
+ thriftMaxContainerItems = 10000
+ thriftMaxMethodNameBytes = 256
+)
+
+const (
+ thriftStop byte = 0
+ thriftVoid byte = 1
+ thriftBool byte = 2
+ thriftByte byte = 3
+ thriftDouble byte = 4
+ thriftI16 byte = 6
+ thriftI32 byte = 8
+ thriftI64 byte = 10
+ thriftString byte = 11
+ thriftStruct byte = 12
+ thriftMap byte = 13
+ thriftSet byte = 14
+ thriftList byte = 15
+ thriftUUID byte = 16
+)
+
+var errInvalidThrift = errors.New("invalid thrift binary payload")
+
+type thriftReader struct {
+ data []byte
+ pos int
+}
+
+func (r *thriftReader) remaining() int {
+ return len(r.data) - r.pos
+}
+
+func (r *thriftReader) atEnd() bool {
+ return r.pos >= len(r.data)
+}
+
+func (r *thriftReader) take(n int) ([]byte, error) {
+ if n < 0 || r.pos+n > len(r.data) {
+ return nil, errInvalidThrift
+ }
+ slice := r.data[r.pos : r.pos+n]
+ r.pos += n
+ return slice, nil
+}
+
+func (r *thriftReader) u8() (byte, error) {
+ b, err := r.take(1)
+ if err != nil {
+ return 0, err
+ }
+ return b[0], nil
+}
+
+func (r *thriftReader) i16() (int16, error) {
+ b, err := r.take(2)
+ if err != nil {
+ return 0, err
+ }
+ return int16(binary.BigEndian.Uint16(b)), nil
+}
+
+func (r *thriftReader) i32() (int32, error) {
+ b, err := r.take(4)
+ if err != nil {
+ return 0, err
+ }
+ return int32(binary.BigEndian.Uint32(b)), nil
+}
+
+func (r *thriftReader) u32() (uint32, error) {
+ b, err := r.take(4)
+ if err != nil {
+ return 0, err
+ }
+ return binary.BigEndian.Uint32(b), nil
+}
+
+func (r *thriftReader) i64() (int64, error) {
+ b, err := r.take(8)
+ if err != nil {
+ return 0, err
+ }
+ return int64(binary.BigEndian.Uint64(b)), nil
+}
+
+func (r *thriftReader) f64() (float64, error) {
+ b, err := r.take(8)
+ if err != nil {
+ return 0, err
+ }
+ v := math.Float64frombits(binary.BigEndian.Uint64(b))
+ if math.IsNaN(v) || math.IsInf(v, 0) {
+ return 0, errInvalidThrift
+ }
+ return v, nil
+}
+
+func isPrintableUTF8(data []byte) bool {
+ if !utf8.Valid(data) {
+ return false
+ }
+ for _, r := range string(data) {
+ if (r < 0x20 && r != '\t' && r != '\n' && r != '\r') || r == 0x7f {
+ return false
+ }
+ }
+ return true
+}
+
+func isValidThriftMethodName(name string) bool {
+ if len(name) == 0 || len(name) > thriftMaxMethodNameBytes {
+ return false
+ }
+ for i := 0; i < len(name); i++ {
+ if name[i] <= 0x20 || name[i] >= 0x7f {
+ return false
+ }
+ }
+ return true
+}
+
+func thriftMessageTypeName(code byte) (string, bool) {
+ switch code {
+ case 1:
+ return "CALL", true
+ case 2:
+ return "REPLY", true
+ case 3:
+ return "EXCEPTION", true
+ case 4:
+ return "ONEWAY", true
+ default:
+ return "", false
+ }
+}
+
+func decodeThriftBinaryOrString(data []byte, depth int) any {
+ if len(data) == 0 {
+ return ""
+ }
+ if isPrintableUTF8(data) {
+ return string(data)
+ }
+ if depth < thriftMaxDepth {
+ if nested, err := decodeThriftRawStruct(data, depth+1); err == nil {
+ return nested
+ }
+ }
+ return map[string]any{"$bytes": hex.EncodeToString(data)}
+}
+
+func readThriftValue(r *thriftReader, fieldType byte, depth int) (any, error) {
+ if depth > thriftMaxDepth {
+ return nil, errInvalidThrift
+ }
+ switch fieldType {
+ case thriftVoid:
+ return nil, nil
+ case thriftBool:
+ b, err := r.u8()
+ if err != nil || (b != 0 && b != 1) {
+ return nil, errInvalidThrift
+ }
+ return b == 1, nil
+ case thriftByte:
+ b, err := r.u8()
+ if err != nil {
+ return nil, err
+ }
+ return int8(b), nil
+ case thriftDouble:
+ return r.f64()
+ case thriftI16:
+ return r.i16()
+ case thriftI32:
+ return r.i32()
+ case thriftI64:
+ return r.i64()
+ case thriftString:
+ length, err := r.i32()
+ if err != nil || length < 0 {
+ return nil, errInvalidThrift
+ }
+ raw, err := r.take(int(length))
+ if err != nil {
+ return nil, err
+ }
+ return decodeThriftBinaryOrString(raw, depth), nil
+ case thriftStruct:
+ return readThriftStruct(r, depth+1)
+ case thriftMap:
+ keyType, err := r.u8()
+ if err != nil {
+ return nil, err
+ }
+ valType, err := r.u8()
+ if err != nil {
+ return nil, err
+ }
+ size, err := r.i32()
+ if err != nil || size < 0 || int(size) > thriftMaxContainerItems || int(size) > r.remaining() {
+ return nil, errInvalidThrift
+ }
+ out := make(map[string]any, int(size))
+ for i := 0; i < int(size); i++ {
+ k, err := readThriftValue(r, keyType, depth+1)
+ if err != nil {
+ return nil, err
+ }
+ v, err := readThriftValue(r, valType, depth+1)
+ if err != nil {
+ return nil, err
+ }
+ keyBytes, err := json.Marshal(k)
+ if err != nil {
+ return nil, errInvalidThrift
+ }
+ keyStr := string(keyBytes)
+ if len(keyStr) >= 2 && keyStr[0] == '"' && keyStr[len(keyStr)-1] == '"' {
+ if unquoted, err := strconv.Unquote(keyStr); err == nil {
+ keyStr = unquoted
+ }
+ }
+ out[keyStr] = v
+ }
+ return out, nil
+ case thriftSet, thriftList:
+ elemType, err := r.u8()
+ if err != nil {
+ return nil, err
+ }
+ size, err := r.i32()
+ if err != nil || size < 0 || int(size) > thriftMaxContainerItems || int(size) > r.remaining() {
+ return nil, errInvalidThrift
+ }
+ items := make([]any, 0, int(size))
+ for i := 0; i < int(size); i++ {
+ item, err := readThriftValue(r, elemType, depth+1)
+ if err != nil {
+ return nil, err
+ }
+ items = append(items, item)
+ }
+ return items, nil
+ case thriftUUID:
+ raw, err := r.take(16)
+ if err != nil {
+ return nil, err
+ }
+ h := hex.EncodeToString(raw)
+ return h[0:8] + "-" + h[8:12] + "-" + h[12:16] + "-" + h[16:20] + "-" + h[20:32], nil
+ default:
+ return nil, errInvalidThrift
+ }
+}
+
+func readThriftStruct(r *thriftReader, depth int) (map[string]any, error) {
+ if depth > thriftMaxDepth {
+ return nil, errInvalidThrift
+ }
+ fields := make(map[string]any)
+ for {
+ fieldType, err := r.u8()
+ if err != nil {
+ return nil, err
+ }
+ if fieldType == thriftStop {
+ break
+ }
+ fieldID, err := r.i16()
+ if err != nil || fieldID < 0 {
+ return nil, errInvalidThrift
+ }
+ key := strconv.Itoa(int(fieldID))
+ if _, exists := fields[key]; exists {
+ return nil, errInvalidThrift
+ }
+ val, err := readThriftValue(r, fieldType, depth)
+ if err != nil {
+ return nil, err
+ }
+ fields[key] = val
+ }
+ return fields, nil
+}
+
+func decodeThriftStrictMessage(data []byte) (map[string]any, error) {
+ r := &thriftReader{data: data}
+ versionAndType, err := r.u32()
+ if err != nil || (versionAndType&thriftVersionMask) != thriftVersion1 || (versionAndType&0x0000ff00) != 0 {
+ return nil, errInvalidThrift
+ }
+ msgType, ok := thriftMessageTypeName(byte(versionAndType & thriftTypeMask))
+ if !ok {
+ return nil, errInvalidThrift
+ }
+ nameLen, err := r.i32()
+ if err != nil || nameLen <= 0 || int(nameLen) > thriftMaxMethodNameBytes {
+ return nil, errInvalidThrift
+ }
+ nameBytes, err := r.take(int(nameLen))
+ if err != nil || !isPrintableUTF8(nameBytes) {
+ return nil, errInvalidThrift
+ }
+ method := string(nameBytes)
+ if !isValidThriftMethodName(method) {
+ return nil, errInvalidThrift
+ }
+ seqID, err := r.i32()
+ if err != nil {
+ return nil, err
+ }
+ fields, err := readThriftStruct(r, 0)
+ if err != nil || !r.atEnd() {
+ return nil, errInvalidThrift
+ }
+ return map[string]any{
+ "method": method,
+ "type": msgType,
+ "seqId": seqID,
+ "fields": fields,
+ }, nil
+}
+
+func decodeThriftNonStrictMessage(data []byte) (map[string]any, error) {
+ r := &thriftReader{data: data}
+ nameLen, err := r.i32()
+ if err != nil || nameLen <= 0 || int(nameLen) > thriftMaxMethodNameBytes {
+ return nil, errInvalidThrift
+ }
+ nameBytes, err := r.take(int(nameLen))
+ if err != nil || !isPrintableUTF8(nameBytes) {
+ return nil, errInvalidThrift
+ }
+ method := string(nameBytes)
+ if !isValidThriftMethodName(method) {
+ return nil, errInvalidThrift
+ }
+ typeByte, err := r.u8()
+ if err != nil {
+ return nil, err
+ }
+ msgType, ok := thriftMessageTypeName(typeByte)
+ if !ok {
+ return nil, errInvalidThrift
+ }
+ seqID, err := r.i32()
+ if err != nil {
+ return nil, err
+ }
+ fields, err := readThriftStruct(r, 0)
+ if err != nil || !r.atEnd() {
+ return nil, errInvalidThrift
+ }
+ return map[string]any{
+ "method": method,
+ "type": msgType,
+ "seqId": seqID,
+ "fields": fields,
+ }, nil
+}
+
+func decodeThriftRawStruct(data []byte, depth int) (map[string]any, error) {
+ r := &thriftReader{data: data}
+ fields, err := readThriftStruct(r, depth)
+ if err != nil || !r.atEnd() || len(fields) == 0 {
+ return nil, errInvalidThrift
+ }
+ return fields, nil
+}
+
+func decodeThriftUnframed(data []byte) (map[string]any, error) {
+ if len(data) >= 4 && (binary.BigEndian.Uint32(data[:4])&thriftVersionMask) == thriftVersion1 {
+ return decodeThriftStrictMessage(data)
+ }
+ if msg, err := decodeThriftNonStrictMessage(data); err == nil {
+ return msg, nil
+ }
+ return decodeThriftRawStruct(data, 0)
+}
+
+func decodeThriftBinaryJSON(data []byte) (string, bool) {
+ if len(data) == 0 {
+ return "", false
+ }
+ value, err := decodeThriftUnframed(data)
+ if err != nil && len(data) > 4 {
+ frameLen := binary.BigEndian.Uint32(data[:4])
+ if frameLen > 0 && int(frameLen) == len(data)-4 {
+ value, err = decodeThriftUnframed(data[4:])
+ }
+ }
+ if err != nil {
+ return "", false
+ }
+ var buf bytes.Buffer
+ enc := json.NewEncoder(&buf)
+ enc.SetEscapeHTML(false)
+ enc.SetIndent("", " ")
+ if err := enc.Encode(value); err != nil {
+ return "", false
+ }
+ return strings.TrimSuffix(buf.String(), "\n"), true
+}
+
+func decodeDeliveryPayloadText(body []byte) (string, bool) {
+ if len(body) > 0 && isPrintableUTF8(body) {
+ return string(body), true
+ }
+ if decoded, ok := decodeThriftBinaryJSON(body); ok {
+ return decoded, true
+ }
+ if utf8.Valid(body) {
+ return string(body), true
+ }
+ return "", false
+}
diff --git a/apps/desktop/src/components/mq/MessageBrowser.vue b/apps/desktop/src/components/mq/MessageBrowser.vue
index 559d036b4c..736662aa99 100644
--- a/apps/desktop/src/components/mq/MessageBrowser.vue
+++ b/apps/desktop/src/components/mq/MessageBrowser.vue
@@ -9,6 +9,7 @@ import { copyToClipboard } from "@/lib/common/clipboard";
import { buildKafkaMessageSearchText, kafkaMessageSearchTextMatches, normalizeKafkaMessageSearchQuery } from "@/lib/mq/kafkaMessageSearch";
import { sortKafkaMessagesByPublishTime, type KafkaMessageDisplayOrder } from "@/lib/mq/kafkaMessageSort";
import { parseNonNegativeSafeInteger } from "@/lib/mq/mqPeekFilters";
+import { formatMqMessagePayload } from "@/lib/mq/thriftBinary";
import { useToast } from "@/composables/useToast";
import { Button } from "@/components/ui/button";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
@@ -142,7 +143,9 @@ function invalidateMessageRequest() {
}
function messagePayload(message: PeekedMessage): string {
- return message.payloadText ?? message.payloadBase64;
+ // Raw-struct thrift sniffing is opt-in per MQ kind: only RabbitMQ's payload
+ // contract is thrift, so Kafka/RocketMQ keep the base64/text fallback.
+ return formatMqMessagePayload(message, { allowRawStructSniff: props.mqSystemKind === "rabbitmq" });
}
async function copyMessagePayload(message: PeekedMessage) {
diff --git a/apps/desktop/src/components/mq/SubscriptionsPanel.vue b/apps/desktop/src/components/mq/SubscriptionsPanel.vue
index b3761078a9..6d6d90f926 100644
--- a/apps/desktop/src/components/mq/SubscriptionsPanel.vue
+++ b/apps/desktop/src/components/mq/SubscriptionsPanel.vue
@@ -8,6 +8,7 @@ import RocketMqConsumerGroupDialogs, { type RocketMqConsumerGroupDialogKind } fr
import MqTypeFilterBar from "./shared/MqTypeFilterBar.vue";
import DangerConfirmDialog from "@/components/editor/DangerConfirmDialog.vue";
import { DEFAULT_ROCKETMQ_CONSUMER_GROUP_TYPE_FILTERS, matchesRocketMqConsumerGroupTypeFilters, resolveRocketMqConsumerGroupMessageModel, resolveRocketMqConsumerGroupType, ROCKETMQ_CONSUMER_GROUP_TYPES, type RocketMqConsumerGroupType } from "@/lib/mq/rocketmqConsumerGroupTypes";
+import { formatMqMessagePayload } from "@/lib/mq/thriftBinary";
import { useMqMutationGuard } from "@/composables/useMqMutationGuard";
interface Props {
@@ -789,7 +790,7 @@ onBeforeUnmount(() => {
<div v-if="Object.keys(message.properties).length" class="peek-properties">
<span v-for="(value, key) in message.properties" :key="key">{{ key }}={{ value }}</span>
</div>
- <pre class="peek-payload">{{ message.payloadText ?? message.payloadBase64 }}</pre>
+ <pre class="peek-payload">{{ formatMqMessagePayload(message, { allowRawStructSniff: props.mqSystemKind === "rabbitmq" }) }}</pre>
</div>
</div>
</div>
diff --git a/apps/desktop/src/components/mq/__tests__/MessageBrowser.spec.ts b/apps/desktop/src/components/mq/__tests__/MessageBrowser.spec.ts
index 13ad96690c..597abf3212 100644
--- a/apps/desktop/src/components/mq/__tests__/MessageBrowser.spec.ts
+++ b/apps/desktop/src/components/mq/__tests__/MessageBrowser.spec.ts
@@ -189,6 +189,24 @@ describe("MessageBrowser", () => {
expect(clipboard.copyToClipboard).toHaveBeenCalledWith("AAE=");
});
+ it("decodes thrift.TBinaryProtocol payloads when browsing RabbitMQ messages", async () => {
+ // Raw TSerializer struct: field 1 (string) = "hello", STOP -> base64 "CwABAAAABWhlbGxvAA=="
+ backend.mqPeekMessages.mockResolvedValueOnce([
+ {
+ position: 1,
+ messageId: "thrift-1",
+ payloadBase64: "CwABAAAABWhlbGxvAA==",
+ payloadText: "\u000b\u0000\u0001\u0000\u0000\u0000\u0005hello\u0000",
+ properties: {},
+ headers: {},
+ },
+ ]);
+ const browser = await mountBrowser("rabbitmq");
+
+ await loadMessages(browser);
+ expect(displayedPayloads(browser)).toEqual(['{\n "1": "hello"\n}']);
+ });
+
it("copies message headers as formatted JSON", async () => {
backend.mqPeekMessages.mockResolvedValueOnce([
{
diff --git a/apps/desktop/src/lib/mq/__tests__/thriftBinary.spec.ts b/apps/desktop/src/lib/mq/__tests__/thriftBinary.spec.ts
new file mode 100644
index 0000000000..04caebaa71
--- /dev/null
+++ b/apps/desktop/src/lib/mq/__tests__/thriftBinary.spec.ts
@@ -0,0 +1,133 @@
+import { describe, expect, it } from "vitest";
+import { decodeThriftBinary, decodeThriftBinaryBase64, formatMqMessagePayload } from "@/lib/mq/thriftBinary";
+
+function toBase64(bytes: number[]): string {
+ return btoa(String.fromCharCode(...bytes));
+}
+
+describe("thriftBinary (thrift.TBinaryProtocol)", () => {
+ it("decodes strict VERSION_1 RPC CALL messages with nested fields and containers", () => {
+ // VERSION_1 | CALL (0x80010001), method="createOrder", seqId=12,
+ // field 1 (STRING) = "ord-9", field 2 (I32) = 250, field 3 (BOOL) = true,
+ // field 4 (LIST<I16>) = [10, 20], field 5 (MAP<STRING, I32>) = {"qty": 3}, STOP
+ const payload = new Uint8Array([
+ 0x80,
+ 0x01,
+ 0x00,
+ 0x01,
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x0b,
+ ...new TextEncoder().encode("createOrder"),
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x0c,
+ 0x0b,
+ 0x00,
+ 0x01,
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x05,
+ ...new TextEncoder().encode("ord-9"),
+ 0x08,
+ 0x00,
+ 0x02,
+ 0x00,
+ 0x00,
+ 0x00,
+ 0xfa,
+ 0x02,
+ 0x00,
+ 0x03,
+ 0x01,
+ 0x0f,
+ 0x00,
+ 0x04,
+ 0x06,
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x02,
+ 0x00,
+ 0x0a,
+ 0x00,
+ 0x14,
+ 0x0d,
+ 0x00,
+ 0x05,
+ 0x0b,
+ 0x08,
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x01,
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x03,
+ ...new TextEncoder().encode("qty"),
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x03,
+ 0x00,
+ ]);
+
+ const result = decodeThriftBinary(payload);
+ expect(result?.value).toEqual({
+ method: "createOrder",
+ type: "CALL",
+ seqId: 12,
+ fields: {
+ "1": "ord-9",
+ "2": 250,
+ "3": true,
+ "4": [10, 20],
+ "5": { qty: 3 },
+ },
+ });
+ });
+
+ it("decodes TFramedTransport-prefixed and raw TSerializer structs", () => {
+ const structBytes = [0x0b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, ...new TextEncoder().encode("hello"), 0x0c, 0x00, 0x02, 0x08, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00];
+ const framed = [0x00, 0x00, 0x00, structBytes.length, ...structBytes];
+
+ expect(decodeThriftBinaryBase64(toBase64(framed))?.value).toBeUndefined();
+ expect(decodeThriftBinaryBase64(toBase64(framed), { allowRawStructSniff: true })?.value).toEqual({
+ "1": "hello",
+ "2": { "1": 7 },
+ });
+
+ // Raw (headerless) TSerializer structs are the same opt-in heuristic:
+ // without the flag the decoder returns null instead of guessing at
+ // arbitrary binary.
+ expect(decodeThriftBinaryBase64(toBase64(structBytes))?.value).toBeUndefined();
+ expect(decodeThriftBinaryBase64(toBase64(structBytes), { allowRawStructSniff: true })?.value).toEqual({
+ "1": "hello",
+ "2": { "1": 7 },
+ });
+ });
+
+ it("formats RabbitMQ TBinaryProtocol messages even when Go utf8.Valid passed control bytes", () => {
+ const rawBytes = [0x0b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, ...new TextEncoder().encode("hello"), 0x00];
+ const rawTextWithControls = String.fromCharCode(...rawBytes);
+
+ expect(
+ formatMqMessagePayload(
+ {
+ payloadBase64: toBase64(rawBytes),
+ payloadText: rawTextWithControls,
+ },
+ { allowRawStructSniff: true },
+ ),
+ ).toBe('{\n "1": "hello"\n}');
+ });
+
+ it("preserves plain text and non-Thrift base64 fallbacks", () => {
+ expect(formatMqMessagePayload({ payloadBase64: "", payloadText: "existing message" })).toBe("existing message");
+ expect(formatMqMessagePayload({ payloadBase64: "AAE=", payloadText: undefined })).toBe("AAE=");
+ });
+});
diff --git a/apps/desktop/src/lib/mq/thriftBinary.ts b/apps/desktop/src/lib/mq/thriftBinary.ts
new file mode 100644
index 0000000000..98311ec870
--- /dev/null
+++ b/apps/desktop/src/lib/mq/thriftBinary.ts
@@ -0,0 +1,403 @@
+import type { PeekedMessage } from "@/types/mq";
+
+export type ThriftMessageType = "CALL" | "REPLY" | "EXCEPTION" | "ONEWAY";
+
+export interface ThriftBinaryDecodeResult {
+ formattedText: string;
+ value: Record<string, unknown>;
+}
+
+const THRIFT_VERSION_MASK = 0xffff0000;
+const THRIFT_VERSION_1 = 0x80010000;
+const THRIFT_TYPE_MASK = 0x000000ff;
+const MAX_DEPTH = 32;
+const MAX_CONTAINER_ITEMS = 10_000;
+const MAX_METHOD_NAME_LENGTH = 256;
+
+const MESSAGE_TYPES: Record<number, ThriftMessageType> = {
+ 1: "CALL",
+ 2: "REPLY",
+ 3: "EXCEPTION",
+ 4: "ONEWAY",
+};
+
+const TType = {
+ STOP: 0,
+ VOID: 1,
+ BOOL: 2,
+ BYTE: 3,
+ DOUBLE: 4,
+ I16: 6,
+ I32: 8,
+ I64: 10,
+ STRING: 11,
+ STRUCT: 12,
+ MAP: 13,
+ SET: 14,
+ LIST: 15,
+ UUID: 16,
+} as const;
+
+class ThriftBinaryReader {
+ private pos = 0;
+ private readonly bytes: Uint8Array;
+ private readonly view: DataView;
+
+ constructor(bytes: Uint8Array) {
+ this.bytes = bytes;
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
+ }
+
+ remaining(): number {
+ return this.bytes.length - this.pos;
+ }
+
+ atEnd(): boolean {
+ return this.pos >= this.bytes.length;
+ }
+
+ private ensure(size: number): void {
+ if (size < 0 || this.pos + size > this.bytes.length) {
+ throw new Error("truncated thrift payload");
+ }
+ }
+
+ u8(): number {
+ this.ensure(1);
+ return this.bytes[this.pos++];
+ }
+
+ i8(): number {
+ this.ensure(1);
+ return this.view.getInt8(this.pos++);
+ }
+
+ i16(): number {
+ this.ensure(2);
+ const value = this.view.getInt16(this.pos, false);
+ this.pos += 2;
+ return value;
+ }
+
+ i32(): number {
+ this.ensure(4);
+ const value = this.view.getInt32(this.pos, false);
+ this.pos += 4;
+ return value;
+ }
+
+ u32(): number {
+ this.ensure(4);
+ const value = this.view.getUint32(this.pos, false);
+ this.pos += 4;
+ return value;
+ }
+
+ i64(): bigint {
+ this.ensure(8);
+ const value = this.view.getBigInt64(this.pos, false);
+ this.pos += 8;
+ return value;
+ }
+
+ f64(): number {
+ this.ensure(8);
+ const value = this.view.getFloat64(this.pos, false);
+ this.pos += 8;
+ return value;
+ }
+
+ bytesSlice(length: number): Uint8Array {
+ if (!Number.isSafeInteger(length) || length < 0) {
+ throw new Error("invalid thrift byte length");
+ }
+ this.ensure(length);
+ const slice = this.bytes.subarray(this.pos, this.pos + length);
+ this.pos += length;
+ return slice;
+ }
+}
+
+export function isPrintableText(text: string): boolean {
+ for (let i = 0; i < text.length; i++) {
+ const code = text.charCodeAt(i);
+ if ((code < 0x20 && code !== 0x09 && code !== 0x0a && code !== 0x0d) || code === 0x7f) {
+ return false;
+ }
+ }
+ return true;
+}
+
+function decodeUtf8Printable(bytes: Uint8Array): string | null {
+ try {
+ const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
+ return isPrintableText(text) ? text : null;
+ } catch {
+ return null;
+ }
+}
+
+function bytesToHex(bytes: Uint8Array): string {
+ return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
+}
+
+function formatUuid(bytes: Uint8Array): string {
+ const hex = bytesToHex(bytes);
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
+}
+
+function decodeBinaryOrString(bytes: Uint8Array, depth: number): unknown {
+ if (bytes.length === 0) return "";
+ const text = decodeUtf8Printable(bytes);
+ if (text !== null) return text;
+ if (depth < MAX_DEPTH) {
+ try {
+ return decodeRawStruct(bytes, depth + 1);
+ } catch {
+ // Fall through to hex bytes representation.
+ }
+ }
+ return { $bytes: bytesToHex(bytes) };
+}
+
+function readValue(reader: ThriftBinaryReader, type: number, depth: number): unknown {
+ if (depth > MAX_DEPTH) throw new Error("thrift nesting too deep");
+ switch (type) {
+ case TType.VOID:
+ return null;
+ case TType.BOOL: {
+ const raw = reader.u8();
+ if (raw !== 0 && raw !== 1) throw new Error("invalid thrift bool");
+ return raw === 1;
+ }
+ case TType.BYTE:
+ return reader.i8();
+ case TType.DOUBLE:
+ return reader.f64();
+ case TType.I16:
+ return reader.i16();
+ case TType.I32:
+ return reader.i32();
+ case TType.I64: {
+ const raw = reader.i64();
+ return raw >= BigInt(Number.MIN_SAFE_INTEGER) && raw <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(raw) : raw.toString();
+ }
+ case TType.STRING: {
+ const length = reader.i32();
+ return decodeBinaryOrString(reader.bytesSlice(length), depth);
+ }
+ case TType.STRUCT:
+ return readStruct(reader, depth + 1);
+ case TType.MAP: {
+ const keyType = reader.u8();
+ const valType = reader.u8();
+ const size = reader.i32();
+ if (size < 0 || size > MAX_CONTAINER_ITEMS || size > reader.remaining()) {
+ throw new Error("invalid thrift map size");
+ }
+ const entries: Array<[unknown, unknown]> = [];
+ let allPrimitiveKeys = true;
+ for (let i = 0; i < size; i++) {
+ const key = readValue(reader, keyType, depth + 1);
+ const value = readValue(reader, valType, depth + 1);
+ if (typeof key !== "string" && typeof key !== "number" && typeof key !== "boolean") {
+ allPrimitiveKeys = false;
+ }
+ entries.push([key, value]);
+ }
+ if (allPrimitiveKeys) {
+ return Object.fromEntries(entries.map(([key, value]) => [String(key), value]));
+ }
+ return entries;
+ }
+ case TType.SET:
+ case TType.LIST: {
+ const elemType = reader.u8();
+ const size = reader.i32();
+ if (size < 0 || size > MAX_CONTAINER_ITEMS || size > reader.remaining()) {
+ throw new Error("invalid thrift list/set size");
+ }
+ const items: unknown[] = [];
+ for (let i = 0; i < size; i++) {
+ items.push(readValue(reader, elemType, depth + 1));
+ }
+ return items;
+ }
+ case TType.UUID:
+ return formatUuid(reader.bytesSlice(16));
+ default:
+ throw new Error(`unsupported thrift type: ${type}`);
+ }
+}
+
+function readStruct(reader: ThriftBinaryReader, depth: number): Record<string, unknown> {
+ if (depth > MAX_DEPTH) throw new Error("thrift struct nesting too deep");
+ const fields: Record<string, unknown> = {};
+ while (true) {
+ const fieldType = reader.u8();
+ if (fieldType === TType.STOP) break;
+ const fieldId = reader.i16();
+ if (fieldId < 0) throw new Error("invalid thrift field id");
+ const key = String(fieldId);
+ if (Object.prototype.hasOwnProperty.call(fields, key)) {
+ throw new Error("duplicate thrift field id");
+ }
+ fields[key] = readValue(reader, fieldType, depth);
+ }
+ return fields;
+}
+
+function isValidMethodName(name: string): boolean {
+ if (name.length === 0 || name.length > MAX_METHOD_NAME_LENGTH) return false;
+ for (let i = 0; i < name.length; i++) {
+ const code = name.charCodeAt(i);
+ if (code <= 0x20 || code >= 0x7f) return false;
+ }
+ return true;
+}
+
+function decodeStrictMessage(bytes: Uint8Array): Record<string, unknown> {
+ const reader = new ThriftBinaryReader(bytes);
+ const versionAndType = reader.u32();
+ if ((versionAndType & THRIFT_VERSION_MASK) >>> 0 !== THRIFT_VERSION_1) {
+ throw new Error("not strict thrift v1 message");
+ }
+ if ((versionAndType & 0x0000ff00) !== 0) {
+ throw new Error("invalid thrift v1 message flags");
+ }
+ const messageType = MESSAGE_TYPES[versionAndType & THRIFT_TYPE_MASK];
+ if (!messageType) throw new Error("invalid thrift message type");
+ const nameLength = reader.i32();
+ if (nameLength <= 0 || nameLength > MAX_METHOD_NAME_LENGTH) {
+ throw new Error("invalid thrift method name length");
+ }
+ const method = decodeUtf8Printable(reader.bytesSlice(nameLength));
+ if (!method || !isValidMethodName(method)) {
+ throw new Error("invalid thrift method name");
+ }
+ const seqId = reader.i32();
+ const fields = readStruct(reader, 0);
+ if (!reader.atEnd()) throw new Error("trailing bytes after thrift message");
+ return {
+ method,
+ type: messageType,
+ seqId,
+ fields,
+ };
+}
+
+function decodeNonStrictMessage(bytes: Uint8Array): Record<string, unknown> {
+ const reader = new ThriftBinaryReader(bytes);
+ const nameLength = reader.i32();
+ if (nameLength <= 0 || nameLength > MAX_METHOD_NAME_LENGTH) {
+ throw new Error("invalid thrift method name length");
+ }
+ const method = decodeUtf8Printable(reader.bytesSlice(nameLength));
+ if (!method || !isValidMethodName(method)) {
+ throw new Error("invalid thrift method name");
+ }
+ const messageType = MESSAGE_TYPES[reader.u8()];
+ if (!messageType) throw new Error("invalid thrift message type");
+ const seqId = reader.i32();
+ const fields = readStruct(reader, 0);
+ if (!reader.atEnd()) throw new Error("trailing bytes after thrift message");
+ return {
+ method,
+ type: messageType,
+ seqId,
+ fields,
+ };
+}
+
+function decodeRawStruct(bytes: Uint8Array, depth = 0): Record<string, unknown> {
+ const reader = new ThriftBinaryReader(bytes);
+ const fields = readStruct(reader, depth);
+ if (!reader.atEnd() || Object.keys(fields).length === 0) {
+ throw new Error("invalid thrift struct");
+ }
+ return fields;
+}
+
+function tryDecodeEnveloped(bytes: Uint8Array): Record<string, unknown> | null {
+ try {
+ return decodeEnveloped(bytes);
+ } catch {
+ return null;
+ }
+}
+
+function tryDecodeFramed(bytes: Uint8Array, allowRawStruct: boolean): Record<string, unknown> | null {
+ if (bytes.length <= 4) return null;
+ const frameLength = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(0, false);
+ if (frameLength === 0 || frameLength !== bytes.length - 4) return null;
+ const inner = bytes.subarray(4);
+ return tryDecodeEnveloped(inner) ?? (allowRawStruct ? tryDecodeRawStruct(inner) : null);
+}
+
+function tryDecodeRawStruct(bytes: Uint8Array): Record<string, unknown> | null {
+ try {
+ return decodeRawStruct(bytes);
+ } catch {
+ return null;
+ }
+}
+
+export function decodeThriftBinary(bytes: Uint8Array, options: { allowRawStructSniff?: boolean } = {}): ThriftBinaryDecodeResult | null {
+ if (bytes.length === 0) return null;
+ // Enveloped thrift messages carry an unambiguous header (strict version word
+ // or a 0x80 0x01 non-strict marker). A headerless payload only "decoding" as
+ // a raw struct is a heuristic that can misfire on arbitrary binary, so it is
+ // opt-in (RabbitMQ's payload contract) rather than applied to every MQ kind.
+ const enveloped = tryDecodeEnveloped(bytes) ?? tryDecodeFramed(bytes, options.allowRawStructSniff === true);
+ if (enveloped) return { formattedText: JSON.stringify(enveloped, null, 2), value: enveloped };
+ if (options.allowRawStructSniff !== true) return null;
+ const raw = tryDecodeRawStruct(bytes);
+ return raw ? { formattedText: JSON.stringify(raw, null, 2), value: raw } : null;
+}
+
+function decodeEnveloped(bytes: Uint8Array): Record<string, unknown> {
+ const firstWord = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(0, false);
+ if (bytes.length >= 4 && (firstWord & THRIFT_VERSION_MASK) >>> 0 === THRIFT_VERSION_1) {
+ return decodeStrictMessage(bytes);
+ }
+ return decodeNonStrictMessage(bytes);
+}
+
+function base64ToBytes(base64: string): Uint8Array | null {
+ const normalized = base64.trim();
+ if (!normalized) return null;
+ try {
+ const binary = atob(normalized);
+ const bytes = new Uint8Array(binary.length);
+ for (let i = 0; i < binary.length; i++) {
+ bytes[i] = binary.charCodeAt(i);
+ }
+ return bytes;
+ } catch {
+ return null;
+ }
+}
+
+export function decodeThriftBinaryBase64(base64?: string, options: { allowRawStructSniff?: boolean } = {}): ThriftBinaryDecodeResult | null {
+ if (!base64) return null;
+ const bytes = base64ToBytes(base64);
+ if (!bytes) return null;
+ return decodeThriftBinary(bytes, options);
+}
+
+export function formatMqMessagePayload(message: Pick<PeekedMessage, "payloadText" | "payloadBase64">, options: { allowRawStructSniff?: boolean } = {}): string {
+ if (message.payloadText !== undefined && message.payloadText !== "" && isPrintableText(message.payloadText)) {
+ return message.payloadText;
+ }
+ const decodedFromBase64 = decodeThriftBinaryBase64(message.payloadBase64, options);
+ if (decodedFromBase64) {
+ return decodedFromBase64.formattedText;
+ }
+ if (message.payloadText) {
+ const decodedFromText = decodeThriftBinary(new TextEncoder().encode(message.payloadText), options);
+ if (decodedFromText) {
+ return decodedFromText.formattedText;
+ }
+ }
+ return message.payloadText ?? message.payloadBase64;
+}
diff --git a/crates/dbx-core/src/admin/mq/adapters/rabbitmq.rs b/crates/dbx-core/src/admin/mq/adapters/rabbitmq.rs
index edb5650d82..7e96eb88d0 100644
--- a/crates/dbx-core/src/admin/mq/adapters/rabbitmq.rs
+++ b/crates/dbx-core/src/admin/mq/adapters/rabbitmq.rs
@@ -1126,6 +1126,8 @@ fn peeked_message_from_json(idx: usize, m: &serde_json::Value) -> PeekedMessage
.and_then(|v| v.as_object())
.map(|obj| obj.iter().map(|(k, v)| (k.clone(), v.as_str().unwrap_or_default().to_string())).collect())
.unwrap_or_default();
+ let payload_base64 = m.get("payloadBase64").and_then(|v| v.as_str()).unwrap_or_default().to_string();
+ let payload_text = resolve_rabbitmq_payload_text(m.get("payloadText").and_then(|v| v.as_str()), &payload_base64);
PeekedMessage {
position: (idx + 1) as u32,
message_id: m
@@ -1148,11 +1150,294 @@ fn peeked_message_from_json(idx: usize, m: &serde_json::Value) -> PeekedMessage
.and_then(|v| v.as_object())
.map(|obj| obj.iter().map(|(k, v)| (k.clone(), v.as_str().unwrap_or_default().to_string())).collect())
.unwrap_or_default(),
- payload_base64: m.get("payloadBase64").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
- payload_text: m.get("payloadText").and_then(|v| v.as_str()).map(String::from),
+ payload_base64,
+ payload_text,
}
}
+fn is_printable_text(text: &str) -> bool {
+ text.chars().all(|c| (c >= ' ' || matches!(c, '\t' | '\n' | '\r')) && c != '\u{7f}')
+}
+
+fn resolve_rabbitmq_payload_text(raw_text: Option<&str>, payload_base64: &str) -> Option<String> {
+ if let Some(text) = raw_text {
+ if !text.is_empty() && is_printable_text(text) {
+ return Some(text.to_string());
+ }
+ }
+ if !payload_base64.is_empty() {
+ use base64::Engine as _;
+ if let Ok(bytes) = base64::engine::general_purpose::STANDARD.decode(payload_base64.trim()) {
+ if let Some(decoded) = decode_thrift_binary_json(&bytes) {
+ return Some(decoded);
+ }
+ }
+ }
+ if let Some(text) = raw_text {
+ if let Some(decoded) = decode_thrift_binary_json(text.as_bytes()) {
+ return Some(decoded);
+ }
+ }
+ raw_text.map(String::from)
+}
+
+const THRIFT_VERSION_MASK: u32 = 0xffff_0000;
+const THRIFT_VERSION_1: u32 = 0x8001_0000;
+const THRIFT_MAX_DEPTH: usize = 32;
+const THRIFT_MAX_ITEMS: usize = 10_000;
+const THRIFT_MAX_METHOD_LEN: usize = 256;
+
+struct ThriftCursor<'a> {
+ data: &'a [u8],
+ pos: usize,
+}
+
+impl<'a> ThriftCursor<'a> {
+ fn remaining(&self) -> usize {
+ self.data.len().saturating_sub(self.pos)
+ }
+
+ fn at_end(&self) -> bool {
+ self.pos >= self.data.len()
+ }
+
+ fn take(&mut self, len: usize) -> Option<&'a [u8]> {
+ let end = self.pos.checked_add(len)?;
+ if end > self.data.len() {
+ return None;
+ }
+ let slice = &self.data[self.pos..end];
+ self.pos = end;
+ Some(slice)
+ }
+
+ fn u8(&mut self) -> Option<u8> {
+ Some(self.take(1)?[0])
+ }
+
+ fn i16(&mut self) -> Option<i16> {
+ Some(i16::from_be_bytes(self.take(2)?.try_into().ok()?))
+ }
+
+ fn i32(&mut self) -> Option<i32> {
+ Some(i32::from_be_bytes(self.take(4)?.try_into().ok()?))
+ }
+
+ fn u32(&mut self) -> Option<u32> {
+ Some(u32::from_be_bytes(self.take(4)?.try_into().ok()?))
+ }
+
+ fn i64(&mut self) -> Option<i64> {
+ Some(i64::from_be_bytes(self.take(8)?.try_into().ok()?))
+ }
+
+ fn f64(&mut self) -> Option<f64> {
+ let val = f64::from_be_bytes(self.take(8)?.try_into().ok()?);
+ val.is_finite().then_some(val)
+ }
+}
+
+fn thrift_message_type(code: u8) -> Option<&'static str> {
+ match code {
+ 1 => Some("CALL"),
+ 2 => Some("REPLY"),
+ 3 => Some("EXCEPTION"),
+ 4 => Some("ONEWAY"),
+ _ => None,
+ }
+}
+
+fn is_valid_thrift_method(name: &str) -> bool {
+ !name.is_empty() && name.len() <= THRIFT_MAX_METHOD_LEN && name.bytes().all(|b| (0x21..0x7f).contains(&b))
+}
+
+fn bytes_to_hex(bytes: &[u8]) -> String {
+ let mut out = String::with_capacity(bytes.len() * 2);
+ for b in bytes {
+ use std::fmt::Write as _;
+ let _ = write!(&mut out, "{b:02x}");
+ }
+ out
+}
+
+fn read_thrift_value(cur: &mut ThriftCursor<'_>, field_type: u8, depth: usize) -> Option<serde_json::Value> {
+ if depth > THRIFT_MAX_DEPTH {
+ return None;
+ }
+ match field_type {
+ 1 => Some(serde_json::Value::Null),
+ 2 => match cur.u8()? {
+ 0 => Some(serde_json::Value::Bool(false)),
+ 1 => Some(serde_json::Value::Bool(true)),
+ _ => None,
+ },
+ 3 => Some(serde_json::json!(cur.u8()? as i8)),
+ 4 => Some(serde_json::json!(cur.f64()?)),
+ 6 => Some(serde_json::json!(cur.i16()?)),
+ 8 => Some(serde_json::json!(cur.i32()?)),
+ 10 => Some(serde_json::json!(cur.i64()?)),
+ 11 => {
+ let len = usize::try_from(cur.i32()?).ok()?;
+ let raw = cur.take(len)?;
+ if raw.is_empty() {
+ return Some(serde_json::Value::String(String::new()));
+ }
+ if let Ok(text) = std::str::from_utf8(raw) {
+ if is_printable_text(text) {
+ return Some(serde_json::Value::String(text.to_string()));
+ }
+ }
+ if depth < THRIFT_MAX_DEPTH {
+ if let Some(nested) = decode_thrift_raw_struct(raw, depth + 1) {
+ return Some(nested);
+ }
+ }
+ Some(serde_json::json!({ "$bytes": bytes_to_hex(raw) }))
+ }
+ 12 => Some(serde_json::Value::Object(read_thrift_struct(cur, depth + 1)?)),
+ 13 => {
+ let key_type = cur.u8()?;
+ let val_type = cur.u8()?;
+ let size = usize::try_from(cur.i32()?).ok()?;
+ if size > THRIFT_MAX_ITEMS || size > cur.remaining() {
+ return None;
+ }
+ let mut map = serde_json::Map::with_capacity(size);
+ for _ in 0..size {
+ let k = read_thrift_value(cur, key_type, depth + 1)?;
+ let v = read_thrift_value(cur, val_type, depth + 1)?;
+ let key_str = match k {
+ serde_json::Value::String(s) => s,
+ other => other.to_string(),
+ };
+ map.insert(key_str, v);
+ }
+ Some(serde_json::Value::Object(map))
+ }
+ 14 | 15 => {
+ let elem_type = cur.u8()?;
+ let size = usize::try_from(cur.i32()?).ok()?;
+ if size > THRIFT_MAX_ITEMS || size > cur.remaining() {
+ return None;
+ }
+ let mut items = Vec::with_capacity(size);
+ for _ in 0..size {
+ items.push(read_thrift_value(cur, elem_type, depth + 1)?);
+ }
+ Some(serde_json::Value::Array(items))
+ }
+ 16 => {
+ let hex = bytes_to_hex(cur.take(16)?);
+ Some(serde_json::Value::String(format!(
+ "{}-{}-{}-{}-{}",
+ &hex[0..8],
+ &hex[8..12],
+ &hex[12..16],
+ &hex[16..20],
+ &hex[20..32]
+ )))
+ }
+ _ => None,
+ }
+}
+
+fn read_thrift_struct(cur: &mut ThriftCursor<'_>, depth: usize) -> Option<serde_json::Map<String, serde_json::Value>> {
+ if depth > THRIFT_MAX_DEPTH {
+ return None;
+ }
+ let mut fields = serde_json::Map::new();
+ loop {
+ let field_type = cur.u8()?;
+ if field_type == 0 {
+ break;
+ }
+ let field_id = cur.i16()?;
+ if field_id < 0 {
+ return None;
+ }
+ let key = field_id.to_string();
+ if fields.contains_key(&key) {
+ return None;
+ }
+ let val = read_thrift_value(cur, field_type, depth)?;
+ fields.insert(key, val);
+ }
+ Some(fields)
+}
+
+fn decode_thrift_strict_message(bytes: &[u8]) -> Option<serde_json::Value> {
+ let mut cur = ThriftCursor { data: bytes, pos: 0 };
+ let version_and_type = cur.u32()?;
+ if (version_and_type & THRIFT_VERSION_MASK) != THRIFT_VERSION_1 || (version_and_type & 0x0000_ff00) != 0 {
+ return None;
+ }
+ let msg_type = thrift_message_type((version_and_type & 0xff) as u8)?;
+ let name_len = usize::try_from(cur.i32()?).ok()?;
+ let method = std::str::from_utf8(cur.take(name_len)?).ok()?;
+ if !is_valid_thrift_method(method) {
+ return None;
+ }
+ let seq_id = cur.i32()?;
+ let fields = read_thrift_struct(&mut cur, 0)?;
+ cur.at_end().then(|| {
+ serde_json::json!({
+ "method": method,
+ "type": msg_type,
+ "seqId": seq_id,
+ "fields": fields,
+ })
+ })
+}
+
+fn decode_thrift_non_strict_message(bytes: &[u8]) -> Option<serde_json::Value> {
+ let mut cur = ThriftCursor { data: bytes, pos: 0 };
+ let name_len = usize::try_from(cur.i32()?).ok()?;
+ let method = std::str::from_utf8(cur.take(name_len)?).ok()?;
+ if !is_valid_thrift_method(method) {
+ return None;
+ }
+ let msg_type = thrift_message_type(cur.u8()?)?;
+ let seq_id = cur.i32()?;
+ let fields = read_thrift_struct(&mut cur, 0)?;
+ cur.at_end().then(|| {
+ serde_json::json!({
+ "method": method,
+ "type": msg_type,
+ "seqId": seq_id,
+ "fields": fields,
+ })
+ })
+}
+
+fn decode_thrift_raw_struct(bytes: &[u8], depth: usize) -> Option<serde_json::Value> {
+ let mut cur = ThriftCursor { data: bytes, pos: 0 };
+ let fields = read_thrift_struct(&mut cur, depth)?;
+ (cur.at_end() && !fields.is_empty()).then_some(serde_json::Value::Object(fields))
+}
+
+fn decode_thrift_unframed(bytes: &[u8]) -> Option<serde_json::Value> {
+ if bytes.len() >= 4 && (u32::from_be_bytes(bytes[..4].try_into().ok()?) & THRIFT_VERSION_MASK) == THRIFT_VERSION_1 {
+ return decode_thrift_strict_message(bytes);
+ }
+ decode_thrift_non_strict_message(bytes).or_else(|| decode_thrift_raw_struct(bytes, 0))
+}
+
+fn decode_thrift_binary_json(bytes: &[u8]) -> Option<String> {
+ if bytes.is_empty() {
+ return None;
+ }
+ let value = decode_thrift_unframed(bytes).or_else(|| {
+ if bytes.len() > 4 {
+ let frame_len = u32::from_be_bytes(bytes[..4].try_into().ok()?) as usize;
+ if frame_len > 0 && frame_len == bytes.len() - 4 {
+ return decode_thrift_unframed(&bytes[4..]);
+ }
+ }
+ None
+ })?;
+ serde_json::to_string_pretty(&value).ok()
+}
+
/// Map an `mq_get_topic_stats` agent payload onto the UI-friendly TopicStats.
///
/// RabbitMQ rates come from the management API's `message_stats` sample. A
@@ -1572,6 +1857,27 @@ mod tests {
assert!(peeked.publish_time.is_none());
}
+ #[test]
+ fn peeked_message_decodes_thrift_binary_protocol() {
+ use base64::Engine as _;
+ let strict_call = [
+ 0x80u8, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, b'p', b'i', b'n', b'g', 0x00, 0x00, 0x00, 0x07, 0x0b,
+ 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, b'o', b'k', 0x08, 0x00, 0x02, 0x00, 0x00, 0x00, 0x2a, 0x00,
+ ];
+ let msg = serde_json::json!({
+ "deliveryTag": 9,
+ "payloadBase64": base64::engine::general_purpose::STANDARD.encode(strict_call),
+ });
+ let peeked = peeked_message_from_json(0, &msg);
+ let decoded: serde_json::Value =
+ serde_json::from_str(peeked.payload_text.as_deref().expect("decoded thrift")).expect("valid json");
+ assert_eq!(decoded["method"], "ping");
+ assert_eq!(decoded["type"], "CALL");
+ assert_eq!(decoded["seqId"], 7);
+ assert_eq!(decoded["fields"]["1"], "ok");
+ assert_eq!(decoded["fields"]["2"], 42);
+ }
+
#[test]
#[allow(clippy::assertions_on_constants)]
fn capabilities_enable_namespaces_and_clear_backlog() {