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
14 changes: 12 additions & 2 deletions src/app/api/auth/upload-avatar/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ import { createClient } from '@supabase/supabase-js';

const supabaseAuth = createClient(process.env.NEXT_PUBLIC_SUPABASE_URL, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY);

function getBearerToken(authHeader) {
if (typeof authHeader !== 'string') return null;

const match = authHeader.match(/^Bearer\s+(.+)$/i);
const token = match?.[1]?.trim();

return token || null;
}

async function authenticateBearerToken(request) {
const authHeader = request.headers.get('authorization');
if (!authHeader || !authHeader.startsWith('Bearer ')) {
const token = getBearerToken(authHeader);

if (!token) {
return { error: 'Authentication required' };
}

const token = authHeader.substring(7);
const { data: { user }, error } = await supabaseAuth.auth.getUser(token);

if (error || !user?.id) {
Expand Down
35 changes: 35 additions & 0 deletions src/app/api/auth/upload-avatar/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ function avatarRequest(method) {
});
}

function avatarRequestWithAuthorization(method, authorization) {
return new Request('https://example.com/api/auth/upload-avatar', {
method,
headers: {
authorization
}
});
}

describe('upload-avatar authentication', () => {
beforeEach(() => {
vi.resetModules();
Expand Down Expand Up @@ -62,4 +71,30 @@ describe('upload-avatar authentication', () => {
expect(mocks.createClient).toHaveBeenCalledTimes(1);
expect(mocks.createClient).toHaveBeenCalledWith('https://example.supabase.co', 'anon-key');
});

it('normalizes bearer scheme casing and extra spaces before validating the token', async () => {
const { DELETE } = await import('./route.js');

const response = await DELETE(avatarRequestWithAuthorization('DELETE', `bearer ${forgedToken} `));
const body = await response.json();

expect(response.status).toBe(401);
expect(body.error).toBe('Invalid authentication token');
expect(mocks.authGetUser).toHaveBeenCalledWith(forgedToken);
expect(mocks.createClient).toHaveBeenCalledTimes(1);
expect(mocks.createClient).toHaveBeenCalledWith('https://example.supabase.co', 'anon-key');
});

it('does not validate an empty bearer header', async () => {
const { DELETE } = await import('./route.js');

const response = await DELETE(avatarRequestWithAuthorization('DELETE', 'Bearer '));
const body = await response.json();

expect(response.status).toBe(401);
expect(body.error).toBe('Authentication required');
expect(mocks.authGetUser).not.toHaveBeenCalled();
expect(mocks.createClient).toHaveBeenCalledTimes(1);
expect(mocks.createClient).toHaveBeenCalledWith('https://example.supabase.co', 'anon-key');
});
});
Loading