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): refuse Claude app tokens on the REST API
spacesdrive5662ec83709e
diff --git a/backend/middleware/auth.ts b/backend/middleware/auth.tsindex feea763..de4d1b6 100644--- a/backend/middleware/auth.ts+++ b/backend/middleware/auth.ts@@ -1,6 +1,7 @@import type { Next } from 'hono';import { getSupabaseAuth } from '../database/supabase.ts';import { UnauthorizedError } from '../lib/errors.ts';+import { jwtClaims, oauthClientId } from '../lib/jwtClaims.ts';import type { AppContext } from '../types/app.ts';const BEARER = /^Bearer\s+(\S+)$/i;@@ -11,6 +12,9 @@ export async function requireAuth(c: AppContext, next: Next): Promise<void> {if (!token) throw new UnauthorizedError('Sign in to use the API');const { data: { user }, error } = await getSupabaseAuth(c.env).auth.getUser(token);if (error || !user) throw new UnauthorizedError('Your session has expired - sign in again');+ // a token Supabase's OAuth server issued to a Claude app only works on /mcp, where the app's permission and approval+ // are checked; here it would act with the person's full session (audit run 2, N1)+ if (oauthClientId(jwtClaims(token))) throw new UnauthorizedError('This token belongs to a connected app and only works with the Claude connection. Sign in to use the API');c.set('userId', user.id);c.set('userEmail', user.email);await next();diff --git a/backend/tests/middleware/auth.test.ts b/backend/tests/middleware/auth.test.tsnew file mode 100644index 0000000..36b5149--- /dev/null+++ b/backend/tests/middleware/auth.test.ts@@ -0,0 +1,48 @@+import { test, mock } from 'node:test';+import assert from 'node:assert/strict';+import { Hono } from 'hono';+import { requireAuth } from '../../middleware/auth.ts';+import { errorHandler } from '../../middleware/errorHandler.ts';+import type { AppEnv } from '../../types/app.ts';+import type { Env } from '../../types/env.ts';++const ENV = { SUPABASE_URL: 'https://project.supabase.co', SUPABASE_ANON_KEY: 'anon' } as Env;++// a token shaped like Supabase's: the claims are what the middleware reads after Supabase verified the signature+function token(claims: Record<string, unknown>): string {+ const encode = (value: unknown) => btoa(JSON.stringify(value)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');+ return `${encode({ alg: 'HS256' })}.${encode(claims)}.signature`;+}++function app() {+ const server = new Hono<AppEnv>();+ server.onError(errorHandler);+ server.get('/api/me', requireAuth, c => c.json({ userId: c.get('userId') }));+ return server;+}++function supabaseAccepts(t: { after(fn: () => void): void }) {+ const fetchMock = mock.method(globalThis, 'fetch', async () => Response.json({ id: 'user-1', email: '[email protected]', aud: 'authenticated' }));+ t.after(() => fetchMock.mock.restore());+}++test('an app session token signs the person in', async t => {+ supabaseAccepts(t);+ const response = await app().request('/api/me', { headers: { Authorization: `Bearer ${token({ sub: 'user-1' })}` } }, ENV);+ assert.equal(response.status, 200);+ assert.deepEqual(await response.json(), { userId: 'user-1' });+});++test('a token issued to a connected Claude app is refused on the API, even though Supabase accepts it', async t => {+ supabaseAccepts(t);+ const response = await app().request('/api/me', { headers: { Authorization: `Bearer ${token({ sub: 'user-1', client_id: 'claude-app' })}` } }, ENV);+ assert.equal(response.status, 401);+ const body = await response.json() as { code: string; message: string };+ assert.equal(body.code, 'UNAUTHORIZED');+ assert.match(body.message, /connected app/);+});++test('a request without a token is refused', async () => {+ const response = await app().request('/api/me', {}, ENV);+ assert.equal(response.status, 401);+});