From 56242e3d65f016a91f65171056a4ebb9c5987e3f Mon Sep 17 00:00:00 2001 From: Afloat Date: Wed, 11 Nov 2026 19:01:15 +0800 Subject: [PATCH] fix(storage): match KV key prefixes literally --- src/storage/mysql.ts | 4 ++-- src/storage/store.ts | 4 ++-- tests/storage-backend-contract.test.ts | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index b3005bb5f4..4950d3475c 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -2173,8 +2173,8 @@ export class MysqlBackend implements StorageBackend { args.push(namespace); } if (keyPrefix !== undefined && keyPrefix !== "") { - where.push("kv_key LIKE CONCAT(?, '%')"); - args.push(keyPrefix.replace(/[\\%_]/g, (ch) => "\\" + ch)); + where.push("kv_key LIKE CONCAT(?, '%') AND LOCATE(CAST(? AS BINARY), CAST(kv_key AS BINARY)) = 1"); + args.push(keyPrefix.replace(/[\\%_]/g, (ch) => "\\" + ch), keyPrefix); } const sql = "SELECT namespace, kv_key, value_json, updated_at FROM kv_store" + diff --git a/src/storage/store.ts b/src/storage/store.ts index 97c674f438..7847750795 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -2567,8 +2567,8 @@ export class UnifiedStore { } if (keyPrefix !== undefined && keyPrefix !== "") { validateExtKey(keyPrefix, "key prefix"); - where.push("key LIKE ? ESCAPE '\\'"); - args.push(likeEscape(keyPrefix) + "%"); + where.push("key LIKE ? ESCAPE '\\' AND instr(key, ?) = 1"); + args.push(likeEscape(keyPrefix) + "%", keyPrefix); } const sql = "SELECT namespace, key, value_json, updated_at FROM kv_store" + diff --git a/tests/storage-backend-contract.test.ts b/tests/storage-backend-contract.test.ts index 9ce9c24e24..6fd514a0d0 100644 --- a/tests/storage-backend-contract.test.ts +++ b/tests/storage-backend-contract.test.ts @@ -78,6 +78,31 @@ test("registry: sqlite resolves through the registry; names are advertised", asy } }); +for (const lane of lanes) { + test(`${lane.name}: KV prefixes match literal keys without case or accent folding`, async () => { + const dir = scratch(); + const backend = await lane.open(dir); + const namespace = `kv-prefix-${lane.name}-${process.pid}-${Date.now()}`; + try { + for (const key of ["Prefix:one", "prefix:two", "café:one", "cafe:two", "a%_\\[literal]:one", "aXYZliteral:two"]) { + await backend.setKv(namespace, key, { key }); + } + const keys = async (prefix: string) => (await backend.listKv(namespace, prefix)).map((r) => r.key); + assert.deepEqual(await keys("Prefix:"), ["Prefix:one"]); + assert.deepEqual(await keys("prefix:"), ["prefix:two"]); + assert.deepEqual(await keys("café:"), ["café:one"]); + assert.deepEqual(await keys("cafe:"), ["cafe:two"]); + assert.deepEqual(await keys("a%_\\[literal]:"), ["a%_\\[literal]:one"]); + assert.deepEqual(await keys("missing:"), []); + assert.equal((await keys("")).length, 6); + } finally { + await backend.deleteKv(namespace); + await backend.close(); + fs.rmSync(dir, { recursive: true, force: true }); + } + }); +} + test("contract: ingest → reads round-trip through the interface", async () => { const dir = scratch(); try {