fix(import): decrypt Navicat passwords over HTTP

This commit is contained in:
t8y2 committed 2026-08-01 20:28:36 +08:00
1 parent 44e4050852
commit 9c95f0af34
4 files changed
+39 -3

No files matched your search

@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { parseNavicatConnections } from "@/lib/imports/navicatImport";
class TestElement {
@@ -75,6 +75,10 @@ if (!globalThis.DOMParser) {
globalThis.DOMParser = TestDOMParser as typeof DOMParser;
}
afterEach(() => {
vi.unstubAllGlobals();
});
describe("parseNavicatConnections", () => {
it("imports SQLite DatabaseFile as the host without treating it as a schema", async () => {
const [connection] = await parseNavicatConnections(`<Connections>
@@ -155,6 +159,17 @@ describe("parseNavicatConnections", () => {
]);
});
it("decrypts Navicat passwords without SubtleCrypto in insecure HTTP contexts", async () => {
const databasePassword = await encryptNavicatPassword("database-secret");
vi.stubGlobal("crypto", {});
const [connection] = await parseNavicatConnections(`<Connections>
<Connection ConnType="ORACLE" ConnectionName="oracle-http" Host="db.internal" UserName="app" Password="${databasePassword}" />
</Connections>`);
expect(connection?.password).toBe("database-secret");
});
it("imports key-authenticated SSH field variants with the default port", async () => {
const keyPassphrase = await encryptNavicatPassword("key-secret");
const [connection] = await parseNavicatConnections(`<Connections>
+13 -2
View File
@@ -44,6 +44,7 @@ const typeMap: Record<string, { dbType: DatabaseType; profile: string; label: st
};
const unsupportedTypes = new Set(["http", "https", "ftp", "sftp", "ssh"]);
let navicatCipherModule: Promise<typeof import("@noble/ciphers/aes.js")> | undefined;
function normalizeKey(value: string) {
return value.toLowerCase().replace(/[^a-z0-9]/g, "");
@@ -96,14 +97,24 @@ async function decryptNavicatPassword(value: string) {
const key = new TextEncoder().encode("libcckeylibcckey");
const iv = new TextEncoder().encode("libcciv libcciv ");
try {
const cryptoKey = await crypto.subtle.importKey("raw", key, { name: "AES-CBC" }, false, ["decrypt"]);
const decrypted = new Uint8Array(await crypto.subtle.decrypt({ name: "AES-CBC", iv }, cryptoKey, encrypted));
const subtle = globalThis.crypto?.subtle;
const decrypted = subtle ? await decryptNavicatPasswordWithWebCrypto(subtle, encrypted, key, iv) : await decryptNavicatPasswordWithoutWebCrypto(encrypted, key, iv);
return new TextDecoder().decode(stripPkcs7(decrypted));
} catch {
return "";
}
}
async function decryptNavicatPasswordWithWebCrypto(subtle: SubtleCrypto, encrypted: Uint8Array<ArrayBuffer>, key: Uint8Array<ArrayBuffer>, iv: Uint8Array<ArrayBuffer>) {
const cryptoKey = await subtle.importKey("raw", key, { name: "AES-CBC" }, false, ["decrypt"]);
return new Uint8Array(await subtle.decrypt({ name: "AES-CBC", iv }, cryptoKey, encrypted));
}
async function decryptNavicatPasswordWithoutWebCrypto(encrypted: Uint8Array<ArrayBuffer>, key: Uint8Array<ArrayBuffer>, iv: Uint8Array<ArrayBuffer>) {
const { cbc } = await (navicatCipherModule ??= import("@noble/ciphers/aes.js"));
return cbc(key, iv).decrypt(encrypted);
}
function parseNavicatPort(value: string, fallback: number) {
const port = Number(value);
return Number.isInteger(port) && port > 0 && port <= 65535 ? port : fallback;