Merge pull request #4450 from NoiceHax/fix/issue-4443
fix(2fa): gate two-factor auth when the encryption key is missing
This commit is contained in:
12 files changed
+183
-9
No files matched your search
@@ -8,6 +8,7 @@ const mocks = vi.hoisted(() => ({
|
||||
upsert: vi.fn(),
|
||||
generateTotpSecret: vi.fn(),
|
||||
encryptSecret: vi.fn(),
|
||||
isTwoFactorConfigured: vi.fn(),
|
||||
generateOtpAuthUri: vi.fn(),
|
||||
generateQrCodeDataUrl: vi.fn(),
|
||||
}));
|
||||
@@ -33,6 +34,7 @@ vi.mock('@/lib/prisma', () => ({
|
||||
|
||||
vi.mock('@/lib/two-factor/crypto', () => ({
|
||||
encryptSecret: mocks.encryptSecret,
|
||||
isTwoFactorConfigured: mocks.isTwoFactorConfigured,
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/two-factor/totp', () => ({
|
||||
@@ -48,9 +50,11 @@ beforeEach(() => {
|
||||
mocks.upsert.mockReset();
|
||||
mocks.generateTotpSecret.mockReset();
|
||||
mocks.encryptSecret.mockReset();
|
||||
mocks.isTwoFactorConfigured.mockReset();
|
||||
mocks.generateOtpAuthUri.mockReset();
|
||||
mocks.generateQrCodeDataUrl.mockReset();
|
||||
|
||||
mocks.isTwoFactorConfigured.mockReturnValue(true);
|
||||
mocks.parseRequest.mockResolvedValue({
|
||||
auth: { user: { id: 'user-1' } },
|
||||
error: undefined,
|
||||
@@ -82,6 +86,22 @@ test('POST creates a pending 2FA setup and returns the manual key and QR data',
|
||||
expect(response.status).toBe(200);
|
||||
});
|
||||
|
||||
test('POST reports a configuration error when the encryption key is missing', async () => {
|
||||
mocks.isTwoFactorConfigured.mockReturnValue(false);
|
||||
|
||||
const response = await POST(
|
||||
new Request('http://localhost/api/2fa/setup/initiate', { method: 'POST' }),
|
||||
);
|
||||
|
||||
expect(mocks.upsert).not.toHaveBeenCalled();
|
||||
await expect(response.json()).resolves.toMatchObject({
|
||||
error: {
|
||||
code: 'two-factor-error-not-configured',
|
||||
},
|
||||
});
|
||||
expect(response.status).toBe(503);
|
||||
});
|
||||
|
||||
test('POST rejects setup when 2FA is already enabled for the user', async () => {
|
||||
mocks.findUnique.mockResolvedValue({ userId: 'user-1', isEnabled: true });
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { badRequest, json, notFound } from '@/lib/response';
|
||||
import { encryptSecret } from '@/lib/two-factor/crypto';
|
||||
import { badRequest, json, notFound, serviceUnavailable } from '@/lib/response';
|
||||
import { encryptSecret, isTwoFactorConfigured } from '@/lib/two-factor/crypto';
|
||||
import {
|
||||
generateOtpAuthUri,
|
||||
generateQrCodeDataUrl,
|
||||
@@ -20,6 +20,14 @@ export async function POST(request: Request) {
|
||||
return error();
|
||||
}
|
||||
|
||||
// Secrets cannot be stored without an encryption key
|
||||
if (!isTwoFactorConfigured()) {
|
||||
return serviceUnavailable({
|
||||
code: 'two-factor-error-not-configured',
|
||||
message: 'TWO_FACTOR_ENCRYPTION_KEY is missing or invalid',
|
||||
});
|
||||
}
|
||||
|
||||
const userId = auth.user.id;
|
||||
const user = await getUser(userId);
|
||||
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import { beforeEach, expect, test, vi } from 'vitest';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { GET } from './route';
|
||||
|
||||
const KEY = 'a'.repeat(64);
|
||||
|
||||
vi.mock('@/lib/request', () => ({
|
||||
parseRequest: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
default: {
|
||||
client: {
|
||||
twoFactorAuth: {
|
||||
findUnique: vi.fn(),
|
||||
},
|
||||
appSetting: {
|
||||
findUnique: vi.fn(),
|
||||
},
|
||||
user: {
|
||||
findUnique: vi.fn(),
|
||||
},
|
||||
teamUser: {
|
||||
findMany: vi.fn(),
|
||||
},
|
||||
team: {
|
||||
findMany: vi.fn(),
|
||||
},
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
const parseRequestMock = vi.mocked(parseRequest);
|
||||
const prismaMock = vi.mocked(prisma, true);
|
||||
|
||||
beforeEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
parseRequestMock.mockReset();
|
||||
prismaMock.client.twoFactorAuth.findUnique.mockReset();
|
||||
prismaMock.client.appSetting.findUnique.mockReset();
|
||||
prismaMock.client.user.findUnique.mockReset();
|
||||
prismaMock.client.teamUser.findMany.mockReset();
|
||||
prismaMock.client.team.findMany.mockReset();
|
||||
|
||||
parseRequestMock.mockResolvedValue({
|
||||
auth: {
|
||||
user: {
|
||||
id: 'user-1',
|
||||
},
|
||||
},
|
||||
error: undefined,
|
||||
});
|
||||
prismaMock.client.twoFactorAuth.findUnique.mockResolvedValue(null as any);
|
||||
prismaMock.client.appSetting.findUnique.mockResolvedValue({
|
||||
key: 'twoFactorRequiredGlobal',
|
||||
value: 'true',
|
||||
} as any);
|
||||
prismaMock.client.user.findUnique.mockResolvedValue({ twoFactorRequired: false } as any);
|
||||
prismaMock.client.teamUser.findMany.mockResolvedValue([] as any);
|
||||
prismaMock.client.team.findMany.mockResolvedValue([] as any);
|
||||
});
|
||||
|
||||
test('GET requires 2FA when it is globally enabled', async () => {
|
||||
vi.stubEnv('TWO_FACTOR_ENCRYPTION_KEY', KEY);
|
||||
|
||||
const response = await GET(new Request('http://localhost/api/2fa/status'));
|
||||
|
||||
await expect(response.json()).resolves.toEqual({
|
||||
isEnabled: false,
|
||||
isRequired: true,
|
||||
requiredReason: 'global',
|
||||
});
|
||||
expect(response.status).toBe(200);
|
||||
});
|
||||
|
||||
test('GET does not require 2FA when the encryption key is missing', async () => {
|
||||
vi.stubEnv('TWO_FACTOR_ENCRYPTION_KEY', '');
|
||||
|
||||
const response = await GET(new Request('http://localhost/api/2fa/status'));
|
||||
|
||||
expect(prismaMock.client.appSetting.findUnique).not.toHaveBeenCalled();
|
||||
await expect(response.json()).resolves.toEqual({
|
||||
isEnabled: false,
|
||||
isRequired: false,
|
||||
requiredReason: null,
|
||||
});
|
||||
expect(response.status).toBe(200);
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import prisma from '@/lib/prisma';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { json } from '@/lib/response';
|
||||
import { isTwoFactorConfigured } from '@/lib/two-factor/crypto';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { auth, error } = await parseRequest(request);
|
||||
@@ -18,6 +19,11 @@ export async function GET(request: Request) {
|
||||
const twoFactor = await prisma.client.twoFactorAuth.findUnique({ where: { userId } });
|
||||
const isEnabled = twoFactor?.isEnabled ?? false;
|
||||
|
||||
// 2FA cannot be set up without an encryption key, so it is never required
|
||||
if (!isTwoFactorConfigured()) {
|
||||
return json({ isEnabled, isRequired: false, requiredReason: null });
|
||||
}
|
||||
|
||||
// Globally required
|
||||
const globalSetting = await prisma.client.appSetting.findUnique({
|
||||
where: { key: 'twoFactorRequiredGlobal' },
|
||||
|
||||
Reference in new issue
Block a user