Skip to content
Open
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
5 changes: 5 additions & 0 deletions src/app/api/chat/messages/[id]/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export async function GET(request, { params } = {}) {

// Handle encrypted content - could be base64 string or serialized Buffer object
const rawContent = message.message_recipients[0].encrypted_content;
if (typeof rawContent !== 'string') {
console.error('[API GET] Invalid encrypted content type for message:', message.id, typeof rawContent);
return NextResponse.json({ error: 'Invalid encrypted content' }, { status: 500 });
}

try {
// Check if it's a serialized Buffer object (JSON string containing {"type":"Buffer","data":[...]})
if (typeof rawContent === 'string' && rawContent.includes('"type":"Buffer"')) {
Expand Down
41 changes: 41 additions & 0 deletions src/app/api/chat/messages/[id]/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ function createMessagesQuery() {
return query;
}

function createMessagesQueryWithEncryptedContent(encryptedContent) {
const query = {
select: vi.fn(() => query),
eq: mocks.messageEq,
single: vi.fn().mockResolvedValue({
data: {
id: 'message-1',
message_recipients: [
{
encrypted_content: encryptedContent
}
]
},
error: null
})
};
mocks.messageEq.mockReturnValue(query);
return query;
}

describe('GET /api/chat/messages/[id]', () => {
beforeEach(() => {
vi.resetModules();
Expand Down Expand Up @@ -93,4 +113,25 @@ describe('GET /api/chat/messages/[id]', () => {
expect(body).toEqual({ error: 'Message ID is required' });
expect(mocks.authGetUser).not.toHaveBeenCalled();
});

it('returns a controlled error for non-string encrypted content', async () => {
mocks.from.mockImplementation((table) => {
if (table === 'users') return createUsersQuery();
if (table === 'messages') return createMessagesQueryWithEncryptedContent({ type: 'Buffer', data: [] });
throw new Error(`Unexpected table: ${table}`);
});

const { GET } = await import('./route.js');
const response = await GET(
new Request('https://qrypt.chat/api/chat/messages/message-1', {
headers: { cookie: 'sb-access-token=valid-token' }
}),
{ params: Promise.resolve({ id: 'message-1' }) }
);
const body = await response.json();

expect(response.status).toBe(500);
expect(body).toEqual({ error: 'Invalid encrypted content' });
expect(mocks.messageEq).toHaveBeenCalledWith('id', 'message-1');
});
});
Loading