📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Return a client error when a stored key cannot be decrypted
Blue Mouse2df9c7e4aefb
diff --git a/backend/apps/cloud/src/api-key/api-key.service.ts b/backend/apps/cloud/src/api-key/api-key.service.tsindex b6866fa7a..c04aeceed 100644--- a/backend/apps/cloud/src/api-key/api-key.service.ts+++ b/backend/apps/cloud/src/api-key/api-key.service.ts@@ -1,5 +1,6 @@import {BadRequestException,+ ConflictException,ForbiddenException,Injectable,NotFoundException,@@ -141,11 +142,15 @@ export class ApiKeyService {async reveal(userId: string, id: string) {const key = await this.get(userId, id)- const secret =- id === 'legacy'- ? (await this.store.userById(userId)).apiKey- : decryptApiKey(key.encryptedKey, userId, id)- return { secret }+ if (id === 'legacy')+ return { secret: (await this.store.userById(userId)).apiKey }+ try {+ return { secret: decryptApiKey(key.encryptedKey, userId, id) }+ } catch {+ throw new ConflictException(+ 'Unable to decrypt this API key. Please rotate the key.',+ )+ }}private async validate(userId: string, dto: SaveApiKeyDto) {diff --git a/backend/apps/cloud/src/api-key/api-key.spec.ts b/backend/apps/cloud/src/api-key/api-key.spec.tsindex 9e86911a6..694ed5b30 100644--- a/backend/apps/cloud/src/api-key/api-key.spec.ts+++ b/backend/apps/cloud/src/api-key/api-key.spec.ts@@ -1,7 +1,12 @@import { AuthGuard } from '@nestjs/passport'import { ApiKeyStrategy } from '../auth/strategies/api-key.strategy'import 'reflect-metadata'-import { ForbiddenException, UnauthorizedException } from '@nestjs/common'+import {+ ConflictException,+ ForbiddenException,+ NotFoundException,+ UnauthorizedException,+} from '@nestjs/common'import { Reflector } from '@nestjs/core'import { validate } from 'class-validator'import { plainToInstance } from 'class-transformer'@@ -167,6 +172,39 @@ describe('scoped API keys', () => {await expect(service.remove('other', result.key.id)).rejects.toThrow()})+ it('asks the owner to rotate a key when decryption fails', async () => {+ const { service, records } = setup()+ const { key } = await service.create(userId, input())+ records.get(key.id).encryptedKey = 'invalid-ciphertext'++ await expect(service.reveal(userId, key.id)).rejects.toThrow(+ new ConflictException(+ 'Unable to decrypt this API key. Please rotate the key.',+ ),+ )+ await expect(service.reveal('other', key.id)).rejects.toThrow(+ NotFoundException,+ )++ const rotated = await service.rotate(userId, key.id)+ await expect(service.reveal(userId, key.id)).resolves.toEqual({+ secret: rotated.secret,+ })+ })++ it('reveals legacy keys without an encryption secret', async () => {+ const { service } = setup()+ const base = process.env.SECRET_KEY_BASE+ delete process.env.SECRET_KEY_BASE+ try {+ await expect(service.reveal(userId, 'legacy')).resolves.toEqual({+ secret: 'old-uuid-key',+ })+ } finally {+ process.env.SECRET_KEY_BASE = base+ }+ })+it('rotates and revokes immediately while preserving scope and creation date', async () => {const { service, records } = setup()const first = await service.create(userId, input())diff --git a/backend/apps/community/src/api-key/api-key.service.ts b/backend/apps/community/src/api-key/api-key.service.tsindex b6866fa7a..c04aeceed 100644--- a/backend/apps/community/src/api-key/api-key.service.ts+++ b/backend/apps/community/src/api-key/api-key.service.ts@@ -1,5 +1,6 @@import {BadRequestException,+ ConflictException,ForbiddenException,Injectable,NotFoundException,@@ -141,11 +142,15 @@ export class ApiKeyService {async reveal(userId: string, id: string) {const key = await this.get(userId, id)- const secret =- id === 'legacy'- ? (await this.store.userById(userId)).apiKey- : decryptApiKey(key.encryptedKey, userId, id)- return { secret }+ if (id === 'legacy')+ return { secret: (await this.store.userById(userId)).apiKey }+ try {+ return { secret: decryptApiKey(key.encryptedKey, userId, id) }+ } catch {+ throw new ConflictException(+ 'Unable to decrypt this API key. Please rotate the key.',+ )+ }}private async validate(userId: string, dto: SaveApiKeyDto) {diff --git a/backend/apps/community/src/api-key/api-key.spec.ts b/backend/apps/community/src/api-key/api-key.spec.tsindex 5f19c8e81..8b38ec3f8 100644--- a/backend/apps/community/src/api-key/api-key.spec.ts+++ b/backend/apps/community/src/api-key/api-key.spec.ts@@ -1,7 +1,12 @@import { AuthGuard } from '@nestjs/passport'import { ApiKeyStrategy } from '../auth/strategies/api-key.strategy'import 'reflect-metadata'-import { ForbiddenException, UnauthorizedException } from '@nestjs/common'+import {+ ConflictException,+ ForbiddenException,+ NotFoundException,+ UnauthorizedException,+} from '@nestjs/common'import { Reflector } from '@nestjs/core'import { validate } from 'class-validator'import { plainToInstance } from 'class-transformer'@@ -168,6 +173,39 @@ describe('scoped API keys', () => {await expect(service.remove('other', result.key.id)).rejects.toThrow()})+ it('asks the owner to rotate a key when decryption fails', async () => {+ const { service, records } = setup()+ const { key } = await service.create(userId, input())+ records.get(key.id).encryptedKey = 'invalid-ciphertext'++ await expect(service.reveal(userId, key.id)).rejects.toThrow(+ new ConflictException(+ 'Unable to decrypt this API key. Please rotate the key.',+ ),+ )+ await expect(service.reveal('other', key.id)).rejects.toThrow(+ NotFoundException,+ )++ const rotated = await service.rotate(userId, key.id)+ await expect(service.reveal(userId, key.id)).resolves.toEqual({+ secret: rotated.secret,+ })+ })++ it('reveals legacy keys without an encryption secret', async () => {+ const { service } = setup()+ const base = process.env.SECRET_KEY_BASE+ delete process.env.SECRET_KEY_BASE+ try {+ await expect(service.reveal(userId, 'legacy')).resolves.toEqual({+ secret: 'old-uuid-key',+ })+ } finally {+ process.env.SECRET_KEY_BASE = base+ }+ })+it('rotates and revokes immediately while preserving scope and creation date', async () => {const { service, records } = setup()const first = await service.create(userId, input())