From 464350d086965ed0ceab843601d6b1a7945046a1 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Tue, 15 Sep 2026 18:42:04 +0800 Subject: [PATCH] fix(grid): preview GBK-stored text in MySQL varbinary cells --- .../src/lib/dataGrid/binaryCellDownload.ts | 70 ++++++++++++++++--- packages/app-tests/binaryCellDownload.test.ts | 32 ++++++++- 2 files changed, 89 insertions(+), 13 deletions(-) diff --git a/apps/desktop/src/lib/dataGrid/binaryCellDownload.ts b/apps/desktop/src/lib/dataGrid/binaryCellDownload.ts index 3e122a19bd..93083d17fd 100644 --- a/apps/desktop/src/lib/dataGrid/binaryCellDownload.ts +++ b/apps/desktop/src/lib/dataGrid/binaryCellDownload.ts @@ -208,7 +208,7 @@ export function binaryCellDisplayText(value: unknown, columnType?: string, origi const bytes = parseBinaryCellBytes(value, columnType, databaseType); if (!bytes || !isBinaryCellColumnType(columnType)) return null; if (isBinaryCellTextPreviewColumn(columnType, databaseType)) { - const text = binaryCellUtf8TextBytes(bytes, columnType); + const text = binaryCellTextPreviewBytes(bytes, columnType, databaseType); if (text !== null) return text; } return `${binaryCellDisplayLabel(columnType)} [${formatBinaryCellByteSize(bytes.length)}]`; @@ -223,9 +223,11 @@ export function binaryCellUtf8Text(value: unknown, columnType?: string, database // 复制到剪贴板时,把「文本型」MySQL VARBINARY 单元格还原成其原始字符串。 // DBX 后端为保留任意 bytes,把该值统一序列化成 `0x`;前端只有在严格 UTF-8 解码、 -// 无控制字符且重新编码后与原 bytes 完全一致时,才把 payload(如 token)复制为文本。 -// 其余情况返回 null,让调用方沿用无损 hex。范围严格限定 MySQL VARBINARY,避免改变 -// MySQL BINARY/BLOB、SQL Server VARBINARY、PostgreSQL BYTEA、Oracle RAW 等现有语义。 +// 无控制字符且重新编码后与原 bytes 完全一致时,才把 payload(如 token)复制为文本; +// UTF-8 解码失败时再按网格预览同一闸门尝试 GBK,保持「显示成 GBK 文本的单元格复制时 +// 还原成原字符串」。其余情况返回 null,让调用方沿用无损 hex。范围严格限定 MySQL +// VARBINARY,避免改变 MySQL BINARY/BLOB、SQL Server VARBINARY、PostgreSQL BYTEA、 +// Oracle RAW 等现有语义。 export function binaryCellClipboardText(value: unknown, columnType?: string, databaseType?: DatabaseType): string | null { if (databaseType !== "mysql" || !VARBINARY_TYPE_RE.test((columnType ?? "").trim())) return null; // MySQL QueryResult 的 binary canonical form 是 `0x`;不要把普通的偶数长度 @@ -233,9 +235,13 @@ export function binaryCellClipboardText(value: unknown, columnType?: string, dat const bytes = typeof value === "string" ? parseBinaryCellHexValue(value) : null; if (!bytes) return null; const text = binaryCellUtf8TextBytes(bytes, columnType); - if (text === null) return null; - const encoded = new TextEncoder().encode(text); - return encoded.length === bytes.length && encoded.every((byte, index) => byte === bytes[index]) ? text : null; + if (text !== null) { + const encoded = new TextEncoder().encode(text); + // UTF-8 可解码但无法 byte-for-byte 回编码(如解码会吞掉的 BOM)时保持 null, + // 与 GBK 回退无关,沿用 hex。 + return encoded.length === bytes.length && encoded.every((byte, index) => byte === bytes[index]) ? text : null; + } + return printableGbkText(binaryPreviewBytes(bytes, columnType)); } // MySQL BLOB 的文本预览必须与编辑写回路径(coerceMysqlBlobTextValue,仅 mysql)走同一闸门: @@ -247,9 +253,26 @@ function isBinaryCellTextPreviewColumn(columnType: string | undefined, databaseT return isBlobCellColumnType(columnType) && databaseType === "mysql"; } +// 显示/复制路径的解码链:先严格 UTF-8,仅 MySQL 连接的 binary/varbinary 再回退严格 +// GBK——中文存量库里 varbinary 常见 GBK 写入(Navicat 等工具按连接字符集直接显示), +// UTF-8 优先保证 ASCII/UTF-8 数据永远不受影响。两处刻意不覆盖: +// 1) 编辑写回路径(dataGridCellEditorText/coerceMysqlBlobTextValue)保持纯 UTF-8, +// 编辑保存会把文本重新按 UTF-8 编码,放 GBK 文本进去会静默翻转该单元格的字节编码; +// 2) MySQL BLOB 列,其文本预览与编辑路径共享同一闸门(见 isBinaryCellTextPreviewColumn), +// GBK 回退会把该闸门凿开。varbinary 的「显示文本、编辑 hex」不对称是既有行为,不受影响。 +function binaryCellTextPreviewBytes(bytes: Uint8Array, columnType?: string, databaseType?: DatabaseType): string | null { + const utf8Text = binaryCellUtf8TextBytes(bytes, columnType); + if (utf8Text !== null) return utf8Text; + if (databaseType !== "mysql" || isBlobCellColumnType(columnType)) return null; + return printableGbkText(binaryPreviewBytes(bytes, columnType)); +} + function binaryCellUtf8TextBytes(bytes: Uint8Array, columnType?: string): string | null { - const previewBytes = FIXED_BINARY_TYPE_RE.test((columnType ?? "").trim()) ? trimTrailingNullBytes(bytes) : bytes; - return printableUtf8Text(previewBytes); + return printableUtf8Text(binaryPreviewBytes(bytes, columnType)); +} + +function binaryPreviewBytes(bytes: Uint8Array, columnType?: string): Uint8Array { + return FIXED_BINARY_TYPE_RE.test((columnType ?? "").trim()) ? trimTrailingNullBytes(bytes) : bytes; } function trimTrailingNullBytes(bytes: Uint8Array): Uint8Array { @@ -265,12 +288,37 @@ function printableUtf8Text(bytes: Uint8Array): string | null { } catch { return null; } + return printableText(text) ? text : null; +} + +// GBK 回退的解码闸门:fatal 解码拒绝非法序列,可打印过滤剔除控制字符,另加拒绝 +// Unicode 私用区(真实文本不含 PUA,而 0xFF 等字节的高段 GBK 序列常落在 PUA,是随机 +// 二进制的强信号)。TextEncoder 只有 UTF-8,GBK 没有 encode 侧、做不了 UTF-8 分支 +// 那样的往返校验;但 fatal 解码对合法序列是双射,随机二进制仍可能恰好全部组成合法 +// GBK 序列(如 0xdeadbeef → 两个 URO 汉字),此时显示/复制与网格预览一致地输出解码 +// 文本,原始 bytes 无损保留在 0x 值中。 +function printableGbkText(bytes: Uint8Array): string | null { + let text: string; + try { + text = new TextDecoder("gbk", { fatal: true }).decode(bytes); + } catch { + return null; + } + if (!printableText(text)) return null; + for (const char of text) { + const codePoint = char.codePointAt(0) ?? 0; + if (codePoint >= 0xe000 && codePoint <= 0xf8ff) return null; + } + return text; +} + +function printableText(text: string): boolean { for (const char of text) { const codePoint = char.codePointAt(0) ?? 0; const allowedWhitespace = codePoint === 9 || codePoint === 10 || codePoint === 13; - if (!allowedWhitespace && (codePoint <= 31 || (codePoint >= 127 && codePoint <= 159))) return null; + if (!allowedWhitespace && (codePoint <= 31 || (codePoint >= 127 && codePoint <= 159))) return false; } - return text; + return true; } function binaryCellDisplayLabel(columnType?: string): string { diff --git a/packages/app-tests/binaryCellDownload.test.ts b/packages/app-tests/binaryCellDownload.test.ts index 289902356b..ea5b322541 100644 --- a/packages/app-tests/binaryCellDownload.test.ts +++ b/packages/app-tests/binaryCellDownload.test.ts @@ -108,6 +108,33 @@ test("binaryCellUtf8Text only returns strict printable text", () => { assert.equal(binaryCellUtf8Text("0x4869", "varchar", "mysql"), null); }); +// 群反馈:MySQL varbinary 里以 GBK 写入的中文(Navicat 按连接字符集直接显示)。 +// UTF-8 严格解码失败后,仅 MySQL 的 binary/varbinary 在显示/复制路径回退严格 GBK; +// BLOB 与编辑写回路径保持纯 UTF-8(与 coerceMysqlBlobTextValue 同闸门)。 +test("MySQL varbinary text preview falls back to strict GBK after UTF-8", () => { + // "2026年5月22日 星期五 9:30" 的 GBK 编码(26 字节;UTF-8 编码为 33 字节)。 + const gbkHex = "0x32303236c4ea35d4c23232c8d520d0c7c6dacee52039a3ba3330"; + assert.equal(binaryCellDisplayText(gbkHex, "VARBINARY(255)", undefined, "mysql"), "2026年5月22日 星期五 9:30"); + assert.equal(binaryCellClipboardText(gbkHex, "VARBINARY(255)", "mysql"), "2026年5月22日 星期五 9:30"); + assert.equal(binaryCellDisplayText("0xd6d0cec4", "VARBINARY(255)", undefined, "mysql"), "中文"); + assert.equal(binaryCellClipboardText("0xd6d0cec4", "VARBINARY(128)", "mysql"), "中文"); + // UTF-8 优先:两种编码都能表达时结果一致。 + assert.equal(binaryCellDisplayText("0xe4b8ade69687", "VARBINARY(128)", undefined, "mysql"), "中文"); + + // GBK 回退仅限 MySQL 连接。 + assert.equal(binaryCellDisplayText("0xd6d0cec4", "VARBINARY(255)", undefined, "sqlserver"), "VARBINARY [4 bytes]"); + assert.equal(binaryCellDisplayText("0xd6d0cec4", "VARBINARY(255)", undefined, undefined), "VARBINARY [4 bytes]"); + + // MySQL BLOB 不参与 GBK 回退(保持与编辑路径的显示/编辑一致性)。 + assert.equal(binaryCellDisplayText("0xd6d0cec4", "LONGBLOB", undefined, "mysql"), "BLOB [4 bytes]"); + assert.equal(binaryCellUtf8Text("0xd6d0cec4", "LONGBLOB", "mysql"), null); + + // 非法 GBK 序列、以及解码落在 Unicode 私用区的(真实文本不含 PUA)仍回退标签 / 保持 hex。 + assert.equal(binaryCellDisplayText("0xfffe", "VARBINARY(2)", undefined, "mysql"), "VARBINARY [2 bytes]"); + assert.equal(binaryCellDisplayText("0xffff", "VARBINARY(2)", undefined, "mysql"), "VARBINARY [2 bytes]"); + assert.equal(binaryCellClipboardText("0xffff", "VARBINARY(2)", "mysql"), null); +}); + // issue #7471:MySQL VARBINARY 的文本 payload 复制为原始字符串,任意二进制保持 0x/hex 无损。 test("binaryCellClipboardText decodes textual MySQL varbinary and preserves arbitrary bytes", () => { // Case 1: ASCII VARBINARY(issue 示例 abc → 0x616263)。 @@ -125,8 +152,9 @@ test("binaryCellClipboardText decodes textual MySQL varbinary and preserves arbi assert.equal(binaryCellClipboardText("0x", "VARBINARY(0)", "mysql"), ""); assert.equal(binaryCellClipboardText("0x68690a", "VARBINARY(3)", "mysql"), "hi\n"); - // Case 5: arbitrary binary(非法 UTF-8 / 含 NUL / 含控制字符)保持 null → 复制端沿用 0x/hex,绝不产生 � 或丢字节。 - assert.equal(binaryCellClipboardText("0xdeadbeef", "VARBINARY(4)", "mysql"), null); + // Case 5: 无法解码的 arbitrary binary(含 NUL / 含控制字符 / 非法序列)保持 null → 复制端沿用 0x/hex,绝不产生 � 或丢字节。 + // 0xdeadbeef 例外:恰好全部组成合法 GBK 序列,随 GBK 回退按解码文本复制(与网格显示一致)。 + assert.equal(binaryCellClipboardText("0xdeadbeef", "VARBINARY(4)", "mysql"), "蕲撅"); assert.equal(binaryCellClipboardText("0xfffe", "VARBINARY(2)", "mysql"), null); assert.equal(binaryCellClipboardText("0x0061", "VARBINARY(2)", "mysql"), null); // 含 NUL assert.equal(binaryCellClipboardText("0x0102", "VARBINARY(2)", "mysql"), null); // 控制字符