fix(mysql): add TLS failure hint

This commit is contained in:
zipg authored and GitHub committed 2026-07-06 10:47:57 +08:00
1 parent 38f9893425
commit 95397f7152
11 files changed
+106 -1

No files matched your search

@@ -0,0 +1,48 @@
import { describe, expect, it } from "vitest";
import { appendConnectionErrorHints } from "@/lib/connection/connectionErrorHints";
import type { ConnectionConfig } from "@/types/database";
function mysqlConfig(urlParams: string | undefined): ConnectionConfig {
return {
id: "mysql-test",
name: "MySQL",
db_type: "mysql",
host: "127.0.0.1",
port: 3306,
username: "root",
password: "",
database: undefined,
url_params: urlParams,
ssl: false,
};
}
const t = (key: string) => (key === "connection.mysqlTlsConnectionFailureHint" ? "Set TLS Mode to Disabled." : key);
describe("appendConnectionErrorHints", () => {
it("adds a MySQL TLS hint for non-disabled TLS failures", () => {
const message = appendConnectionErrorHints(mysqlConfig("ssl-mode=preferred"), "MySQL connection failed: TLS handshake failed", t);
expect(message).toContain("TLS handshake failed");
expect(message).toContain("Set TLS Mode to Disabled.");
});
it("adds the TLS hint for camel-case MySQL sslMode params", () => {
const message = appendConnectionErrorHints(mysqlConfig("sslMode=REQUIRED"), "MySQL connection failed: Driver error: `Client asked for SSL but server does not have this capability'", t);
expect(message).toContain("server does not have this capability");
expect(message).toContain("Set TLS Mode to Disabled.");
});
it("does not add the TLS hint when MySQL TLS is disabled", () => {
const message = appendConnectionErrorHints(mysqlConfig("ssl-mode=disabled"), "MySQL connection failed: TLS handshake failed", t);
expect(message).toBe("MySQL connection failed: TLS handshake failed");
});
it("does not add the TLS hint for non-TLS errors", () => {
const message = appendConnectionErrorHints(mysqlConfig("ssl-mode=preferred"), "Access denied for user root", t);
expect(message).toBe("Access denied for user root");
});
});
@@ -0,0 +1,46 @@
import type { ConnectionConfig } from "@/types/database";
type Translate = (key: string) => string;
function normalizeUrlParams(params: string | undefined): URLSearchParams {
return new URLSearchParams((params || "").trim().replace(/^\?/, ""));
}
function normalizeUrlParamKey(key: string): string {
return key.trim().toLowerCase().replace(/[-_]/g, "");
}
function urlParamValue(params: URLSearchParams, key: string): string {
const normalizedKey = normalizeUrlParamKey(key);
for (const [paramKey, value] of params.entries()) {
if (normalizeUrlParamKey(paramKey) === normalizedKey) return value;
}
return "";
}
function mysqlTlsMode(config: ConnectionConfig): string {
const parsed = normalizeUrlParams(config.url_params);
// MySQL clients use ssl-mode, sslmode and sslMode spellings; keep hint behavior aligned with backend parsing.
const mode = urlParamValue(parsed, "ssl-mode").trim().toLowerCase().replace("-", "_");
if (["disabled", "disable"].includes(mode)) return "disabled";
if (["preferred", "prefer"].includes(mode)) return "preferred";
if (["required", "require", "verify_ca", "verify_identity"].includes(mode)) return mode;
if (config.ssl || ["true", "1", "yes", "on"].includes(urlParamValue(parsed, "require_ssl").trim().toLowerCase())) return "required";
return "disabled";
}
function isMysqlTlsLikeFailure(message: string): boolean {
const text = message.toLowerCase();
return (
(text.includes("mysql") || text.includes("mariadb") || text.includes("tidb") || text.includes("tls") || text.includes("ssl")) &&
(text.includes("tls") || text.includes("ssl") || text.includes("handshake") || text.includes("certificate") || text.includes("cert") || text.includes("unknown ca") || text.includes("self signed"))
);
}
export function appendConnectionErrorHints(config: ConnectionConfig | undefined, message: string, t: Translate): string {
if (!config || config.db_type !== "mysql") return message;
if (mysqlTlsMode(config) === "disabled") return message;
if (!isMysqlTlsLikeFailure(message)) return message;
const hint = t("connection.mysqlTlsConnectionFailureHint");
return message.includes(hint) ? message : `${message}\n\n${hint}`;
}