Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/app/api/crypto/public-keys/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function getServiceRoleClient() {
return supabaseServiceRole;
}

function normalizeUserId(userId) {
return typeof userId === 'string' ? userId.trim() : '';
}

// Create regular client for JWT validation
const supabaseClient = createClient(process.env.NEXT_PUBLIC_SUPABASE_URL, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY);

Expand Down Expand Up @@ -105,7 +109,7 @@ export async function GET(request) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}

const userId = url.searchParams.get('user_id');
const userId = normalizeUserId(url.searchParams.get('user_id'));
if (!userId) {
return NextResponse.json({ error: 'Missing user_id parameter' }, { status: 400 });
}
Expand Down
17 changes: 17 additions & 0 deletions src/app/api/crypto/public-keys/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,21 @@ describe('public key cookie authentication', () => {
expect(body).toEqual({ public_key: 'public-key', user_id: 'target-user-id' });
expect(mocks.authGetUser).toHaveBeenCalledWith('access-token');
});

it('rejects blank user ids before looking up a target public key', async () => {
const { GET } = await import('./route.js');
const response = await GET(
new Request('https://qrypt.chat/api/crypto/public-keys?user_id=%20%20%20', {
headers: {
cookie: `sb-xydzwxwsbgmznthiiscl-auth-token=${cookieValue('access-token')}`
}
})
);
const body = await response.json();

expect(response.status).toBe(400);
expect(body).toEqual({ error: 'Missing user_id parameter' });
expect(mocks.serviceFrom).toHaveBeenCalledTimes(1);
expect(mocks.rpc).not.toHaveBeenCalled();
});
});
Loading