fix(import): decode connection import files beyond UTF-8

This commit is contained in:
Johan authored and GitHub committed 2026-09-30 15:16:47 +08:00
1 parent be82fdc989
commit b32ef45da1
3 files changed
+112 -6

No files matched your search

@@ -0,0 +1,58 @@
import { describe, expect, it } from "vitest";
import { decodeImportFileText } from "@/lib/common/decodeText";
function utf16Bytes(text: string, littleEndian: boolean, bom: boolean): ArrayBuffer {
const out: number[] = [];
if (bom) out.push(...(littleEndian ? [0xff, 0xfe] : [0xfe, 0xff]));
for (const char of text) {
const code = char.charCodeAt(0);
if (littleEndian) out.push(code & 0xff, (code >> 8) & 0xff);
else out.push((code >> 8) & 0xff, code & 0xff);
}
return new Uint8Array(out).buffer as ArrayBuffer;
}
function utf8Bytes(text: string, bom: boolean): ArrayBuffer {
const encoded = new TextEncoder().encode(text);
if (!bom) return encoded.buffer as ArrayBuffer;
const out = new Uint8Array(encoded.length + 3);
out.set([0xef, 0xbb, 0xbf], 0);
out.set(encoded, 3);
return out.buffer as ArrayBuffer;
}
const xml = `<Connections><Connection ConnType="MYSQL" Name="local" Host="127.0.0.1" Port="3306" /></Connections>`;
describe("decodeImportFileText", () => {
it("decodes plain UTF-8 unchanged", () => {
expect(decodeImportFileText(utf8Bytes(xml, false))).toBe(xml);
});
it("strips a UTF-8 BOM", () => {
expect(decodeImportFileText(utf8Bytes(xml, true))).toBe(xml);
});
it("decodes UTF-16LE with BOM (#10666 Navicat 17 macOS export)", () => {
expect(decodeImportFileText(utf16Bytes(xml, true, true))).toBe(xml);
});
it("decodes UTF-16BE with BOM", () => {
expect(decodeImportFileText(utf16Bytes(xml, false, true))).toBe(xml);
});
it("decodes BOM-less UTF-16LE via the null-byte heuristic", () => {
expect(decodeImportFileText(utf16Bytes(xml, true, false))).toBe(xml);
});
it("decodes BOM-less UTF-16BE via the null-byte heuristic", () => {
expect(decodeImportFileText(utf16Bytes(xml, false, false))).toBe(xml);
});
it("decodes an empty file to an empty string", () => {
expect(decodeImportFileText(new ArrayBuffer(0))).toBe("");
});
it("accepts Uint8Array input (Tauri readFile shape)", () => {
expect(decodeImportFileText(new Uint8Array(utf8Bytes(xml, false)))).toBe(xml);
});
});
+44
View File
@@ -0,0 +1,44 @@
/**
* Decode an imported connection file's bytes to text.
*
* Navicat 17 exports on macOS can arrive as UTF-16 (with or without BOM)
* while the import path historically decoded everything as UTF-8, which
* turned the XML into mojibake and surfaced as "Invalid Navicat connection
* file" (#10666). Sniff the BOM first, then fall back to a null-byte
* heuristic for BOM-less UTF-16; plain UTF-8 (including JSON payloads)
* decodes exactly as before.
*/
export function decodeImportFileText(buffer: ArrayBuffer | Uint8Array): string {
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
if (bytes.length >= 3 && bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf) {
return stripBom(new TextDecoder("utf-8").decode(bytes.subarray(3)));
}
if (bytes.length >= 2 && bytes[0] === 0xff && bytes[1] === 0xfe) {
return stripBom(new TextDecoder("utf-16le").decode(bytes.subarray(2)));
}
if (bytes.length >= 2 && bytes[0] === 0xfe && bytes[1] === 0xff) {
return stripBom(new TextDecoder("utf-16be").decode(bytes.subarray(2)));
}
// BOM-less UTF-16: ASCII-range text leaves a null in every other byte.
// `<C` is `<\0C\0` in LE (nulls on odd indices) and `\0<\0C` in BE.
const sample = bytes.subarray(0, 64);
let evenNulls = 0;
let oddNulls = 0;
for (let i = 0; i < sample.length; i++) {
if (sample[i] === 0) {
if (i % 2 === 0) evenNulls++;
else oddNulls++;
}
}
if (oddNulls > 0 && oddNulls >= evenNulls) {
return stripBom(new TextDecoder("utf-16le").decode(bytes));
}
if (evenNulls > 0 && evenNulls > oddNulls) {
return stripBom(new TextDecoder("utf-16be").decode(bytes));
}
return stripBom(new TextDecoder("utf-8").decode(bytes));
}
function stripBom(text: string): string {
return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
}
+10 -6
View File
@@ -9870,14 +9870,18 @@ export const useConnectionStore = defineStore("connection", () => {
if (isTauriRuntime()) { if (isTauriRuntime()) {
const { open } = await import("@tauri-apps/plugin-dialog"); const { open } = await import("@tauri-apps/plugin-dialog");
const { readTextFile } = await import("@tauri-apps/plugin-fs"); const { readFile } = await import("@tauri-apps/plugin-fs");
const { decodeImportFileText } = await import("@/lib/common/decodeText");
const path = await open({ const path = await open({
filters: source === "navicat" ? [{ name: "Navicat Connection Export", extensions: ["ncx", "xml"] }] : [{ name: "DBX JSON", extensions: ["json"] }], filters: source === "navicat" ? [{ name: "Navicat Connection Export", extensions: ["ncx", "xml"] }] : [{ name: "DBX JSON", extensions: ["json"] }],
multiple: false, multiple: false,
}); });
if (!path) return null; if (!path) return null;
content = await readTextFile(path as string); // Raw bytes: Navicat 17 macOS exports can be UTF-16 (#10666), which
// readTextFile would reject as invalid UTF-8.
content = decodeImportFileText(await readFile(path as string));
} else { } else {
const { decodeImportFileText } = await import("@/lib/common/decodeText");
content = await new Promise<string>((resolve, reject) => { content = await new Promise<string>((resolve, reject) => {
const input = document.createElement("input"); const input = document.createElement("input");
input.type = "file"; input.type = "file";
@@ -9888,10 +9892,10 @@ export const useConnectionStore = defineStore("connection", () => {
reject(new Error("No file selected")); reject(new Error("No file selected"));
return; return;
} }
const reader = new FileReader(); file
reader.onload = () => resolve(reader.result as string); .arrayBuffer()
reader.onerror = () => reject(reader.error); .then((buffer) => resolve(decodeImportFileText(buffer)))
reader.readAsText(file); .catch(reject);
}; };
input.click(); input.click();
}); });