mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
fix(oidc): bind the SSO auth code to the browser that earned it
The callback hands the finished login back as a code in a redirect URL, and /exchange turned any code into a session. That made the code the whole credential while it travelled somewhere URLs are readable: browser history on a shared machine, a referrer, a reverse proxy log. It cuts both ways. Redeem a code you saw over someone's shoulder and you are them; or complete a login as yourself, hand the victim's browser your code, and they are silently signed into your account and write their next trip into it. The callback now also sets a one-minute httpOnly cookie holding a random secret, and /exchange wants the code plus that cookie. Only the browser that came back from the provider holds both, which is the same shape the state cookie already gives the callback. The code is burnt on every attempt, right or wrong, and an unbound code answers exactly like an unknown one so presenting one confirms nothing.
This commit is contained in:
@@ -56,6 +56,16 @@ import { resetTestDb, resetRateLimits } from '../helpers/test-db';
|
||||
import { createUser } from '../helpers/factories';
|
||||
import { OidcService } from '../../src/nest/oidc/oidc.service';
|
||||
|
||||
/** Read one cookie's value out of a response's Set-Cookie header, as a browser would. */
|
||||
function readCookie(res: request.Response, name: string): string | undefined {
|
||||
const raw = res.headers['set-cookie'];
|
||||
const all: string[] = Array.isArray(raw) ? raw : raw ? [raw as unknown as string] : [];
|
||||
const hit = all.filter((c) => c.startsWith(`${name}=`)).pop();
|
||||
const value = hit?.split(';')[0].slice(name.length + 1);
|
||||
// An expired clear-cookie carries an empty value; treat that as "gone".
|
||||
return value ? decodeURIComponent(value) : undefined;
|
||||
}
|
||||
|
||||
const MOCK_DISCOVERY_DOC = {
|
||||
authorization_endpoint: 'https://oidc.example.com/auth',
|
||||
token_endpoint: 'https://oidc.example.com/token',
|
||||
@@ -307,9 +317,11 @@ describe('GET /api/auth/oidc/callback', () => {
|
||||
describe('GET /api/auth/oidc/exchange', () => {
|
||||
it('OIDC-011: valid auth code returns JWT and sets cookie', async () => {
|
||||
const fakeToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test.sig';
|
||||
const code = oidcSvc.createAuthCode(fakeToken);
|
||||
const { code, binding } = oidcSvc.createAuthCode(fakeToken);
|
||||
|
||||
const res = await request(app).get(`/api/auth/oidc/exchange?code=${code}`);
|
||||
const res = await request(app)
|
||||
.get(`/api/auth/oidc/exchange?code=${code}`)
|
||||
.set('Cookie', `trek_oidc_exchange=${binding}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.token).toBe(fakeToken);
|
||||
@@ -332,16 +344,28 @@ describe('GET /api/auth/oidc/exchange', () => {
|
||||
expect(res.body.error).toBeDefined();
|
||||
});
|
||||
|
||||
it('OIDC-013b: a real code without its binding cookie returns 400 and buys nothing', async () => {
|
||||
const { code } = oidcSvc.createAuthCode('valid.but.unbound');
|
||||
|
||||
const res = await request(app).get(`/api/auth/oidc/exchange?code=${code}`);
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
// Same wording as an unknown code: presenting a code must not confirm it exists.
|
||||
expect(res.body.error).toBe('Invalid or expired code');
|
||||
expect(readCookie(res, 'trek_session')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('OIDC-014: auth code is single-use (second use returns 400)', async () => {
|
||||
const fakeToken = 'test.token.here';
|
||||
const code = oidcSvc.createAuthCode(fakeToken);
|
||||
const { code, binding } = oidcSvc.createAuthCode(fakeToken);
|
||||
const cookie = `trek_oidc_exchange=${binding}`;
|
||||
|
||||
// First use: success
|
||||
const res1 = await request(app).get(`/api/auth/oidc/exchange?code=${code}`);
|
||||
const res1 = await request(app).get(`/api/auth/oidc/exchange?code=${code}`).set('Cookie', cookie);
|
||||
expect(res1.status).toBe(200);
|
||||
|
||||
// Second use: rejected
|
||||
const res2 = await request(app).get(`/api/auth/oidc/exchange?code=${code}`);
|
||||
// Second use: rejected, even from the same browser
|
||||
const res2 = await request(app).get(`/api/auth/oidc/exchange?code=${code}`).set('Cookie', cookie);
|
||||
expect(res2.status).toBe(400);
|
||||
});
|
||||
});
|
||||
@@ -367,8 +391,13 @@ describe('OIDC remember-me (#1927)', () => {
|
||||
expect(cb.status).toBe(302);
|
||||
const oidcCode = new URL(cb.headers.location!, 'http://localhost').searchParams.get('oidc_code')!;
|
||||
expect(oidcCode).toBeTruthy();
|
||||
// The browser carries the binding cookie the callback just set back to /exchange.
|
||||
const binding = readCookie(cb, 'trek_oidc_exchange')!;
|
||||
expect(binding).toBeTruthy();
|
||||
|
||||
return request(app).get(`/api/auth/oidc/exchange?code=${oidcCode}`);
|
||||
return request(app)
|
||||
.get(`/api/auth/oidc/exchange?code=${oidcCode}`)
|
||||
.set('Cookie', `trek_oidc_exchange=${binding}`);
|
||||
}
|
||||
|
||||
function sessionCookie(res: request.Response): string {
|
||||
@@ -402,3 +431,94 @@ describe('OIDC remember-me (#1927)', () => {
|
||||
expect(sessionCookie(res)).toContain('Max-Age=86400');
|
||||
});
|
||||
});
|
||||
|
||||
// ── the auth code is bound to one browser (GHSA / session fixation) ───────────
|
||||
|
||||
describe('OIDC auth-code binding', () => {
|
||||
// Walks /login → /callback for one "browser" and hands back everything that
|
||||
// browser holds afterwards: the code out of the redirect URL, and the binding
|
||||
// cookie out of the callback response.
|
||||
async function loginUpToCallback(sub: string, email: string) {
|
||||
mockDiscover.mockResolvedValue(MOCK_DISCOVERY_DOC);
|
||||
mockExchangeCode.mockResolvedValue({ access_token: 'tok', id_token: 'fake.id.token', _ok: true, _status: 200 });
|
||||
mockVerifyIdToken.mockResolvedValue({ ok: true, claims: { sub } });
|
||||
mockGetUserInfo.mockResolvedValue({ sub, email, name: 'Binding User' });
|
||||
|
||||
const login = await request(app).get('/api/auth/oidc/login');
|
||||
const state = new URL(login.headers.location!).searchParams.get('state')!;
|
||||
const cb = await request(app)
|
||||
.get(`/api/auth/oidc/callback?code=anycode&state=${state}`)
|
||||
.set('Cookie', `trek_oidc_state=${state}`);
|
||||
|
||||
return {
|
||||
code: new URL(cb.headers.location!, 'http://localhost').searchParams.get('oidc_code')!,
|
||||
binding: readCookie(cb, 'trek_oidc_exchange'),
|
||||
response: cb,
|
||||
};
|
||||
}
|
||||
|
||||
it('OIDC-018: /callback sets the binding as an httpOnly cookie that dies with the code', async () => {
|
||||
const { response } = await loginUpToCallback('sub-bind-1', 'bind1@example.com');
|
||||
|
||||
const raw = (response.headers['set-cookie'] as unknown as string[]).find((c) => c.startsWith('trek_oidc_exchange='))!;
|
||||
expect(raw).toMatch(/HttpOnly/i);
|
||||
expect(raw).toMatch(/SameSite=Lax/i);
|
||||
// Same minute the code lives; a cookie that outlived it could only ever fail.
|
||||
expect(raw).toContain('Max-Age=60;');
|
||||
});
|
||||
|
||||
it('OIDC-019: a code captured in browser A is refused in browser B', async () => {
|
||||
// Browser A finishes the handshake. B is anything that only got to see the
|
||||
// URL: history on a shared machine, a referrer, a proxy log, a screen share.
|
||||
const a = await loginUpToCallback('sub-bind-2', 'bind2@example.com');
|
||||
|
||||
const inB = await request(app).get(`/api/auth/oidc/exchange?code=${a.code}`);
|
||||
|
||||
expect(inB.status).toBe(400);
|
||||
expect(readCookie(inB, 'trek_session')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('OIDC-020: the failed attempt burns the code, so a stolen code is never redeemable', async () => {
|
||||
const a = await loginUpToCallback('sub-bind-3', 'bind3@example.com');
|
||||
|
||||
await request(app).get(`/api/auth/oidc/exchange?code=${a.code}`);
|
||||
// A retries with the right cookie and is refused too. Deliberate: a code that
|
||||
// someone else has already presented is treated as spent, and A only has to
|
||||
// log in again.
|
||||
const retryInA = await request(app)
|
||||
.get(`/api/auth/oidc/exchange?code=${a.code}`)
|
||||
.set('Cookie', `trek_oidc_exchange=${a.binding}`);
|
||||
|
||||
expect(retryInA.status).toBe(400);
|
||||
expect(readCookie(retryInA, 'trek_session')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('OIDC-021: an attacker-minted code cannot be forced onto a victim browser', async () => {
|
||||
// The session-fixation direction: the attacker completes a real login as
|
||||
// themselves, then makes the victim's browser call /exchange with that code.
|
||||
// If it worked, the victim would be silently signed into the attacker's
|
||||
// account and would write their next trip into it.
|
||||
const attacker = await loginUpToCallback('sub-attacker', 'attacker@example.com');
|
||||
const victimHasHisOwnFlow = await loginUpToCallback('sub-victim', 'victim@example.com');
|
||||
|
||||
const forced = await request(app)
|
||||
.get(`/api/auth/oidc/exchange?code=${attacker.code}`)
|
||||
// The victim's browser carries its own binding, never the attacker's.
|
||||
.set('Cookie', `trek_oidc_exchange=${victimHasHisOwnFlow.binding}`);
|
||||
|
||||
expect(forced.status).toBe(400);
|
||||
expect(readCookie(forced, 'trek_session')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('OIDC-022: /exchange clears the binding cookie on the way out', async () => {
|
||||
const a = await loginUpToCallback('sub-bind-4', 'bind4@example.com');
|
||||
|
||||
const ok = await request(app)
|
||||
.get(`/api/auth/oidc/exchange?code=${a.code}`)
|
||||
.set('Cookie', `trek_oidc_exchange=${a.binding}`);
|
||||
|
||||
expect(ok.status).toBe(200);
|
||||
const cleared = (ok.headers['set-cookie'] as unknown as string[]).find((c) => c.startsWith('trek_oidc_exchange='))!;
|
||||
expect(cleared).toContain('Expires=Thu, 01 Jan 1970');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user