From 5e056a9a5aab5f688f8ecfda7aff1530514f3089 Mon Sep 17 00:00:00 2001 From: Develop-KIM Date: Fri, 2 Oct 2026 11:00:50 +0900 Subject: [PATCH 1/3] fix: decode RESP3 attribute replies instead of failing the connection ioredis defaults to protocol 3, but the decoder has no case for the RESP3 attribute type (`|`, 124). A single attribute reply therefore ends the connection: the decoder throws "Unknown RESP type 124", DataHandler turns that into a fatal protocol error, and every command in flight on the socket is dropped. `DEBUG PROTOCOL attrib` reproduces it, and the public module API (`RedisModule_ReplyWithAttribute`) lets any module produce one. An attribute is metadata for the reply behind it, not a reply of its own, so at the top level the attribute map is consumed without invoking a callback - the main loop then decodes the real reply that follows - and nested in an array, set or map the element after the attribute takes its place. The contents are discarded, which the spec allows and which is all the connection needs; surfacing them to callers would be a separate API change. --- lib/resp/decoder.ts | 39 +++++++++++++++++++++++ test/unit/resp/decoder.ts | 66 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/lib/resp/decoder.ts b/lib/resp/decoder.ts index b2312c57d..15b847879 100644 --- a/lib/resp/decoder.ts +++ b/lib/resp/decoder.ts @@ -25,6 +25,7 @@ export const RESP_TYPES = { ARRAY: 42, // * SET: 126, // ~ MAP: 37, // % + ATTRIBUTE: 124, // | PUSH: 62, // > } as const; @@ -40,6 +41,10 @@ export const PUSH_TYPE_MAPPING = { [RESP_TYPES.BLOB_STRING]: Buffer, }; +// Attribute contents aren't surfaced to callers; this keeps the decoded map +// from being reported as a reply of its own. +function discardAttribute() {} + // this was written with performance in mind, so it's not very readable... sorry :( interface DecoderOptions { @@ -196,6 +201,17 @@ export class Decoder { this.#decodeMap(this.getTypeMapping(), chunk) ); + // An attribute is metadata for the reply that follows it, not a reply of + // its own. The spec lets clients ignore the contents, but they still have + // to be consumed: leaving them in the stream turns the reply behind them + // into a protocol error. Dropping out of #decodeTypeValue without calling + // a callback resumes the main loop on the real reply. + case RESP_TYPES.ATTRIBUTE: + return this.#handleDecodedValue( + discardAttribute, + this.#decodeMap(this.getTypeMapping(), chunk) + ); + case RESP_TYPES.PUSH: return this.#handleDecodedValue( this.onPush, @@ -641,6 +657,15 @@ export class Decoder { case RESP_TYPES.MAP: return this.#decodeMap(typeMapping, chunk); + // Nested attributes decorate the element that follows them, so the + // element takes their place in the array/set/map being decoded. + case RESP_TYPES.ATTRIBUTE: + return this.#decodeAttribute( + this.#decodeMap(typeMapping, chunk), + typeMapping, + chunk + ); + default: throw new Error( `Unknown RESP type ${type} "${String.fromCharCode(type)}"` @@ -648,6 +673,20 @@ export class Decoder { } } + #decodeAttribute(attribute, typeMapping, chunk) { + if (typeof attribute === "function") { + return this.#continueDecodeAttribute.bind(this, attribute, typeMapping); + } + + return this.#cursor >= chunk.length + ? this.#decodeNestedType.bind(this, typeMapping) + : this.#decodeNestedType(typeMapping, chunk); + } + + #continueDecodeAttribute(attributeCb, typeMapping, chunk) { + return this.#decodeAttribute(attributeCb(chunk), typeMapping, chunk); + } + #decodeArray(typeMapping, chunk) { // RESP 2 null // https://github.com/redis/redis-specifications/blob/master/protocol/RESP2.md#resp-arrays diff --git a/test/unit/resp/decoder.ts b/test/unit/resp/decoder.ts index 849f3fd9a..4f62c22ba 100644 --- a/test/unit/resp/decoder.ts +++ b/test/unit/resp/decoder.ts @@ -552,6 +552,72 @@ describe("RESP Decoder", () => { }); }); + // Attributes are metadata attached to the reply behind them. They carry no + // reply of their own, so nothing reaches onReply until the real reply is + // decoded - and a client that doesn't consume them reads that real reply as + // an unknown type and kills the connection. + describe("Attribute", () => { + test("DEBUG PROTOCOL attrib", { + toWrite: Buffer.from( + "|1\r\n$14\r\nkey-popularity\r\n*2\r\n$7\r\nkey:123\r\n:90\r\n$39\r\nSome real reply following the attribute\r\n" + ), + replies: ["Some real reply following the attribute"], + }); + + test("empty", { + toWrite: Buffer.from("|0\r\n+OK\r\n"), + replies: ["OK"], + }); + + test("two in a row", { + toWrite: Buffer.from("|1\r\n+a\r\n+b\r\n|1\r\n+c\r\n+d\r\n:1\r\n"), + replies: [1], + }); + + test("before an error", { + toWrite: Buffer.from("|1\r\n+a\r\n+b\r\n-ERROR\r\n"), + errorReplies: [new SimpleError("ERROR")], + }); + + test("before a push", { + toWrite: Buffer.from("|1\r\n+a\r\n+b\r\n>2\r\n:0\r\n:1\r\n"), + pushReplies: [[0, 1]], + }); + + test("on an array element", { + toWrite: Buffer.from("*2\r\n|1\r\n+a\r\n+b\r\n:1\r\n:2\r\n"), + replies: [[1, 2]], + }); + + test("on a nested array element", { + toWrite: Buffer.from("*1\r\n*1\r\n|1\r\n+a\r\n+b\r\n:1\r\n"), + replies: [[[1]]], + }); + + test("on a map value", { + toWrite: Buffer.from("%1\r\n+key\r\n|1\r\n+a\r\n+b\r\n:1\r\n"), + replies: [{ key: 1 }], + }); + + test("on a map key", { + toWrite: Buffer.from("%1\r\n|1\r\n+a\r\n+b\r\n+key\r\n:1\r\n"), + replies: [{ key: 1 }], + }); + + test("on a set element as Set", { + typeMapping: { + [RESP_TYPES.SET]: Set, + }, + toWrite: Buffer.from("~1\r\n|1\r\n+a\r\n+b\r\n:1\r\n"), + replies: [new Set([1])], + }); + + test("with a map value of its own", { + toWrite: Buffer.from("|1\r\n+a\r\n%1\r\n+b\r\n:1\r\n:2\r\n"), + replies: [2], + }); + }); + describe("Push", () => { test("[]", { toWrite: Buffer.from(">0\r\n"), From 3e7c9e6783825022d38bcde5ff5ae8005536202a Mon Sep 17 00:00:00 2001 From: Develop-KIM Date: Fri, 2 Oct 2026 11:18:09 +0900 Subject: [PATCH 2/3] fix: keep attributed map keys on the map-key path The first commit resumed every attributed element through #decodeNestedType, so a map key behind an attribute followed typeMapping instead of being coerced to a string like any other key. With blob strings mapped to Buffer - what both of DataHandler's presets do - that key came back as a Buffer from a Map, so map.get("foo") missed it, and in the object path it walked past the `__proto__` guard in #decodeMapAsObject, whose own-property check compares against a string. A reply with an attributed `__proto__` key and an object value then changed the decoded object's prototype. Pass the decoder for the position the attribute was found in, so a key resumes through #decodeMapKey and everything else through #decodeNestedType. --- lib/resp/decoder.ts | 27 ++++++++++++++++++-------- test/unit/resp/decoder.ts | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/lib/resp/decoder.ts b/lib/resp/decoder.ts index 15b847879..1110b37a7 100644 --- a/lib/resp/decoder.ts +++ b/lib/resp/decoder.ts @@ -662,7 +662,7 @@ export class Decoder { case RESP_TYPES.ATTRIBUTE: return this.#decodeAttribute( this.#decodeMap(typeMapping, chunk), - typeMapping, + this.#decodeNestedType.bind(this, typeMapping), chunk ); @@ -673,18 +673,19 @@ export class Decoder { } } - #decodeAttribute(attribute, typeMapping, chunk) { + // `decodeNext` decodes whatever the attribute decorates, and has to be the + // decoder for the position the attribute was found in: a map key still needs + // #decodeMapKey, which forces string keys, and not the generic nested path. + #decodeAttribute(attribute, decodeNext, chunk) { if (typeof attribute === "function") { - return this.#continueDecodeAttribute.bind(this, attribute, typeMapping); + return this.#continueDecodeAttribute.bind(this, attribute, decodeNext); } - return this.#cursor >= chunk.length - ? this.#decodeNestedType.bind(this, typeMapping) - : this.#decodeNestedType(typeMapping, chunk); + return this.#cursor >= chunk.length ? decodeNext : decodeNext(chunk); } - #continueDecodeAttribute(attributeCb, typeMapping, chunk) { - return this.#decodeAttribute(attributeCb(chunk), typeMapping, chunk); + #continueDecodeAttribute(attributeCb, decodeNext, chunk) { + return this.#decodeAttribute(attributeCb(chunk), decodeNext, chunk); } #decodeArray(typeMapping, chunk) { @@ -915,6 +916,16 @@ export class Decoder { case RESP_TYPES.BLOB_STRING: return this.#decodeBlobString(String, chunk); + // An attributed key is still a key: resume on the key path so it keeps + // being coerced to a string. Leaving it to the nested path would hand + // #decodeMapAsObject a Buffer key, which bypasses its `__proto__` guard. + case RESP_TYPES.ATTRIBUTE: + return this.#decodeAttribute( + this.#decodeMap(typeMapping, chunk), + this.#decodeMapKey.bind(this, typeMapping), + chunk + ); + default: return this.#decodeNestedTypeValue(type, typeMapping, chunk); } diff --git a/test/unit/resp/decoder.ts b/test/unit/resp/decoder.ts index 4f62c22ba..e4e820d42 100644 --- a/test/unit/resp/decoder.ts +++ b/test/unit/resp/decoder.ts @@ -604,6 +604,46 @@ describe("RESP Decoder", () => { replies: [{ key: 1 }], }); + // An attributed key has to stay on the map-key path, which forces keys to + // strings. A Buffer key would read back as `Buffer(foo)` from a Map and + // would walk straight past the `__proto__` guard in #decodeMapAsObject. + test("on a map key with blob strings as Buffer", { + typeMapping: { + [RESP_TYPES.SIMPLE_STRING]: Buffer, + [RESP_TYPES.BLOB_STRING]: Buffer, + }, + toWrite: Buffer.from("%1\r\n|1\r\n+a\r\n+b\r\n$3\r\nfoo\r\n:1\r\n"), + replies: [{ foo: 1 }], + }); + + test("on a map key as Map with blob strings as Buffer", { + typeMapping: { + [RESP_TYPES.MAP]: Map, + [RESP_TYPES.SIMPLE_STRING]: Buffer, + [RESP_TYPES.BLOB_STRING]: Buffer, + }, + toWrite: Buffer.from("%1\r\n|1\r\n+a\r\n+b\r\n$3\r\nfoo\r\n:1\r\n"), + replies: [new Map([["foo", 1]])], + }); + + test("on a __proto__ map key", { + typeMapping: { + [RESP_TYPES.SIMPLE_STRING]: Buffer, + [RESP_TYPES.BLOB_STRING]: Buffer, + }, + toWrite: Buffer.from( + "%1\r\n|1\r\n+a\r\n+b\r\n$9\r\n__proto__\r\n*1\r\n:1\r\n" + ), + replies: [ + Object.defineProperty({}, "__proto__", { + value: [1], + configurable: true, + enumerable: true, + writable: true, + }), + ], + }); + test("on a set element as Set", { typeMapping: { [RESP_TYPES.SET]: Set, From a9f096b1095bbf7029c166091d811e2409573866 Mon Sep 17 00:00:00 2001 From: Develop-KIM Date: Wed, 7 Oct 2026 11:23:32 +0900 Subject: [PATCH 3/3] test: keep one attribute case per decoder path Three cases cover what the patch touches - top level, nested, and the map-key path with Buffer mapping - and the decoder comments are down to one line per case. --- lib/resp/decoder.ts | 20 +++-------- test/unit/resp/decoder.ts | 74 --------------------------------------- 2 files changed, 5 insertions(+), 89 deletions(-) diff --git a/lib/resp/decoder.ts b/lib/resp/decoder.ts index 1110b37a7..377e58ebe 100644 --- a/lib/resp/decoder.ts +++ b/lib/resp/decoder.ts @@ -41,8 +41,7 @@ export const PUSH_TYPE_MAPPING = { [RESP_TYPES.BLOB_STRING]: Buffer, }; -// Attribute contents aren't surfaced to callers; this keeps the decoded map -// from being reported as a reply of its own. +// Attribute contents aren't surfaced to callers. function discardAttribute() {} // this was written with performance in mind, so it's not very readable... sorry :( @@ -201,11 +200,7 @@ export class Decoder { this.#decodeMap(this.getTypeMapping(), chunk) ); - // An attribute is metadata for the reply that follows it, not a reply of - // its own. The spec lets clients ignore the contents, but they still have - // to be consumed: leaving them in the stream turns the reply behind them - // into a protocol error. Dropping out of #decodeTypeValue without calling - // a callback resumes the main loop on the real reply. + // RESP3 attribute: consume and discard, then decode the reply it decorates case RESP_TYPES.ATTRIBUTE: return this.#handleDecodedValue( discardAttribute, @@ -657,8 +652,7 @@ export class Decoder { case RESP_TYPES.MAP: return this.#decodeMap(typeMapping, chunk); - // Nested attributes decorate the element that follows them, so the - // element takes their place in the array/set/map being decoded. + // RESP3 attribute: the element it decorates takes its place case RESP_TYPES.ATTRIBUTE: return this.#decodeAttribute( this.#decodeMap(typeMapping, chunk), @@ -673,9 +667,7 @@ export class Decoder { } } - // `decodeNext` decodes whatever the attribute decorates, and has to be the - // decoder for the position the attribute was found in: a map key still needs - // #decodeMapKey, which forces string keys, and not the generic nested path. + // `decodeNext` has to be the decoder for the position the attribute was found in. #decodeAttribute(attribute, decodeNext, chunk) { if (typeof attribute === "function") { return this.#continueDecodeAttribute.bind(this, attribute, decodeNext); @@ -916,9 +908,7 @@ export class Decoder { case RESP_TYPES.BLOB_STRING: return this.#decodeBlobString(String, chunk); - // An attributed key is still a key: resume on the key path so it keeps - // being coerced to a string. Leaving it to the nested path would hand - // #decodeMapAsObject a Buffer key, which bypasses its `__proto__` guard. + // RESP3 attribute: resume on the key path so the key is still forced to a string case RESP_TYPES.ATTRIBUTE: return this.#decodeAttribute( this.#decodeMap(typeMapping, chunk), diff --git a/test/unit/resp/decoder.ts b/test/unit/resp/decoder.ts index e4e820d42..210047b75 100644 --- a/test/unit/resp/decoder.ts +++ b/test/unit/resp/decoder.ts @@ -552,10 +552,6 @@ describe("RESP Decoder", () => { }); }); - // Attributes are metadata attached to the reply behind them. They carry no - // reply of their own, so nothing reaches onReply until the real reply is - // decoded - and a client that doesn't consume them reads that real reply as - // an unknown type and kills the connection. describe("Attribute", () => { test("DEBUG PROTOCOL attrib", { toWrite: Buffer.from( @@ -564,68 +560,11 @@ describe("RESP Decoder", () => { replies: ["Some real reply following the attribute"], }); - test("empty", { - toWrite: Buffer.from("|0\r\n+OK\r\n"), - replies: ["OK"], - }); - - test("two in a row", { - toWrite: Buffer.from("|1\r\n+a\r\n+b\r\n|1\r\n+c\r\n+d\r\n:1\r\n"), - replies: [1], - }); - - test("before an error", { - toWrite: Buffer.from("|1\r\n+a\r\n+b\r\n-ERROR\r\n"), - errorReplies: [new SimpleError("ERROR")], - }); - - test("before a push", { - toWrite: Buffer.from("|1\r\n+a\r\n+b\r\n>2\r\n:0\r\n:1\r\n"), - pushReplies: [[0, 1]], - }); - test("on an array element", { toWrite: Buffer.from("*2\r\n|1\r\n+a\r\n+b\r\n:1\r\n:2\r\n"), replies: [[1, 2]], }); - test("on a nested array element", { - toWrite: Buffer.from("*1\r\n*1\r\n|1\r\n+a\r\n+b\r\n:1\r\n"), - replies: [[[1]]], - }); - - test("on a map value", { - toWrite: Buffer.from("%1\r\n+key\r\n|1\r\n+a\r\n+b\r\n:1\r\n"), - replies: [{ key: 1 }], - }); - - test("on a map key", { - toWrite: Buffer.from("%1\r\n|1\r\n+a\r\n+b\r\n+key\r\n:1\r\n"), - replies: [{ key: 1 }], - }); - - // An attributed key has to stay on the map-key path, which forces keys to - // strings. A Buffer key would read back as `Buffer(foo)` from a Map and - // would walk straight past the `__proto__` guard in #decodeMapAsObject. - test("on a map key with blob strings as Buffer", { - typeMapping: { - [RESP_TYPES.SIMPLE_STRING]: Buffer, - [RESP_TYPES.BLOB_STRING]: Buffer, - }, - toWrite: Buffer.from("%1\r\n|1\r\n+a\r\n+b\r\n$3\r\nfoo\r\n:1\r\n"), - replies: [{ foo: 1 }], - }); - - test("on a map key as Map with blob strings as Buffer", { - typeMapping: { - [RESP_TYPES.MAP]: Map, - [RESP_TYPES.SIMPLE_STRING]: Buffer, - [RESP_TYPES.BLOB_STRING]: Buffer, - }, - toWrite: Buffer.from("%1\r\n|1\r\n+a\r\n+b\r\n$3\r\nfoo\r\n:1\r\n"), - replies: [new Map([["foo", 1]])], - }); - test("on a __proto__ map key", { typeMapping: { [RESP_TYPES.SIMPLE_STRING]: Buffer, @@ -643,19 +582,6 @@ describe("RESP Decoder", () => { }), ], }); - - test("on a set element as Set", { - typeMapping: { - [RESP_TYPES.SET]: Set, - }, - toWrite: Buffer.from("~1\r\n|1\r\n+a\r\n+b\r\n:1\r\n"), - replies: [new Set([1])], - }); - - test("with a map value of its own", { - toWrite: Buffer.from("|1\r\n+a\r\n%1\r\n+b\r\n:1\r\n:2\r\n"), - replies: [2], - }); }); describe("Push", () => {