Skip to content
Merged
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
10 changes: 7 additions & 3 deletions src/app/api/crypto/public-keys/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,20 @@ export async function PUT(request) {

const { public_key, key_type = 'ML-KEM-1024' } = await request.json();

if (!public_key) {
if (!public_key || typeof public_key !== 'string' || !public_key.trim()) {
return NextResponse.json({ error: 'Missing public_key' }, { status: 400 });
}

if (typeof key_type !== 'string' || !key_type.trim()) {
return NextResponse.json({ error: 'Invalid key_type' }, { status: 400 });
}

// Sync public key to database using the upsert function
const { data: result, error } = await getServiceRoleClient()
.rpc('upsert_user_public_key', {
target_user_id: user.auth_user_id, // Use auth_user_id for the function
public_key_param: public_key,
key_type_param: key_type
public_key_param: public_key.trim(),
key_type_param: key_type.trim()
});

if (error) {
Expand Down
21 changes: 21 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,25 @@ 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 non-string public keys before upsert RPC work', async () => {
const { PUT } = await import('./route.js');
const response = await PUT(
new Request('https://qrypt.chat/api/crypto/public-keys', {
method: 'PUT',
headers: {
cookie: `sb-xydzwxwsbgmznthiiscl-auth-token=${cookieValue('access-token')}`,
'content-type': 'application/json'
},
body: JSON.stringify({
public_key: { key: 'ml-kem-public-key' }
})
})
);
const body = await response.json();

expect(response.status).toBe(400);
expect(body).toEqual({ error: 'Missing public_key' });
expect(mocks.rpc).not.toHaveBeenCalled();
});
});
Loading