fix(core): Don't allow creating more projects than allowed by exploiting a race condition (#15218)

This commit is contained in:
Danny Martini
2025-05-27 15:50:44 +02:00
committed by GitHub
parent f062e260f4
commit 6466e76c06
7 changed files with 137 additions and 27 deletions

View File

@@ -1,3 +1,4 @@
import { GlobalConfig } from '@n8n/config';
import type { Project } from '@n8n/db';
import { FolderRepository } from '@n8n/db';
import { ProjectRelationRepository } from '@n8n/db';
@@ -432,6 +433,34 @@ describe('POST /projects/', () => {
expect(await Container.get(ProjectRepository).count({ where: { type: 'team' } })).toBe(2);
});
const globalConfig = Container.get(GlobalConfig);
// Preventing this relies on transactions and we can't use them with the
// sqlite legacy driver due to data loss risks.
if (!globalConfig.database.isLegacySqlite) {
test('should respect the quota when trying to create multiple projects in parallel (no race conditions)', async () => {
expect(await Container.get(ProjectRepository).count({ where: { type: 'team' } })).toBe(0);
testServer.license.setQuota('quota:maxTeamProjects', 3);
const ownerUser = await createOwner();
const ownerAgent = testServer.authAgentFor(ownerUser);
await expect(
Container.get(ProjectRepository).count({ where: { type: 'team' } }),
).resolves.toBe(0);
await Promise.all([
ownerAgent.post('/projects/').send({ name: 'Test Team Project 1' }),
ownerAgent.post('/projects/').send({ name: 'Test Team Project 2' }),
ownerAgent.post('/projects/').send({ name: 'Test Team Project 3' }),
ownerAgent.post('/projects/').send({ name: 'Test Team Project 4' }),
ownerAgent.post('/projects/').send({ name: 'Test Team Project 5' }),
ownerAgent.post('/projects/').send({ name: 'Test Team Project 6' }),
]);
await expect(
Container.get(ProjectRepository).count({ where: { type: 'team' } }),
).resolves.toBe(3);
});
}
});
describe('PATCH /projects/:projectId', () => {