From cc03980d8328998c0e6d960788dbcd10bc3403f1 Mon Sep 17 00:00:00 2001 From: "keita.katayama" Date: Thu, 26 Jun 2025 23:22:23 +0900 Subject: [PATCH 1/2] fix: caching_sha2_password auth with mysql 8.0.5+ --- src/auth_plugin/crypt.ts | 2 +- src/auth_plugin/index.ts | 4 ++-- src/connection.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/auth_plugin/crypt.ts b/src/auth_plugin/crypt.ts index 8eb2339..3f2244f 100644 --- a/src/auth_plugin/crypt.ts +++ b/src/auth_plugin/crypt.ts @@ -11,7 +11,7 @@ async function encryptWithPublicKey( const importedKey = await crypto.subtle.importKey( "spki", base64Decode(key), - { name: "RSA-OAEP", hash: "SHA-256" }, + { name: "RSA-OAEP", hash: "SHA-1" }, false, ["encrypt"], ); diff --git a/src/auth_plugin/index.ts b/src/auth_plugin/index.ts index 198e023..74bc2db 100644 --- a/src/auth_plugin/index.ts +++ b/src/auth_plugin/index.ts @@ -1,4 +1,4 @@ -import * as caching_sha2_password from "./caching_sha2_password.ts"; +import { start as caching_sha2_password } from "./caching_sha2_password.ts"; export default { - caching_sha2_password, + caching_sha2_password: { start: caching_sha2_password }, }; diff --git a/src/connection.ts b/src/connection.ts index 3f2b23f..42d6a79 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -186,7 +186,7 @@ export class Connection { receive = await this.nextPacket(); } if (result.quickRead) { - await this.nextPacket(); + receive = await this.nextPacket(); } if (result.next) { result = await result.next(receive); From bfb20eee943f76a973750c8c43eb9a85c78deafe Mon Sep 17 00:00:00 2001 From: "keita.katayama" Date: Fri, 27 Jun 2025 00:13:05 +0900 Subject: [PATCH 2/2] feat: add tests for caching_sha2_password user creation and authentication --- test.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test.ts b/test.ts index e030e8c..4dbf95e 100644 --- a/test.ts +++ b/test.ts @@ -352,6 +352,38 @@ testWithClient(async function testSelectEmptyString(client) { ); }); +testWithClient(async function testCreateUserWithCachingSha2Password(client) { + const { version } = (await client.query(`SELECT VERSION() as version`))[0]; + if (version.startsWith("8.")) { + await client.execute( + `CREATE USER 'sha2_test_user'@'%' IDENTIFIED WITH caching_sha2_password BY 'sha2_password'`, + ); + } else { + await client.execute( + `CREATE USER 'sha2_test_user'@'%' IDENTIFIED BY 'sha2_password'`, + ); + } + await client.execute(`GRANT ALL ON test.* TO 'sha2_test_user'@'%'`); +}); + +testWithClient(async function testCachingSha2PasswordAuthenticateRoot(client) { + await client.execute("FLUSH PRIVILEGES"); +}); + +testWithClient(async function testCachingSha2PasswordWithClearCache(client) { + assertEquals( + await client.query(`SELECT CURRENT_USER() AS user`), + [{ user: "sha2_test_user@%" }], + ); + + const result = await client.query(`SELECT 'caching_sha2_password_test' AS test`); + assertEquals(result, [{ test: "caching_sha2_password_test" }]); +}, { username: "sha2_test_user", password: "sha2_password" }); + +testWithClient(async function testDropUserWithCachingSha2Password(client) { + await client.execute(`DROP USER 'sha2_test_user'@'%'`); +}); + registerTests(); Deno.test("configLogger()", async () => {