Skip to content

Commit

Permalink
fix(oauth): clear session when encountering errors
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiDood committed Aug 8, 2024
1 parent 1e131e0 commit 2cbcf8c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions app/src/routes/oauth/callback/+server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ export async function GET({ fetch, locals: { db }, cookies, url: { searchParams
if (typeof sid === 'undefined') redirect(302, '/oauth/login/');

const state = searchParams.get('state');
if (state === null) error(400);
if (state === null) {
cookies.delete('sid', { path: '/', httpOnly: true, sameSite: 'lax' });
error(400);
}

const hashedSessionId = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(sid));
if (Buffer.from(state, 'base64url').compare(Buffer.from(hashedSessionId)) !== 0) error(400);
if (Buffer.from(state, 'base64url').compare(Buffer.from(hashedSessionId)) !== 0) {
cookies.delete('sid', { path: '/', httpOnly: true, sameSite: 'lax' });
error(400);
}

const code = searchParams.get('code');
if (code === null) error(400);
if (code === null) {
cookies.delete('sid', { path: '/', httpOnly: true, sameSite: 'lax' });
error(400);
}

const body = new URLSearchParams({
code: parse(AuthorizationCode, code),
Expand Down

0 comments on commit 2cbcf8c

Please sign in to comment.