fix(mysql): support cleartext password auth option (#2499)

This commit is contained in:
LRcoding authored and GitHub committed 2026-07-03 13:47:47 +08:00
1 parent cabe89a19b
commit 21cec24e75
12 files changed
+187 -1

No files matched your search

@@ -0,0 +1,19 @@
import { describe, expect, it } from "vitest";
import { mysqlCleartextPasswordAuthEnabled, setMysqlCleartextPasswordAuthEnabled } from "../mysqlConnectionOptions";
describe("mysqlConnectionOptions", () => {
it("detects cleartext password auth aliases", () => {
expect(mysqlCleartextPasswordAuthEnabled("allowCleartextPasswords=true")).toBe(true);
expect(mysqlCleartextPasswordAuthEnabled("enable_cleartext_plugin=true")).toBe(true);
expect(mysqlCleartextPasswordAuthEnabled("?AllowCleartextPasswords=TRUE")).toBe(true);
expect(mysqlCleartextPasswordAuthEnabled("allowCleartextPasswords=false")).toBe(false);
});
it("enables the canonical cleartext password auth param and removes aliases", () => {
expect(setMysqlCleartextPasswordAuthEnabled("allowCleartextPasswords=true&charset=utf8mb4", true)).toBe("charset=utf8mb4&enable_cleartext_plugin=true");
});
it("disables cleartext password auth params while preserving unrelated params", () => {
expect(setMysqlCleartextPasswordAuthEnabled("allowCleartextPasswords=true&enable_cleartext_plugin=true&charset=utf8mb4&connect_timeout=10", false)).toBe("charset=utf8mb4&connect_timeout=10");
});
});
@@ -0,0 +1,31 @@
const MYSQL_CLEAR_TEXT_PARAM_ALIASES = new Set(["allowcleartextpasswords", "enable_cleartext_plugin"]);
const MYSQL_CLEAR_TEXT_PARAM = "enable_cleartext_plugin";
function normalizeKey(key: string): string {
return key.trim().toLowerCase();
}
function isTruthyUrlParam(value: string): boolean {
return ["true", "1", "yes", "on"].includes(value.trim().toLowerCase());
}
function isMysqlCleartextPasswordParam(key: string): boolean {
return MYSQL_CLEAR_TEXT_PARAM_ALIASES.has(normalizeKey(key));
}
export function mysqlCleartextPasswordAuthEnabled(params: string | undefined): boolean {
const parsed = new URLSearchParams((params || "").trim().replace(/^\?/, ""));
for (const [key, value] of parsed.entries()) {
if (isMysqlCleartextPasswordParam(key) && isTruthyUrlParam(value)) return true;
}
return false;
}
export function setMysqlCleartextPasswordAuthEnabled(params: string | undefined, enabled: boolean): string {
const parsed = new URLSearchParams((params || "").trim().replace(/^\?/, ""));
for (const key of [...parsed.keys()]) {
if (isMysqlCleartextPasswordParam(key)) parsed.delete(key);
}
if (enabled) parsed.set(MYSQL_CLEAR_TEXT_PARAM, "true");
return parsed.toString();
}