diff --git a/back-end/libs/common/src/blacklist/blacklist.service.spec.ts b/back-end/libs/common/src/blacklist/blacklist.service.spec.ts index 4dbf0776f0..6c1fef753c 100644 --- a/back-end/libs/common/src/blacklist/blacklist.service.spec.ts +++ b/back-end/libs/common/src/blacklist/blacklist.service.spec.ts @@ -1,3 +1,5 @@ +import { createHash } from 'crypto'; + import { Test, TestingModule } from '@nestjs/testing'; import { ConfigService } from '@nestjs/config'; import { Redis } from 'ioredis'; @@ -6,6 +8,9 @@ import { mockDeep } from 'jest-mock-extended'; import { BlacklistService } from '@app/common'; import { REDIS_CLIENT } from '@app/common'; +const hashKey = (jwt: string) => + `blacklisted:token:${createHash('sha256').update(jwt).digest('hex')}`; + describe('BlacklistService', () => { let service: BlacklistService; const configService = mockDeep(); @@ -43,7 +48,13 @@ describe('BlacklistService', () => { await service.blacklistToken(jwt); - expect(client.set).toHaveBeenCalledWith(jwt, 'blacklisted', 'EX', expirationSeconds); + expect(client.set).toHaveBeenCalledWith( + hashKey(jwt), + 'blacklisted', + 'EX', + expirationSeconds, + ); + expect(client.set).not.toHaveBeenCalledWith(jwt, expect.anything(), 'EX', expect.anything()); }); }); @@ -69,12 +80,19 @@ describe('BlacklistService', () => { it('should return true if the token is blacklisted', async () => { const jwt = 'testToken'; - client.get.mockResolvedValue('blacklisted'); + client.get.mockImplementation(async key => (key === hashKey(jwt) ? 'blacklisted' : null)); const result = await service.isTokenBlacklisted(jwt); expect(result).toBe(true); - expect(client.get).toHaveBeenCalledWith(jwt); + }); + + it('should return true for a legacy raw-JWT blacklist entry', async () => { + const jwt = 'testToken'; + + client.get.mockImplementation(async key => (key === jwt ? 'blacklisted' : null)); + + await expect(service.isTokenBlacklisted(jwt)).resolves.toBe(true); }); it('should return false if the token is not blacklisted', async () => { @@ -85,7 +103,13 @@ describe('BlacklistService', () => { const result = await service.isTokenBlacklisted(jwt); expect(result).toBe(false); - expect(client.get).toHaveBeenCalledWith(jwt); + expect(client.get).toHaveBeenCalledWith(hashKey(jwt)); + }); + + it('should propagate Redis errors so callers fail closed', async () => { + client.get.mockRejectedValue(new Error('redis down')); + + await expect(service.isTokenBlacklisted('testToken')).rejects.toThrow('redis down'); }); it('should reject a JWT issued before the user was removed', async () => { diff --git a/back-end/libs/common/src/blacklist/blacklist.service.ts b/back-end/libs/common/src/blacklist/blacklist.service.ts index ec9e1f5d55..1ff53bb589 100644 --- a/back-end/libs/common/src/blacklist/blacklist.service.ts +++ b/back-end/libs/common/src/blacklist/blacklist.service.ts @@ -1,6 +1,8 @@ import { Inject, Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; +import { createHash } from 'crypto'; + import { Redis } from 'ioredis'; import { REDIS_CLIENT } from '../redis/redis-client.module'; @@ -9,6 +11,7 @@ import { REDIS_CLIENT } from '../redis/redis-client.module'; export class BlacklistService { private BLACKLISTED = 'blacklisted'; private USER_INVALID_BEFORE_PREFIX = 'blacklisted:user:'; + private TOKEN_PREFIX = 'blacklisted:token:'; // The shared connection's lifecycle (including closing it on shutdown) is // owned by RedisClientModule, not this service. @@ -19,7 +22,7 @@ export class BlacklistService { async blacklistToken(jwt: string | null) { if (jwt !== null) { - await this.client.set(jwt, this.BLACKLISTED, 'EX', this.getJwtExpirationSeconds()); + await this.client.set(this.getTokenKey(jwt), this.BLACKLISTED, 'EX', this.getJwtExpirationSeconds()); } } @@ -38,8 +41,12 @@ export class BlacklistService { } async isTokenBlacklisted(jwt: string) { - const data = await this.client.get(jwt); - if (data === this.BLACKLISTED) { + const [hashed, legacy] = await Promise.all([ + this.client.get(this.getTokenKey(jwt)), + // TODO: remove once all legacy raw-JWT keys have expired (JWT_EXPIRATION after deploy). + this.client.get(jwt), + ]); + if (hashed === this.BLACKLISTED || legacy === this.BLACKLISTED) { return true; } @@ -66,6 +73,11 @@ export class BlacklistService { return Number(expirationDays) * 24 * 60 * 60; } + /* Only a one-way fingerprint is stored so Redis contents can never be replayed as credentials. */ + private getTokenKey(jwt: string): string { + return `${this.TOKEN_PREFIX}${createHash('sha256').update(jwt).digest('hex')}`; + } + private getUserInvalidationKey(userId: number): string { return `${this.USER_INVALID_BEFORE_PREFIX}${userId}`; }