diff --git a/apps/desktop/src/lib/__tests__/imports/navicatImport.spec.ts b/apps/desktop/src/lib/__tests__/imports/navicatImport.spec.ts index e2729ff3b5..309fa172b1 100644 --- a/apps/desktop/src/lib/__tests__/imports/navicatImport.spec.ts +++ b/apps/desktop/src/lib/__tests__/imports/navicatImport.spec.ts @@ -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(` @@ -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(` + +`); + + 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(` diff --git a/apps/desktop/src/lib/imports/navicatImport.ts b/apps/desktop/src/lib/imports/navicatImport.ts index b2b50c44c3..05554cf2b0 100644 --- a/apps/desktop/src/lib/imports/navicatImport.ts +++ b/apps/desktop/src/lib/imports/navicatImport.ts @@ -44,6 +44,7 @@ const typeMap: Record | 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, key: Uint8Array, iv: Uint8Array) { + 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, key: Uint8Array, iv: Uint8Array) { + 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; diff --git a/package.json b/package.json index 2b492032ad..981c0f4956 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "@lezer/highlight": "^1.2.3", "@lucide/vue": "1.17.0", "@msgpack/msgpack": "^3.1.3", + "@noble/ciphers": "2.2.0", "@replit/codemirror-vim": "^6.3.0", "@tauri-apps/api": "^2.11.0", "@tauri-apps/plugin-clipboard-manager": "^2.3.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 04da636603..fa55647e68 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -65,6 +65,9 @@ importers: '@msgpack/msgpack': specifier: ^3.1.3 version: 3.1.3 + '@noble/ciphers': + specifier: 2.2.0 + version: 2.2.0 '@replit/codemirror-vim': specifier: ^6.3.0 version: 6.3.0(@codemirror/commands@6.10.3)(@codemirror/language@6.12.3)(@codemirror/search@6.7.0)(@codemirror/state@6.6.0)(@codemirror/view@6.43.0) @@ -912,6 +915,10 @@ packages: resolution: {integrity: sha512-47XIizs9XZXvuJgoaJUIE2lFoID8ugvc0jzSHP+Ptfk8nTbnR8g788wv48N03Kx0UkAv559HWRQ3yzOgzlRNUA==} engines: {node: '>= 18'} + '@noble/ciphers@2.2.0': + resolution: {integrity: sha512-Z6pjIZ/8IJcCGzb2S/0Px5J81yij85xASuk1teLNeg75bfT07MV3a/O2Mtn1I2se43k3lkVEcFaR10N4cgQcZA==} + engines: {node: '>= 20.19.0'} + '@napi-rs/wasm-runtime@1.1.4': resolution: {integrity: sha512-3NQNNgA1YSlJb/kMH1ildASP9HW7/7kYnRI2szWJaofaS1hWmbGI4H+d3+22aGzXXN9IJ+n+GiFVcGipJP18ow==} peerDependencies: @@ -4529,6 +4536,8 @@ snapshots: '@msgpack/msgpack@3.1.3': {} + '@noble/ciphers@2.2.0': {} + '@napi-rs/wasm-runtime@1.1.4(@emnapi/core@1.10.0)(@emnapi/runtime@1.10.0)': dependencies: '@emnapi/core': 1.10.0