fix(mysql): preserve JDBC TLS parameter semantics

This commit is contained in:
zipg authored and GitHub committed 2026-08-21 19:57:22 +08:00
1 parent 8671a2fb2a
commit 7d5cae6486
17 files changed
+428 -23

No files matched your search

@@ -33,6 +33,7 @@ function jdbcConfig(): ConnectionConfig {
const t = (key: string) => {
if (key === "connection.mysqlTlsConnectionFailureHint") return "Set TLS Mode to Disabled.";
if (key === "connection.mysqlUnsupportedCertVersionHint") return "Replace the certificate or use Required mode.";
if (key === "connection.mysqlMissingPasswordHint") return "No database password was sent.";
if (key === "connection.jdbcMissingRuntimeDependencyHint") return "Install from Maven or import every dependency JAR.";
return key;
@@ -53,6 +54,52 @@ describe("appendConnectionErrorHints", () => {
expect(message).toContain("Set TLS Mode to Disabled.");
});
it("recognizes Connector/J TLS aliases", () => {
const message = appendConnectionErrorHints(mysqlConfig("useSSL=true&requireSSL=true&verifyServerCertificate=true"), "MySQL connection failed: TLS handshake failed", t);
expect(message).toContain("Set TLS Mode to Disabled.");
});
it("treats verifyServerCertificate as verified TLS unless useSSL is disabled", () => {
const verified = appendConnectionErrorHints(mysqlConfig("verifyServerCertificate=true"), "MySQL connection failed: TLS handshake failed", t);
const disabled = appendConnectionErrorHints(mysqlConfig("useSSL=false&requireSSL=true&verifyServerCertificate=true"), "MySQL connection failed: TLS handshake failed", t);
expect(verified).toContain("Set TLS Mode to Disabled.");
expect(disabled).toBe("MySQL connection failed: TLS handshake failed");
});
it("uses the last duplicate Connector/J TLS parameter", () => {
const enabled = appendConnectionErrorHints(mysqlConfig("useSSL=false&useSSL=true"), "MySQL connection failed: TLS handshake failed", t);
const disabled = appendConnectionErrorHints(mysqlConfig("useSSL=true&useSSL=false"), "MySQL connection failed: TLS handshake failed", t);
expect(enabled).toContain("Set TLS Mode to Disabled.");
expect(disabled).toBe("MySQL connection failed: TLS handshake failed");
});
it("prefers native require_ssl over Connector/J TLS aliases", () => {
const disabled = appendConnectionErrorHints(mysqlConfig("require_ssl=false&verifyServerCertificate=true"), "MySQL connection failed: TLS handshake failed", t);
const required = appendConnectionErrorHints(mysqlConfig("require_ssl=true&useSSL=false"), "MySQL connection failed: TLS handshake failed", t);
expect(disabled).toBe("MySQL connection failed: TLS handshake failed");
expect(required).toContain("Set TLS Mode to Disabled.");
});
it("uses the last duplicate native require_ssl value", () => {
const required = appendConnectionErrorHints(mysqlConfig("require_ssl=false&require_ssl=true"), "MySQL connection failed: TLS handshake failed", t);
const disabled = appendConnectionErrorHints(mysqlConfig("require_ssl=true&require_ssl=false"), "MySQL connection failed: TLS handshake failed", t);
expect(required).toContain("Set TLS Mode to Disabled.");
expect(disabled).toBe("MySQL connection failed: TLS handshake failed");
});
it("uses a certificate-specific hint for unsupported certificate versions", () => {
const error = "MySQL connection failed: invalid peer certificate: Other(OtherError(UnsupportedCertVersion))";
const message = appendConnectionErrorHints(mysqlConfig("sslMode=VERIFY_CA"), error, t);
expect(message).toContain("Replace the certificate or use Required mode.");
expect(message).not.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);
@@ -12,10 +12,28 @@ function normalizeUrlParamKey(key: string): string {
function urlParamValue(params: URLSearchParams, key: string): string {
const normalizedKey = normalizeUrlParamKey(key);
let result = "";
for (const [paramKey, value] of params.entries()) {
if (normalizeUrlParamKey(paramKey) === normalizedKey) return value;
if (normalizeUrlParamKey(paramKey) === normalizedKey) result = value;
}
return "";
return result;
}
function jdbcUrlParamValue(params: URLSearchParams, key: string): string {
const normalizedKey = key.trim().toLowerCase();
let result = "";
for (const [paramKey, value] of params.entries()) {
if (paramKey.trim().toLowerCase() === normalizedKey) result = value;
}
return result;
}
function hasJdbcUrlParam(params: URLSearchParams, key: string): boolean {
const normalizedKey = key.trim().toLowerCase();
for (const paramKey of params.keys()) {
if (paramKey.trim().toLowerCase() === normalizedKey) return true;
}
return false;
}
function mysqlTlsMode(config: ConnectionConfig): string {
@@ -25,7 +43,19 @@ function mysqlTlsMode(config: ConnectionConfig): string {
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";
const isTrue = (value: string) => ["true", "1", "yes", "on"].includes(value);
const isFalse = (value: string) => ["false", "0", "no", "off"].includes(value);
const nativeRequireSsl = jdbcUrlParamValue(parsed, "require_ssl").trim().toLowerCase();
if (isFalse(nativeRequireSsl)) return "disabled";
if (isTrue(nativeRequireSsl)) return "required";
const jdbcUseSsl = jdbcUrlParamValue(parsed, "useSSL").trim().toLowerCase();
const jdbcRequireSsl = jdbcUrlParamValue(parsed, "requireSSL").trim().toLowerCase();
const jdbcVerifyServerCertificate = jdbcUrlParamValue(parsed, "verifyServerCertificate").trim().toLowerCase();
if (isFalse(jdbcUseSsl)) return "disabled";
if (isTrue(jdbcVerifyServerCertificate)) return "verify_ca";
if (isTrue(jdbcRequireSsl)) return "required";
if (hasJdbcUrlParam(parsed, "useSSL") || hasJdbcUrlParam(parsed, "requireSSL") || hasJdbcUrlParam(parsed, "verifyServerCertificate")) return "preferred";
if (config.ssl) return "required";
return "disabled";
}
@@ -66,6 +96,9 @@ export function appendConnectionErrorHints(config: ConnectionConfig | undefined,
}
if (mysqlTlsMode(config) === "disabled") return message;
if (!isMysqlTlsLikeFailure(message)) return message;
if (/UnsupportedCertVersion/i.test(message)) {
return appendHint(message, t("connection.mysqlUnsupportedCertVersionHint"));
}
const hint = t("connection.mysqlTlsConnectionFailureHint");
return appendHint(message, hint);
}
@@ -257,6 +257,18 @@ function queryParamValue(params: string, key: string): string | undefined {
return undefined;
}
function queryParamLastValue(params: string, key: string): string | undefined {
let result: string | undefined;
for (const part of params.split(/[&;]/)) {
if (!part) continue;
const [rawKey, ...rest] = part.split("=");
if (decodeUrlPart(rawKey).toLowerCase() === key.toLowerCase()) {
result = decodeUrlPart(rest.join("=")).trim();
}
}
return result;
}
function extractHiveStructuredParams(params: string): { username?: string; password?: string; ssl: boolean; urlParams: string } {
let username: string | undefined;
let password: string | undefined;
@@ -334,10 +346,16 @@ function urlParamsRequireTls(dbType: DatabaseType, params: string): boolean {
}
if (dbType === "mysql") {
const requireSsl = queryParamValue(params, "require_ssl")?.toLowerCase();
const requireSsl = queryParamLastValue(params, "require_ssl")?.toLowerCase();
if (requireSsl === "true" || requireSsl === "1" || requireSsl === "yes") return true;
const sslMode = (queryParamValue(params, "ssl-mode") || queryParamValue(params, "sslmode") || "").toLowerCase().replace("-", "_");
return sslMode === "required" || sslMode === "require" || sslMode === "verify_ca" || sslMode === "verify_identity";
const sslMode = (queryParamLastValue(params, "ssl-mode") || queryParamLastValue(params, "sslmode") || "").toLowerCase().replace("-", "_");
if (sslMode === "required" || sslMode === "require" || sslMode === "verify_ca" || sslMode === "verify_identity") return true;
if (requireSsl !== undefined || sslMode) return false;
const jdbcUseSsl = (queryParamLastValue(params, "useSSL") || "").toLowerCase();
const jdbcRequireSsl = (queryParamLastValue(params, "requireSSL") || "").toLowerCase();
const jdbcVerifyServerCertificate = (queryParamLastValue(params, "verifyServerCertificate") || "").toLowerCase();
if (["false", "0", "no", "off"].includes(jdbcUseSsl)) return false;
return ["true", "1", "yes", "on"].includes(jdbcRequireSsl) || ["true", "1", "yes", "on"].includes(jdbcVerifyServerCertificate);
}
if (dbType === "postgres" || dbType === "redshift" || dbType === "kwdb") {