2023-01-05 04:28:40 -08:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2023-11-13 02:50:43 -08:00
|
|
|
import { SecurityAuditService } from '@/security-audit/SecurityAudit.service';
|
2023-01-05 04:28:40 -08:00
|
|
|
import {
|
|
|
|
DATABASE_REPORT,
|
|
|
|
SQL_NODE_TYPES,
|
|
|
|
SQL_NODE_TYPES_WITH_QUERY_PARAMS,
|
2023-11-13 02:50:43 -08:00
|
|
|
} from '@/security-audit/constants';
|
2023-01-05 04:28:40 -08:00
|
|
|
import { getRiskSection, saveManualTriggerWorkflow } from './utils';
|
|
|
|
import * as testDb from '../shared/testDb';
|
2023-07-13 01:14:48 -07:00
|
|
|
import { generateNanoId } from '@db/utils/generators';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { WorkflowRepository } from '@db/repositories/workflow.repository';
|
|
|
|
import Container from 'typedi';
|
2023-01-05 04:28:40 -08:00
|
|
|
|
2023-11-13 02:50:43 -08:00
|
|
|
let securityAuditService: SecurityAuditService;
|
|
|
|
|
2023-01-05 04:28:40 -08:00
|
|
|
beforeAll(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.init();
|
2023-11-13 02:50:43 -08:00
|
|
|
|
|
|
|
securityAuditService = new SecurityAuditService(Container.get(WorkflowRepository));
|
2023-01-05 04:28:40 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.truncate(['Workflow']);
|
2023-01-05 04:28:40 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.terminate();
|
2023-01-05 04:28:40 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should report expressions in queries', async () => {
|
|
|
|
const map = [...SQL_NODE_TYPES].reduce<{ [nodeType: string]: string }>((acc, cur) => {
|
|
|
|
return (acc[cur] = uuid()), acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
const promises = Object.entries(map).map(async ([nodeType, nodeId]) => {
|
|
|
|
const details = {
|
2023-06-20 10:13:18 -07:00
|
|
|
id: generateNanoId(),
|
2023-01-05 04:28:40 -08:00
|
|
|
name: 'My Test Workflow',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodeTypes: {},
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: nodeId,
|
|
|
|
name: 'My Node',
|
|
|
|
type: nodeType,
|
|
|
|
parameters: {
|
|
|
|
operation: 'executeQuery',
|
|
|
|
query: '=SELECT * FROM {{ $json.table }}',
|
|
|
|
additionalFields: {},
|
|
|
|
},
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0] as [number, number],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
return Container.get(WorkflowRepository).save(details);
|
2023-01-05 04:28:40 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
2023-11-13 02:50:43 -08:00
|
|
|
const testAudit = await securityAuditService.run(['database']);
|
2023-01-05 04:28:40 -08:00
|
|
|
|
|
|
|
const section = getRiskSection(
|
|
|
|
testAudit,
|
|
|
|
DATABASE_REPORT.RISK,
|
|
|
|
DATABASE_REPORT.SECTIONS.EXPRESSIONS_IN_QUERIES,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(section.location).toHaveLength(SQL_NODE_TYPES.size);
|
|
|
|
|
|
|
|
for (const loc of section.location) {
|
|
|
|
if (loc.kind === 'node') {
|
|
|
|
expect(loc.nodeId).toBe(map[loc.nodeType]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should report expressions in query params', async () => {
|
|
|
|
const map = [...SQL_NODE_TYPES_WITH_QUERY_PARAMS].reduce<{ [nodeType: string]: string }>(
|
|
|
|
(acc, cur) => {
|
|
|
|
return (acc[cur] = uuid()), acc;
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
|
|
|
|
const promises = Object.entries(map).map(async ([nodeType, nodeId]) => {
|
|
|
|
const details = {
|
2023-06-20 10:13:18 -07:00
|
|
|
id: generateNanoId(),
|
2023-01-05 04:28:40 -08:00
|
|
|
name: 'My Test Workflow',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodeTypes: {},
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: nodeId,
|
|
|
|
name: 'My Node',
|
|
|
|
type: nodeType,
|
|
|
|
parameters: {
|
|
|
|
operation: 'executeQuery',
|
|
|
|
query: 'SELECT * FROM users WHERE id = $1;',
|
|
|
|
additionalFields: {
|
|
|
|
queryParams: '={{ $json.userId }}',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0] as [number, number],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
return Container.get(WorkflowRepository).save(details);
|
2023-01-05 04:28:40 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
2023-11-13 02:50:43 -08:00
|
|
|
const testAudit = await securityAuditService.run(['database']);
|
2023-01-05 04:28:40 -08:00
|
|
|
|
|
|
|
const section = getRiskSection(
|
|
|
|
testAudit,
|
|
|
|
DATABASE_REPORT.RISK,
|
|
|
|
DATABASE_REPORT.SECTIONS.EXPRESSIONS_IN_QUERY_PARAMS,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(section.location).toHaveLength(SQL_NODE_TYPES_WITH_QUERY_PARAMS.size);
|
|
|
|
|
|
|
|
for (const loc of section.location) {
|
|
|
|
if (loc.kind === 'node') {
|
|
|
|
expect(loc.nodeId).toBe(map[loc.nodeType]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should report unused query params', async () => {
|
|
|
|
const map = [...SQL_NODE_TYPES_WITH_QUERY_PARAMS].reduce<{ [nodeType: string]: string }>(
|
|
|
|
(acc, cur) => {
|
|
|
|
return (acc[cur] = uuid()), acc;
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
|
|
|
|
const promises = Object.entries(map).map(async ([nodeType, nodeId]) => {
|
|
|
|
const details = {
|
2023-06-20 10:13:18 -07:00
|
|
|
id: generateNanoId(),
|
2023-01-05 04:28:40 -08:00
|
|
|
name: 'My Test Workflow',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodeTypes: {},
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: nodeId,
|
|
|
|
name: 'My Node',
|
|
|
|
type: nodeType,
|
|
|
|
parameters: {
|
|
|
|
operation: 'executeQuery',
|
|
|
|
query: 'SELECT * FROM users WHERE id = 123;',
|
|
|
|
},
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0] as [number, number],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
return Container.get(WorkflowRepository).save(details);
|
2023-01-05 04:28:40 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
2023-11-13 02:50:43 -08:00
|
|
|
const testAudit = await securityAuditService.run(['database']);
|
2023-01-05 04:28:40 -08:00
|
|
|
|
|
|
|
const section = getRiskSection(
|
|
|
|
testAudit,
|
|
|
|
DATABASE_REPORT.RISK,
|
|
|
|
DATABASE_REPORT.SECTIONS.UNUSED_QUERY_PARAMS,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(section.location).toHaveLength(SQL_NODE_TYPES_WITH_QUERY_PARAMS.size);
|
|
|
|
|
|
|
|
for (const loc of section.location) {
|
|
|
|
if (loc.kind === 'node') {
|
|
|
|
expect(loc.nodeId).toBe(map[loc.nodeType]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not report non-database node', async () => {
|
|
|
|
await saveManualTriggerWorkflow();
|
|
|
|
|
2023-11-13 02:50:43 -08:00
|
|
|
const testAudit = await securityAuditService.run(['database']);
|
2023-01-05 04:28:40 -08:00
|
|
|
|
|
|
|
expect(testAudit).toBeEmptyArray();
|
|
|
|
});
|