Files
TREK/server/tests/unit/nest/session-renewal.interceptor.test.ts
T
Maurice c7de823c78 fix(auth): renew a session cookie only after it verifies on its own
SessionRenewalInterceptor took req.user as proof that the trek_session cookie
had been verified. On a route with its own guard chain that does not hold:
ApiTokenGuard authenticates a public API key and sets req.user, and the cookie
sent beside it is never checked. Past half its lifetime such a cookie was
decoded without its signature and renewed, so a holder of a read-only key
could come away with a signed session of the key's owner.

The interceptor now runs the cookie through the guards' own check (signature,
expiry, password version, active session) before it renews, requires it to
name the request's user, and renews nothing on a request a public API key
authenticated.
2026-11-22 18:15:51 +01:00

275 lines
12 KiB
TypeScript

/**
* Unit tests for SessionRenewalInterceptor (#1927) — sliding renewal of the
* trek_session cookie once a cookie-authenticated token is past half of its
* lifetime. Covers the renew path (fresh token + cookie semantics per the
* `remember` claim) and every skip condition: young token, bearer-only request,
* unauthenticated request, purpose token, id mismatch, malformed claims,
* headers already sent, non-http context, a cookie the session check refuses
* and a request a public API key authenticated.
*/
import type { AuthService } from '../../../src/nest/auth/auth.service';
import { SessionRenewalInterceptor } from '../../../src/nest/auth/session-renewal.interceptor';
import type { SessionsService } from '../../../src/nest/sessions/sessions.service';
import type { CallHandler, ExecutionContext } from '@nestjs/common';
import jwt from 'jsonwebtoken';
import { of, lastValueFrom } from 'rxjs';
import { describe, it, expect, vi } from 'vitest';
const SECRET = 'test-jwt-secret-for-trek-testing-only';
/** Sign a session token whose lifetime is `lifetime` seconds, `consumed` of which have already passed. */
function token(payload: Record<string, unknown>, lifetime: number, consumed: number): string {
const iat = Math.floor(Date.now() / 1000) - consumed;
// jsonwebtoken computes exp = iat + expiresIn when iat is supplied in the payload.
return jwt.sign({ ...payload, iat }, SECRET, { expiresIn: lifetime, algorithm: 'HS256' });
}
function makeRes(headersSent = false) {
return { headersSent, cookie: vi.fn() };
}
type ReqShape = {
cookies?: Record<string, string>;
user?: { id: number };
apiToken?: { scopes: string[] | null };
headers?: Record<string, string>;
secure?: boolean;
};
function ctx(req: ReqShape, res: ReturnType<typeof makeRes>, type = 'http'): ExecutionContext {
return {
getType: () => type,
// Express always hands over a headers object; the shapes below leave it out.
switchToHttp: () => ({ getRequest: () => ({ headers: {}, ...req }), getResponse: () => res }),
} as unknown as ExecutionContext;
}
function handler(): CallHandler & { handle: ReturnType<typeof vi.fn> } {
return { handle: vi.fn(() => of('ok')) };
}
/**
* `verified` is what the session check answers for the cookie: the user its
* token names when it is a live session, null when it is not one.
*/
function makeInterceptor(renewed: string | null = 'renewed.jwt', verified: { id: number } | null = { id: 1 }) {
const renew = vi.fn().mockResolvedValue(renewed);
const verifyJwtToken = vi.fn().mockResolvedValue(verified);
const interceptor = new SessionRenewalInterceptor(
{ renew } as unknown as SessionsService,
{ verifyJwtToken } as unknown as AuthService,
);
return { interceptor, renew, verifyJwtToken };
}
async function run(
interceptor: SessionRenewalInterceptor,
req: ReqShape,
res: ReturnType<typeof makeRes>,
type = 'http',
) {
const h = handler();
const out = await lastValueFrom(await interceptor.intercept(ctx(req, res, type), h));
expect(out).toBe('ok');
expect(h.handle).toHaveBeenCalled();
}
describe('SessionRenewalInterceptor', () => {
it('SES-RENEW-001: does not renew a token under half of its lifetime, nor look it up', async () => {
const { interceptor, renew, verifyJwtToken } = makeInterceptor();
const res = makeRes();
const t = token({ id: 1, pv: 0 }, 86400, 1000); // ~1% consumed
await run(interceptor, { cookies: { trek_session: t }, user: { id: 1 } }, res);
expect(renew).not.toHaveBeenCalled();
expect(verifyJwtToken).not.toHaveBeenCalled();
expect(res.cookie).not.toHaveBeenCalled();
});
it('SES-RENEW-002: renews a token past half of its lifetime, copying pv from the old token', async () => {
const { interceptor, renew } = makeInterceptor();
const res = makeRes();
const t = token({ id: 1, pv: 3 }, 86400, 60000); // ~70% consumed
await run(interceptor, { cookies: { trek_session: t }, user: { id: 1 } }, res);
expect(renew).toHaveBeenCalledWith(
{ id: 1, pv: 3, remember: undefined, jti: undefined, token: t },
{ userAgent: null },
);
expect(res.cookie).toHaveBeenCalledWith(
'trek_session',
'renewed.jwt',
expect.objectContaining({ maxAge: 86400000, httpOnly: true }),
);
});
it('SES-RENEW-003: a remember:true claim renews with the long persistent cookie', async () => {
const { interceptor, renew } = makeInterceptor();
const res = makeRes();
const t = token({ id: 1, pv: 0, remember: true }, 2592000, 1600000); // ~62% consumed
await run(interceptor, { cookies: { trek_session: t }, user: { id: 1 } }, res);
expect(renew).toHaveBeenCalledWith({ id: 1, pv: 0, remember: true, jti: undefined, token: t }, { userAgent: null });
expect(res.cookie).toHaveBeenCalledWith(
'trek_session',
'renewed.jwt',
expect.objectContaining({ maxAge: 2592000000 }),
);
});
it('SES-RENEW-004: a remember:false claim renews as a browser-session cookie (no maxAge)', async () => {
const { interceptor, renew } = makeInterceptor();
const res = makeRes();
const t = token({ id: 1, pv: 0, remember: false }, 86400, 60000);
await run(interceptor, { cookies: { trek_session: t }, user: { id: 1 } }, res);
expect(renew).toHaveBeenCalledWith(
{ id: 1, pv: 0, remember: false, jti: undefined, token: t },
{ userAgent: null },
);
const options = res.cookie.mock.calls[0][2];
expect(options).not.toHaveProperty('maxAge');
});
it('SES-RENEW-005: a claimless legacy token renews with the historical default cookie', async () => {
const { interceptor, renew } = makeInterceptor();
const res = makeRes();
const t = token({ id: 1, pv: 0 }, 86400, 60000);
await run(interceptor, { cookies: { trek_session: t }, user: { id: 1 } }, res);
expect(renew).toHaveBeenCalledWith(
{ id: 1, pv: 0, remember: undefined, jti: undefined, token: t },
{ userAgent: null },
);
expect(res.cookie).toHaveBeenCalledWith(
'trek_session',
'renewed.jwt',
expect.objectContaining({ maxAge: 86400000 }),
);
});
it('SES-RENEW-006: never renews a Bearer-only request (no session cookie)', async () => {
const { interceptor, renew } = makeInterceptor();
const res = makeRes();
await run(interceptor, { headers: { authorization: 'Bearer x' }, user: { id: 1 } }, res);
expect(renew).not.toHaveBeenCalled();
expect(res.cookie).not.toHaveBeenCalled();
});
it('SES-RENEW-007: never renews an unauthenticated (public-route) request', async () => {
const { interceptor, renew } = makeInterceptor();
const res = makeRes();
const t = token({ id: 1, pv: 0 }, 86400, 60000);
await run(interceptor, { cookies: { trek_session: t } }, res);
expect(renew).not.toHaveBeenCalled();
});
it('SES-RENEW-008: never renews a purpose-scoped token or a cookie whose id differs from req.user', async () => {
const { interceptor, renew } = makeInterceptor();
const purposeRes = makeRes();
const purposeToken = token({ id: 1, pv: 0, purpose: 'mfa_login' }, 86400, 60000);
await run(interceptor, { cookies: { trek_session: purposeToken }, user: { id: 1 } }, purposeRes);
const mismatchRes = makeRes();
const otherUsers = token({ id: 2, pv: 0 }, 86400, 60000);
await run(interceptor, { cookies: { trek_session: otherUsers }, user: { id: 1 } }, mismatchRes);
expect(renew).not.toHaveBeenCalled();
expect(purposeRes.cookie).not.toHaveBeenCalled();
expect(mismatchRes.cookie).not.toHaveBeenCalled();
});
it('SES-RENEW-009: skips when headers were already sent or the cookie is not a JWT', async () => {
const { interceptor, renew } = makeInterceptor();
const sentRes = makeRes(true);
const t = token({ id: 1, pv: 0 }, 86400, 60000);
await run(interceptor, { cookies: { trek_session: t }, user: { id: 1 } }, sentRes);
const garbageRes = makeRes();
await run(interceptor, { cookies: { trek_session: 'not-a-jwt' }, user: { id: 1 } }, garbageRes);
expect(renew).not.toHaveBeenCalled();
expect(sentRes.cookie).not.toHaveBeenCalled();
expect(garbageRes.cookie).not.toHaveBeenCalled();
});
it('SES-RENEW-010: skips tokens with missing or inverted iat/exp claims', async () => {
const { interceptor, renew } = makeInterceptor();
const res = makeRes();
// Hand-built token without exp (noTimestamp keeps iat but expiresIn omitted).
const noExp = jwt.sign({ id: 1, pv: 0 }, SECRET, { algorithm: 'HS256' });
await run(interceptor, { cookies: { trek_session: noExp }, user: { id: 1 } }, res);
expect(renew).not.toHaveBeenCalled();
});
it('SES-RENEW-011: ignores non-http execution contexts', async () => {
const { interceptor, renew } = makeInterceptor();
const res = makeRes();
const t = token({ id: 1, pv: 0 }, 86400, 60000);
await run(interceptor, { cookies: { trek_session: t }, user: { id: 1 } }, res, 'ws');
expect(renew).not.toHaveBeenCalled();
});
it('SES-RENEW-012: a tracked token renews under its own session id, with the device', async () => {
const { interceptor, renew } = makeInterceptor();
const res = makeRes();
const t = jwt.sign({ id: 1, pv: 0, iat: Math.floor(Date.now() / 1000) - 60000 }, SECRET, {
expiresIn: 86400,
algorithm: 'HS256',
jwtid: 'sid-1',
});
await run(
interceptor,
{ cookies: { trek_session: t }, user: { id: 1 }, headers: { 'user-agent': 'Firefox/130' } },
res,
);
expect(renew).toHaveBeenCalledWith(
{ id: 1, pv: 0, remember: undefined, jti: 'sid-1', token: t },
{ userAgent: 'Firefox/130' },
);
expect(res.cookie).toHaveBeenCalledWith(
'trek_session',
'renewed.jwt',
expect.objectContaining({ maxAge: 86400000 }),
);
});
it('SES-RENEW-013: sets no cookie when the session ended before it could be renewed', async () => {
const { interceptor, renew } = makeInterceptor(null);
const res = makeRes();
const t = token({ id: 1, pv: 0 }, 86400, 60000);
await run(interceptor, { cookies: { trek_session: t }, user: { id: 1 } }, res);
expect(renew).toHaveBeenCalled();
expect(res.cookie).not.toHaveBeenCalled();
});
it('SES-RENEW-014: renews no cookie the session check refuses, whoever req.user is', async () => {
// What a route with its own guard chain can see: ApiTokenGuard set req.user from
// a key, and the cookie beside it is unsigned, past half its life and names the
// key's owner without a session id. Decoding it alone would re-sign it.
const { interceptor, renew, verifyJwtToken } = makeInterceptor('renewed.jwt', null);
const res = makeRes();
const iat = Math.floor(Date.now() / 1000) - 60000;
const forged = jwt.sign({ id: 1, pv: 0, iat }, 'not-the-server-secret', { expiresIn: 86400, algorithm: 'HS256' });
await run(interceptor, { cookies: { trek_session: forged }, user: { id: 1 } }, res);
expect(verifyJwtToken).toHaveBeenCalledWith(forged);
expect(renew).not.toHaveBeenCalled();
expect(res.cookie).not.toHaveBeenCalled();
});
it('SES-RENEW-015: renews no cookie whose session belongs to another user than the request', async () => {
const { interceptor, renew } = makeInterceptor('renewed.jwt', { id: 2 });
const res = makeRes();
const t = token({ id: 1, pv: 0 }, 86400, 60000);
await run(interceptor, { cookies: { trek_session: t }, user: { id: 1 } }, res);
expect(renew).not.toHaveBeenCalled();
expect(res.cookie).not.toHaveBeenCalled();
});
it('SES-RENEW-016: a request a public API key authenticated renews nothing, even beside a valid cookie', async () => {
const { interceptor, renew, verifyJwtToken } = makeInterceptor();
const res = makeRes();
const t = token({ id: 1, pv: 0 }, 86400, 60000);
await run(interceptor, { cookies: { trek_session: t }, user: { id: 1 }, apiToken: { scopes: null } }, res);
expect(verifyJwtToken).not.toHaveBeenCalled();
expect(renew).not.toHaveBeenCalled();
expect(res.cookie).not.toHaveBeenCalled();
});
});