Files
n8n/packages/cli/test/integration/security-audit/credentials-risk-reporter.test.ts
alighasami 3d5eaf9445
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
first commit
2026-03-17 16:22:57 +03:30

259 lines
5.9 KiB
TypeScript

import { createActiveWorkflow, createWorkflowWithHistory, testDb } from '@n8n/backend-test-utils';
import type { SecurityConfig } from '@n8n/config';
import {
generateNanoId,
CredentialsRepository,
ExecutionDataRepository,
ExecutionRepository,
WorkflowRepository,
} from '@n8n/db';
import { Container } from '@n8n/di';
import { mock } from 'jest-mock-extended';
import { v4 as uuid } from 'uuid';
import { CREDENTIALS_REPORT } from '@/security-audit/constants';
import { SecurityAuditService } from '@/security-audit/security-audit.service';
import { getRiskSection } from './utils';
let securityAuditService: SecurityAuditService;
const securityConfig = mock<SecurityConfig>({ daysAbandonedWorkflow: 90 });
beforeAll(async () => {
await testDb.init();
securityAuditService = new SecurityAuditService(
Container.get(WorkflowRepository),
securityConfig,
);
});
beforeEach(async () => {
await testDb.truncate([
'WorkflowEntity',
'CredentialsEntity',
'ExecutionEntity',
'WorkflowHistory',
'WorkflowPublishHistory',
]);
});
afterAll(async () => {
await testDb.terminate();
});
test('should report credentials not in any use', async () => {
const credentialDetails = {
id: generateNanoId(),
name: 'My Slack Credential',
data: 'U2FsdGVkX18WjITBG4IDqrGB1xE/uzVNjtwDAG3lP7E=',
type: 'slackApi',
};
const workflowDetails = {
name: 'My Test Workflow',
connections: {},
nodeTypes: {},
nodes: [
{
id: uuid(),
name: 'My Node',
type: 'n8n-nodes-base.slack',
typeVersion: 1,
position: [0, 0] as [number, number],
parameters: {},
},
],
};
await Promise.all([
Container.get(CredentialsRepository).save(credentialDetails),
createWorkflowWithHistory(workflowDetails),
]);
const testAudit = await securityAuditService.run(['credentials']);
const section = getRiskSection(
testAudit,
CREDENTIALS_REPORT.RISK,
CREDENTIALS_REPORT.SECTIONS.CREDS_NOT_IN_ANY_USE,
);
expect(section.location).toHaveLength(1);
expect(section.location[0]).toMatchObject({
id: credentialDetails.id,
name: 'My Slack Credential',
});
});
test('should report credentials not in active use', async () => {
const credentialDetails = {
id: generateNanoId(),
name: 'My Slack Credential',
data: 'U2FsdGVkX18WjITBG4IDqrGB1xE/uzVNjtwDAG3lP7E=',
type: 'slackApi',
};
const credential = await Container.get(CredentialsRepository).save(credentialDetails);
const workflowDetails = {
name: 'My Test Workflow',
connections: {},
nodeTypes: {},
nodes: [
{
id: uuid(),
name: 'My Node',
type: 'n8n-nodes-base.slack',
typeVersion: 1,
position: [0, 0] as [number, number],
parameters: {},
},
],
};
await createWorkflowWithHistory(workflowDetails);
const testAudit = await securityAuditService.run(['credentials']);
const section = getRiskSection(
testAudit,
CREDENTIALS_REPORT.RISK,
CREDENTIALS_REPORT.SECTIONS.CREDS_NOT_IN_ACTIVE_USE,
);
expect(section.location).toHaveLength(1);
expect(section.location[0]).toMatchObject({
id: credential.id,
name: 'My Slack Credential',
});
});
test('should report credential in not recently executed workflow', async () => {
const credentialDetails = {
id: generateNanoId(),
name: 'My Slack Credential',
data: 'U2FsdGVkX18WjITBG4IDqrGB1xE/uzVNjtwDAG3lP7E=',
type: 'slackApi',
};
const credential = await Container.get(CredentialsRepository).save(credentialDetails);
const workflowDetails = {
name: 'My Test Workflow',
connections: {},
nodeTypes: {},
nodes: [
{
id: uuid(),
name: 'My Node',
type: 'n8n-nodes-base.slack',
typeVersion: 1,
position: [0, 0] as [number, number],
credentials: {
slackApi: {
id: credential.id,
name: credential.name,
},
},
parameters: {},
},
],
};
const workflow = await createWorkflowWithHistory(workflowDetails);
const date = new Date();
date.setDate(date.getDate() - securityConfig.daysAbandonedWorkflow - 1);
const savedExecution = await Container.get(ExecutionRepository).save({
finished: true,
mode: 'manual',
createdAt: date,
startedAt: date,
stoppedAt: date,
workflowId: workflow.id,
waitTill: null,
status: 'success',
});
await Container.get(ExecutionDataRepository).save({
execution: savedExecution,
data: '[]',
workflowData: workflow,
});
const testAudit = await securityAuditService.run(['credentials']);
const section = getRiskSection(
testAudit,
CREDENTIALS_REPORT.RISK,
CREDENTIALS_REPORT.SECTIONS.CREDS_NOT_RECENTLY_EXECUTED,
);
expect(section.location).toHaveLength(1);
expect(section.location[0]).toMatchObject({
id: credential.id,
name: credential.name,
});
});
test('should not report credentials in recently executed workflow', async () => {
const credentialDetails = {
id: generateNanoId(),
name: 'My Slack Credential',
data: 'U2FsdGVkX18WjITBG4IDqrGB1xE/uzVNjtwDAG3lP7E=',
type: 'slackApi',
};
const credential = await Container.get(CredentialsRepository).save(credentialDetails);
const workflowDetails = {
name: 'My Test Workflow',
connections: {},
nodeTypes: {},
nodes: [
{
id: uuid(),
name: 'My Node',
type: 'n8n-nodes-base.slack',
typeVersion: 1,
position: [0, 0] as [number, number],
credentials: {
slackApi: {
id: credential.id,
name: credential.name,
},
},
parameters: {},
},
],
};
const workflow = await createActiveWorkflow(workflowDetails);
const date = new Date();
date.setDate(date.getDate() - securityConfig.daysAbandonedWorkflow + 1);
const savedExecution = await Container.get(ExecutionRepository).save({
finished: true,
mode: 'manual',
createdAt: date,
startedAt: date,
stoppedAt: date,
workflowId: workflow.id,
waitTill: null,
status: 'success',
});
await Container.get(ExecutionDataRepository).save({
execution: savedExecution,
data: '[]',
workflowData: workflow,
});
const testAudit = await securityAuditService.run(['credentials']);
expect(testAudit).toBeEmptyArray();
});