diff --git a/lib/utils/calculateSlot.ts b/lib/utils/calculateSlot.ts index c0b159136..e35c48ebb 100644 --- a/lib/utils/calculateSlot.ts +++ b/lib/utils/calculateSlot.ts @@ -114,6 +114,15 @@ function toUTF8Array(str: string): number[] { utf8[p++] = ((char >> 6) & 63) | 128; utf8[p++] = (char & 63) | 128; } else { + // A lone surrogate has no UTF-8 encoding, so Node writes it as U+FFFD + // (EF BF BD), both to a socket and in `Buffer.from(str)`. Encoding it + // the same way hashes the bytes the server receives. Node's UTF-8 write + // passes V8 `kReplaceInvalidUtf8`, which replaces orphan surrogates: + // https://github.com/nodejs/node/blob/59e46cac2943/src/string_bytes.cc#L305-L316 + // https://github.com/nodejs/node/blob/59e46cac2943/deps/v8/include/v8-primitive.h#L222-L225 + if ((char & 0xf800) === 0xd800) { + char = 0xfffd; + } utf8[p++] = (char >> 12) | 224; utf8[p++] = ((char >> 6) & 63) | 128; utf8[p++] = (char & 63) | 128; diff --git a/test/unit/calculateSlot.ts b/test/unit/calculateSlot.ts index 75aa39b5f..61af50db9 100644 --- a/test/unit/calculateSlot.ts +++ b/test/unit/calculateSlot.ts @@ -38,6 +38,34 @@ describe("calculateSlot", () => { }); }); + describe("lone UTF-16 surrogates", () => { + // Node writes string keys to the socket as UTF-8, replacing any lone + // surrogate with U+FFFD (EF BF BD). The slot must be computed from those + // same bytes, or the server answers with MOVED on every command. + const keys: Array<[string, string]> = [ + ["a lone high surrogate", "x\ud800y"], + ["a lone low surrogate", "x\udc00"], + ["a high surrogate at the end", "abc\ud83d"], + ["a low surrogate followed by a high one", "a\udc00\ud800b"], + ]; + + for (const [name, key] of keys) { + it(`hashes ${name} as the UTF-8 bytes Node sends`, () => { + expect(calculateSlot(key)).to.eql(calculateSlot(Buffer.from(key))); + }); + } + + it("matches the server's slot for a lone low surrogate", () => { + // Measured with `CLUSTER KEYSLOT` against a live Redis 8 server. + expect(calculateSlot("x\udc00")).to.eql(13299); + }); + + it("still hashes a valid surrogate pair as one code point", () => { + const key = "x\ud83d\ude00"; + expect(calculateSlot(key)).to.eql(calculateSlot(Buffer.from(key))); + }); + }); + describe("generateMulti()", () => { it("returns the shared slot when all keys hash to the same slot", () => { // "a{}b}c" and "42703" both land on slot 2041 under the corrected