From df92e413849f519f573e9b1a344db394392d1413 Mon Sep 17 00:00:00 2001 From: Lokowitz Date: Thu, 25 Sep 2025 19:55:36 +0000 Subject: [PATCH] added migration for simplewebauthn --- server/setup/migrationsPg.ts | 2 ++ server/setup/migrationsSqlite.ts | 2 ++ server/setup/scriptsPg/1.10.4.ts | 39 ++++++++++++++++++++++++++++ server/setup/scriptsSqlite/1.10.4.ts | 34 ++++++++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 server/setup/scriptsPg/1.10.4.ts create mode 100644 server/setup/scriptsSqlite/1.10.4.ts diff --git a/server/setup/migrationsPg.ts b/server/setup/migrationsPg.ts index 04779f30..5c748c89 100644 --- a/server/setup/migrationsPg.ts +++ b/server/setup/migrationsPg.ts @@ -11,6 +11,7 @@ import m3 from "./scriptsPg/1.8.0"; import m4 from "./scriptsPg/1.9.0"; import m5 from "./scriptsPg/1.10.0"; import m6 from "./scriptsPg/1.10.2"; +import m7 from "./scriptsPg/1.10.4"; // THIS CANNOT IMPORT ANYTHING FROM THE SERVER // EXCEPT FOR THE DATABASE AND THE SCHEMA @@ -23,6 +24,7 @@ const migrations = [ { version: "1.9.0", run: m4 }, { version: "1.10.0", run: m5 }, { version: "1.10.2", run: m6 }, + { version: "1.10.4", run: m7 }, // Add new migrations here as they are created ] as { version: string; diff --git a/server/setup/migrationsSqlite.ts b/server/setup/migrationsSqlite.ts index 654c2716..d7c6793f 100644 --- a/server/setup/migrationsSqlite.ts +++ b/server/setup/migrationsSqlite.ts @@ -29,6 +29,7 @@ import m24 from "./scriptsSqlite/1.9.0"; import m25 from "./scriptsSqlite/1.10.0"; import m26 from "./scriptsSqlite/1.10.1"; import m27 from "./scriptsSqlite/1.10.2"; +import m28 from "./scriptsSqlite/1.10.4"; // THIS CANNOT IMPORT ANYTHING FROM THE SERVER // EXCEPT FOR THE DATABASE AND THE SCHEMA @@ -57,6 +58,7 @@ const migrations = [ { version: "1.10.0", run: m25 }, { version: "1.10.1", run: m26 }, { version: "1.10.2", run: m27 }, + { version: "1.10.4", run: m28 }, // Add new migrations here as they are created ] as const; diff --git a/server/setup/scriptsPg/1.10.4.ts b/server/setup/scriptsPg/1.10.4.ts new file mode 100644 index 00000000..311e6dc2 --- /dev/null +++ b/server/setup/scriptsPg/1.10.4.ts @@ -0,0 +1,39 @@ +import { db } from "@server/db/pg/driver"; +import { sql } from "drizzle-orm"; +import { isoBase64URL } from "@simplewebauthn/server/helpers"; + +const version = "1.10.4"; + +export default async function migration() { + console.log(`Running setup script ${version}...`); + + try { + await db.execute(sql`BEGIN`); + + const webauthnCredentialsQuery = await db.execute(sql`SELECT credentialId, publicKey FROM 'webauthnCredentials'`); + + const webauthnCredentials = webauthnCredentialsQuery.rows as { credentialId: string; publicKey: string }[]; + + for (const webauthnCredential of webauthnCredentials) { + const credentialId = isoBase64URL.fromBuffer(new Uint8Array(Buffer.from(webauthnCredential.credentialId, 'base64'))); + await db.execute(sql` + UPDATE "webauthnCredentials" SET "credentialId" = ${credentialId} + `); + + const publicKey = isoBase64URL.fromBuffer(new Uint8Array(Buffer.from(webauthnCredential.publicKey, 'base64'))); + await db.execute(sql` + UPDATE "webauthnCredentials" SET "publicKey" = ${publicKey} + `); + } + + await db.execute(sql`COMMIT`); + console.log(`Updated credentialId and publicKey`); + } catch (e) { + await db.execute(sql`ROLLBACK`); + console.log("Unable to update credentialId and publicKey"); + console.log(e); + throw e; + } + + console.log(`${version} migration complete`); +} diff --git a/server/setup/scriptsSqlite/1.10.4.ts b/server/setup/scriptsSqlite/1.10.4.ts new file mode 100644 index 00000000..5c7f0a0e --- /dev/null +++ b/server/setup/scriptsSqlite/1.10.4.ts @@ -0,0 +1,34 @@ +import { APP_PATH } from "@server/lib/consts"; +import Database from "better-sqlite3"; +import path from "path"; +import { isoBase64URL } from "@simplewebauthn/server/helpers"; + +const version = "1.10.4"; + +export default async function migration() { + console.log(`Running setup script ${version}...`); + + const location = path.join(APP_PATH, "db", "db.sqlite"); + const db = new Database(location); + + db.transaction(() => { + + const webauthnCredentials = db.prepare(`SELECT credentialId, publicKey FROM 'webauthnCredentials'`).all() as { + credentialId: string; publicKey: string + }[]; + + for (const webauthnCredential of webauthnCredentials) { + const credentialId = isoBase64URL.fromBuffer(new Uint8Array(Buffer.from(webauthnCredential.credentialId, 'base64'))); + db.prepare( + `UPDATE 'webauthnCredentials' SET 'credentialId' = ?` + ).run(credentialId); + + const publicKey = isoBase64URL.fromBuffer(new Uint8Array(Buffer.from(webauthnCredential.publicKey, 'base64'))); + db.prepare( + `UPDATE 'webauthnCredentials' SET 'publicKey' = ?` + ).run(publicKey); + } + })(); + + console.log(`${version} migration complete`); +}