Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(security): sign in with the PKCE flow so a link cannot plant a session

spacesdrive0d8efaa0af52
diff --git a/docs/architecture/data-flows/AUTHENTICATION.md b/docs/architecture/data-flows/AUTHENTICATION.md
index 80731a5..d0b1445 100644
--- a/docs/architecture/data-flows/AUTHENTICATION.md
+++ b/docs/architecture/data-flows/AUTHENTICATION.md
@@ -170,6 +170,8 @@ No OAuth redirect is involved, so the Google client has JavaScript origins only
## Session Lifecycle
+The browser client uses the PKCE flow (`SUPABASE_AUTH_OPTIONS` in `frontend/src/lib/supabaseAuthOptions.ts`). A sign-in only comes back from a URL as `?code=`, which this browser exchanges with the verifier it stored when the request started; a `#access_token` fragment is never adopted, so a crafted link cannot sign a person in as someone else and have them connect accounts to that person (audit run 2, N2). Email links therefore sign in only in the browser that asked for them; the 6-digit codes work anywhere.
+
```
Session created (on any sign-in)
│
diff --git a/frontend/src/lib/supabase.ts b/frontend/src/lib/supabase.ts
index cc6a4ad..8424cb3 100644
--- a/frontend/src/lib/supabase.ts
+++ b/frontend/src/lib/supabase.ts
@@ -1,4 +1,5 @@
import { createClient } from '@supabase/supabase-js';
+import { SUPABASE_AUTH_OPTIONS } from './supabaseAuthOptions';
const url = import.meta.env.VITE_SUPABASE_URL;
const anonKey = import.meta.env.VITE_SUPABASE_ANON_KEY;
@@ -7,4 +8,4 @@ if (!url || !anonKey) {
throw new Error('Missing VITE_SUPABASE_URL or VITE_SUPABASE_ANON_KEY in environment');
}
-export const supabase = createClient(url, anonKey);
+export const supabase = createClient(url, anonKey, { auth: SUPABASE_AUTH_OPTIONS });
diff --git a/frontend/src/lib/supabaseAuthOptions.ts b/frontend/src/lib/supabaseAuthOptions.ts
new file mode 100644
index 0000000..3c9900f
--- /dev/null
+++ b/frontend/src/lib/supabaseAuthOptions.ts
@@ -0,0 +1,4 @@
+// the PKCE flow only takes a sign-in back from a URL when this browser started it (a code plus the verifier it stored);
+// the default implicit flow would adopt any #access_token fragment, so a crafted link could sign a person in as someone
+// else and make them connect their accounts to that person (audit run 2, N2)
+export const SUPABASE_AUTH_OPTIONS = { flowType: 'pkce' } as const;
diff --git a/frontend/tests/lib/supabaseAuthOptions.test.ts b/frontend/tests/lib/supabaseAuthOptions.test.ts
new file mode 100644
index 0000000..3560222
--- /dev/null
+++ b/frontend/tests/lib/supabaseAuthOptions.test.ts
@@ -0,0 +1,7 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { SUPABASE_AUTH_OPTIONS } from '../../src/lib/supabaseAuthOptions.ts';
+
+test('the app signs in with the PKCE flow, so a session in a link fragment is never adopted', () => {
+ assert.equal(SUPABASE_AUTH_OPTIONS.flowType, 'pkce');
+});