Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

fix(auth): store magic link tokens hashed (#547)

Croffc8d2ff981b07
diff --git a/apps/api/src/__tests__/integration/magic-link.test.ts b/apps/api/src/__tests__/integration/magic-link.test.ts
new file mode 100644
index 00000000..89308430
--- /dev/null
+++ b/apps/api/src/__tests__/integration/magic-link.test.ts
@@ -0,0 +1,32 @@
+import { describe, it, expect, beforeEach } from 'bun:test';
+import { auth, setAuthSettings } from '@repo/auth';
+import { db, verification } from '@repo/db';
+import { like } from 'drizzle-orm';
+import { signUpTestUser } from '#tests/helpers/auth';
+import { resetDb } from '#tests/helpers/db';
+
+describe('magic link', () => {
+ beforeEach(resetDb);
+
+ // An OAuth state is one such identifier (GHSA-965c-763c-88jm).
+ it('does not redeem a stored verification identifier as a token', async () => {
+ const user = await signUpTestUser();
+ await setAuthSettings({ magicLink: true });
+ await auth.api.signInMagicLink({ body: { email: user.email }, headers: new Headers() });
+
+ const rows = await db
+ .select({ identifier: verification.identifier })
+ .from(verification)
+ .where(like(verification.value, `%${user.email}%`));
+ expect(rows).toHaveLength(1);
+
+ const res = await auth.api.magicLinkVerify({
+ query: { token: rows[0].identifier, callbackURL: '/' },
+ headers: new Headers(),
+ asResponse: true,
+ });
+
+ expect(res.headers.get('location')).toContain('error=INVALID_TOKEN');
+ expect(res.headers.getSetCookie()).toEqual([]);
+ });
+});
diff --git a/packages/auth/src/index.ts b/packages/auth/src/index.ts
index a373fa22..08c114db 100644
--- a/packages/auth/src/index.ts
+++ b/packages/auth/src/index.ts
@@ -744,6 +744,8 @@ export const auth = betterAuth({
// sender refuses when it is off. It reuses the `verification` table — no new
// table, so `auth:generate` output is unchanged.
magicLink({
+ // Stored plain, an OAuth state also passes as a token (GHSA-965c-763c-88jm).
+ storeToken: 'hashed',
sendMagicLink: async ({ email, url }) => {
const settings = await getAuthSettings();
if (!settings.magicLink) {