fix(mysql): require TLS for TiDB Cloud connections (#911)

Thanks!
This commit is contained in:
Holata Seminole
2026-06-09 12:06:38 +08:00
committed by GitHub
parent 5138c1a702
commit 70dad4e00b
3 changed files with 44 additions and 6 deletions
+9 -1
View File
@@ -205,6 +205,10 @@ function urlParamsRequireTls(dbType: DatabaseType, params: string): boolean {
return false;
}
function isTidbCloudHost(host: string): boolean {
return host.toLowerCase().endsWith(".tidbcloud.com");
}
export function connectionProfileForScheme(scheme: string, preferredProfile?: string): ConnectionProfile | undefined {
if ((scheme === "http" || scheme === "https") && preferredProfile) {
return HTTP_SELECTED_PROFILES[preferredProfile];
@@ -433,7 +437,11 @@ export function parseConnectionUrl(value: string, preferredProfile?: string): Pa
password: mysqlCredentials?.password ?? decodeUrlPart(parsed.password),
database: databaseFromPath(parsed.pathname),
urlParams: effectiveUrlParams,
ssl: scheme === "rediss" || scheme === "https" || urlParamsRequireTls(profile.type, effectiveUrlParams),
ssl:
scheme === "rediss" ||
scheme === "https" ||
urlParamsRequireTls(profile.type, effectiveUrlParams) ||
(profile.type === "mysql" && isTidbCloudHost(parsed.hostname)),
};
}
+26 -5
View File
@@ -921,7 +921,9 @@ impl ConnectionConfig {
return normalize_bare_mysql_url_params(value);
}
match self.db_type {
DatabaseType::Mysql => normalize_mysql_url_params(value, self.ssl, self.ca_cert_path.trim().is_empty()),
DatabaseType::Mysql => {
normalize_mysql_url_params(value, self.mysql_uses_tls(), self.ca_cert_path.trim().is_empty())
}
DatabaseType::Doris | DatabaseType::StarRocks | DatabaseType::Databend => {
normalize_bare_mysql_url_params(value)
}
@@ -935,6 +937,10 @@ impl ConnectionConfig {
self.ssl || url_params_contains_flag(self.url_params.as_deref(), "secure", "true")
}
fn mysql_uses_tls(&self) -> bool {
self.ssl || self.host.to_ascii_lowercase().ends_with(".tidbcloud.com")
}
fn redis_tls_insecure_fragment(&self) -> &'static str {
if self.ssl && self.redis_tls_insecure() {
"#insecure"
@@ -987,10 +993,12 @@ fn normalize_mysql_url_params(value: &str, force_tls: bool, accept_invalid_certs
let mut parts: Vec<String> = value.split('&').filter(|part| !part.is_empty()).map(str::to_string).collect();
if force_tls {
parts.retain(|part| !url_param_key_is(part, "ssl-mode") && !url_param_key_is(part, "sslmode"));
if !parts.iter().any(|part| url_param_key_is(part, "require_ssl")) {
parts.insert(0, "require_ssl=true".to_string());
}
parts.retain(|part| {
!url_param_key_is(part, "ssl-mode")
&& !url_param_key_is(part, "sslmode")
&& !url_param_key_is(part, "require_ssl")
});
parts.insert(0, "require_ssl=true".to_string());
if accept_invalid_certs && !parts.iter().any(|part| url_param_key_is(part, "verify_ca")) {
parts.push("verify_ca=false".to_string());
}
@@ -1668,6 +1676,19 @@ mod tests {
);
}
#[test]
fn tidb_cloud_mysql_url_requires_tls() {
let mut config = mysql_config("root", "secret", Some("test"));
config.host = "gateway01.us-west-2.prod.aws.tidbcloud.com".to_string();
config.port = 4000;
config.url_params = Some("require_ssl=false&charset=utf8mb4".to_string());
assert_eq!(
config.connection_url(),
"mysql://root:secret@gateway01.us-west-2.prod.aws.tidbcloud.com:4000/test?require_ssl=true&verify_ca=false&verify_identity=false&charset=utf8mb4"
);
}
#[test]
fn postgres_url_appends_custom_params() {
let mut config = mysql_config("postgres", "secret", Some("test"));
+9
View File
@@ -50,6 +50,15 @@ test("parses mysql TLS URL params into the SSL switch state", () => {
assert.equal(parseConnectionUrl("mysql://root@tidb.example.com:4000/test?require_ssl=true").ssl, true);
});
test("parses TiDB Cloud MySQL URLs as TLS connections", () => {
const parsed = parseConnectionUrl(
"mysql://root:secret@gateway01.us-west-2.prod.aws.tidbcloud.com:4000/test",
);
assert.equal(parsed.dbType, "mysql");
assert.equal(parsed.ssl, true);
});
test("parses MySQL JDBC user and password URL params as credentials", () => {
const parsed = parseConnectionUrl(
"jdbc:mysql://127.0.0.1:1234/example?user=admin&password=pwd&useUnicode=true&characterEncoding=UTF8&useSSL=false",