2022-11-11 02:14:45 -08:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2023-03-17 09:24:05 -07:00
|
|
|
import { Container } from 'typedi';
|
2024-01-16 05:15:29 -08:00
|
|
|
import type { WorkflowSettings } from 'n8n-workflow';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { SubworkflowOperationError, Workflow } from 'n8n-workflow';
|
2022-11-11 02:14:45 -08:00
|
|
|
|
2022-12-22 09:28:23 -08:00
|
|
|
import config from '@/config';
|
2023-03-17 09:24:05 -07:00
|
|
|
import { User } from '@db/entities/User';
|
2024-01-16 05:15:29 -08:00
|
|
|
import { WorkflowRepository } from '@db/repositories/workflow.repository';
|
|
|
|
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
|
|
|
|
import { UserRepository } from '@/databases/repositories/user.repository';
|
|
|
|
import { generateNanoId } from '@/databases/utils/generators';
|
|
|
|
import { License } from '@/License';
|
2023-03-17 09:24:05 -07:00
|
|
|
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
|
|
|
|
import { NodeTypes } from '@/NodeTypes';
|
2023-10-09 07:09:23 -07:00
|
|
|
import { OwnershipService } from '@/services/ownership.service';
|
2024-01-16 05:15:29 -08:00
|
|
|
import { PermissionChecker } from '@/UserManagement/PermissionChecker';
|
2023-03-17 09:24:05 -07:00
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
import { mockInstance } from '../shared/mocking';
|
2022-11-11 02:14:45 -08:00
|
|
|
import {
|
|
|
|
randomCredentialPayload as randomCred,
|
2023-12-06 04:27:11 -08:00
|
|
|
randomName,
|
2022-11-11 02:14:45 -08:00
|
|
|
randomPositiveDigit,
|
|
|
|
} from '../integration/shared/random';
|
2024-01-16 05:15:29 -08:00
|
|
|
import { LicenseMocker } from '../integration/shared/license';
|
2023-03-17 09:24:05 -07:00
|
|
|
import * as testDb from '../integration/shared/testDb';
|
2022-11-11 02:14:45 -08:00
|
|
|
import type { SaveCredentialFunction } from '../integration/shared/types';
|
2023-10-09 07:09:23 -07:00
|
|
|
import { mockNodeTypesData } from './Helpers';
|
2023-11-08 07:29:39 -08:00
|
|
|
import { affixRoleToSaveCredential } from '../integration/shared/db/credentials';
|
|
|
|
import { createOwner, createUser } from '../integration/shared/db/users';
|
2023-12-06 04:27:11 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
export const toTargetCallErrorMsg = (subworkflowId: string) =>
|
|
|
|
`Target workflow ID ${subworkflowId} may not be called`;
|
|
|
|
|
|
|
|
export function createParentWorkflow() {
|
|
|
|
return Container.get(WorkflowRepository).create({
|
|
|
|
id: generateNanoId(),
|
|
|
|
name: randomName(),
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
name: '',
|
|
|
|
typeVersion: 1,
|
|
|
|
type: 'n8n-nodes-base.executeWorkflow',
|
|
|
|
position: [0, 0],
|
|
|
|
parameters: {},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createSubworkflow({
|
|
|
|
policy,
|
|
|
|
callerIds,
|
|
|
|
}: {
|
|
|
|
policy?: WorkflowSettings.CallerPolicy;
|
|
|
|
callerIds?: string;
|
|
|
|
} = {}) {
|
2023-12-06 04:27:11 -08:00
|
|
|
return new Workflow({
|
|
|
|
id: uuid(),
|
|
|
|
nodes: [],
|
|
|
|
connections: {},
|
|
|
|
active: false,
|
|
|
|
nodeTypes: mockNodeTypes,
|
|
|
|
settings: {
|
2023-12-08 02:21:43 -08:00
|
|
|
...(policy ? { callerPolicy: policy } : {}),
|
|
|
|
...(callerIds ? { callerIds } : {}),
|
2023-12-06 04:27:11 -08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2023-09-20 06:21:42 -07:00
|
|
|
|
2022-11-11 02:14:45 -08:00
|
|
|
let saveCredential: SaveCredentialFunction;
|
|
|
|
|
2024-01-16 05:15:29 -08:00
|
|
|
const mockNodeTypes = mockInstance(NodeTypes);
|
2023-03-17 09:24:05 -07:00
|
|
|
mockInstance(LoadNodesAndCredentials, {
|
2023-10-09 07:09:23 -07:00
|
|
|
loadedNodes: mockNodeTypesData(['start', 'actionNetwork']),
|
2023-03-17 09:24:05 -07:00
|
|
|
});
|
|
|
|
|
2024-01-16 05:15:29 -08:00
|
|
|
let permissionChecker: PermissionChecker;
|
|
|
|
|
2022-11-11 02:14:45 -08:00
|
|
|
beforeAll(async () => {
|
2023-01-13 09:12:22 -08:00
|
|
|
await testDb.init();
|
2022-11-11 02:14:45 -08:00
|
|
|
|
2024-01-24 04:38:57 -08:00
|
|
|
saveCredential = affixRoleToSaveCredential('credential:owner');
|
2024-01-16 05:15:29 -08:00
|
|
|
|
|
|
|
permissionChecker = Container.get(PermissionChecker);
|
2022-11-11 02:14:45 -08:00
|
|
|
});
|
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
describe('check()', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await testDb.truncate(['Workflow', 'Credentials']);
|
|
|
|
});
|
2022-11-11 02:14:45 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
afterAll(async () => {
|
|
|
|
await testDb.terminate();
|
|
|
|
});
|
2022-11-11 02:14:45 -08:00
|
|
|
|
|
|
|
test('should allow if workflow has no creds', async () => {
|
|
|
|
const userId = uuid();
|
|
|
|
|
|
|
|
const workflow = new Workflow({
|
|
|
|
id: randomPositiveDigit().toString(),
|
|
|
|
name: 'test',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodeTypes: mockNodeTypes,
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: uuid(),
|
|
|
|
name: 'Start',
|
|
|
|
type: 'n8n-nodes-base.start',
|
|
|
|
typeVersion: 1,
|
|
|
|
parameters: {},
|
|
|
|
position: [0, 0],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
expect(async () => await permissionChecker.check(workflow, userId)).not.toThrow();
|
2022-11-11 02:14:45 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should allow if requesting user is instance owner', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const owner = await createOwner();
|
2022-11-11 02:14:45 -08:00
|
|
|
|
|
|
|
const workflow = new Workflow({
|
|
|
|
id: randomPositiveDigit().toString(),
|
|
|
|
name: 'test',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodeTypes: mockNodeTypes,
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: uuid(),
|
|
|
|
name: 'Action Network',
|
|
|
|
type: 'n8n-nodes-base.actionNetwork',
|
|
|
|
parameters: {},
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0],
|
|
|
|
credentials: {
|
|
|
|
actionNetworkApi: {
|
|
|
|
id: randomPositiveDigit().toString(),
|
|
|
|
name: 'Action Network Account',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
expect(async () => await permissionChecker.check(workflow, owner.id)).not.toThrow();
|
2022-11-11 02:14:45 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should allow if workflow creds are valid subset', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const [owner, member] = await Promise.all([createOwner(), createUser()]);
|
2022-11-11 02:14:45 -08:00
|
|
|
|
|
|
|
const ownerCred = await saveCredential(randomCred(), { user: owner });
|
|
|
|
const memberCred = await saveCredential(randomCred(), { user: member });
|
|
|
|
|
|
|
|
const workflow = new Workflow({
|
|
|
|
id: randomPositiveDigit().toString(),
|
|
|
|
name: 'test',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodeTypes: mockNodeTypes,
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: uuid(),
|
|
|
|
name: 'Action Network',
|
|
|
|
type: 'n8n-nodes-base.actionNetwork',
|
|
|
|
parameters: {},
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0],
|
|
|
|
credentials: {
|
|
|
|
actionNetworkApi: {
|
2023-01-02 08:42:32 -08:00
|
|
|
id: ownerCred.id,
|
2022-11-11 02:14:45 -08:00
|
|
|
name: ownerCred.name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: uuid(),
|
|
|
|
name: 'Action Network 2',
|
|
|
|
type: 'n8n-nodes-base.actionNetwork',
|
|
|
|
parameters: {},
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0],
|
|
|
|
credentials: {
|
|
|
|
actionNetworkApi: {
|
2023-01-02 08:42:32 -08:00
|
|
|
id: memberCred.id,
|
2022-11-11 02:14:45 -08:00
|
|
|
name: memberCred.name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
expect(async () => await permissionChecker.check(workflow, owner.id)).not.toThrow();
|
2022-11-11 02:14:45 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should deny if workflow creds are not valid subset', async () => {
|
2023-11-08 07:29:39 -08:00
|
|
|
const member = await createUser();
|
2022-11-11 02:14:45 -08:00
|
|
|
|
|
|
|
const memberCred = await saveCredential(randomCred(), { user: member });
|
|
|
|
|
|
|
|
const workflowDetails = {
|
2023-01-02 08:42:32 -08:00
|
|
|
id: randomPositiveDigit().toString(),
|
2022-11-11 02:14:45 -08:00
|
|
|
name: 'test',
|
|
|
|
active: false,
|
|
|
|
connections: {},
|
|
|
|
nodeTypes: mockNodeTypes,
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: uuid(),
|
|
|
|
name: 'Action Network',
|
|
|
|
type: 'n8n-nodes-base.actionNetwork',
|
|
|
|
parameters: {},
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0] as [number, number],
|
|
|
|
credentials: {
|
|
|
|
actionNetworkApi: {
|
2023-01-02 08:42:32 -08:00
|
|
|
id: memberCred.id,
|
2022-11-11 02:14:45 -08:00
|
|
|
name: memberCred.name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: uuid(),
|
|
|
|
name: 'Action Network 2',
|
|
|
|
type: 'n8n-nodes-base.actionNetwork',
|
|
|
|
parameters: {},
|
|
|
|
typeVersion: 1,
|
|
|
|
position: [0, 0] as [number, number],
|
|
|
|
credentials: {
|
|
|
|
actionNetworkApi: {
|
|
|
|
id: 'non-existing-credential-id',
|
|
|
|
name: 'Non-existing credential name',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
const workflowEntity = await Container.get(WorkflowRepository).save(workflowDetails);
|
2022-11-11 02:14:45 -08:00
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
await Container.get(SharedWorkflowRepository).save({
|
2022-11-11 02:14:45 -08:00
|
|
|
workflow: workflowEntity,
|
|
|
|
user: member,
|
2024-01-24 04:38:57 -08:00
|
|
|
role: 'workflow:owner',
|
2022-11-11 02:14:45 -08:00
|
|
|
});
|
|
|
|
|
2023-01-02 08:42:32 -08:00
|
|
|
const workflow = new Workflow(workflowDetails);
|
2022-11-11 02:14:45 -08:00
|
|
|
|
2024-01-16 05:15:29 -08:00
|
|
|
await expect(permissionChecker.check(workflow, member.id)).rejects.toThrow();
|
2022-11-11 02:14:45 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
describe('checkSubworkflowExecutePolicy()', () => {
|
2023-07-31 02:37:09 -07:00
|
|
|
const ownershipService = mockInstance(OwnershipService);
|
2022-12-23 06:23:36 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
let license: LicenseMocker;
|
2023-12-06 04:27:11 -08:00
|
|
|
|
|
|
|
beforeAll(() => {
|
2023-12-08 02:21:43 -08:00
|
|
|
license = new LicenseMocker();
|
|
|
|
license.mock(Container.get(License));
|
|
|
|
license.enable('feat:sharing');
|
2023-12-06 04:27:11 -08:00
|
|
|
});
|
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
describe('no caller policy', () => {
|
|
|
|
test('should fall back to N8N_WORKFLOW_CALLER_POLICY_DEFAULT_OPTION', async () => {
|
|
|
|
config.set('workflows.callerPolicyDefaultOption', 'none');
|
2023-12-06 04:27:11 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
const parentWorkflow = createParentWorkflow();
|
|
|
|
const subworkflow = createSubworkflow(); // no caller policy
|
2022-12-22 09:28:23 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
ownershipService.getWorkflowOwnerCached.mockResolvedValue(new User());
|
2022-12-22 09:28:23 -08:00
|
|
|
|
2024-01-16 05:15:29 -08:00
|
|
|
const check = permissionChecker.checkSubworkflowExecutePolicy(subworkflow, parentWorkflow.id);
|
2023-12-06 04:27:11 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
await expect(check).rejects.toThrow(toTargetCallErrorMsg(subworkflow.id));
|
2022-12-22 09:28:23 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
config.load(config.default);
|
2022-12-22 09:28:23 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
describe('overridden caller policy', () => {
|
|
|
|
test('if no sharing, should override policy to workflows-from-same-owner', async () => {
|
|
|
|
license.disable('feat:sharing');
|
2023-12-06 04:27:11 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
const parentWorkflow = createParentWorkflow();
|
|
|
|
const subworkflow = createSubworkflow({ policy: 'any' }); // should be overridden
|
2022-12-22 09:28:23 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
const firstUser = Container.get(UserRepository).create({ id: uuid() });
|
|
|
|
const secondUser = Container.get(UserRepository).create({ id: uuid() });
|
2022-12-22 09:28:23 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
ownershipService.getWorkflowOwnerCached.mockResolvedValueOnce(firstUser); // parent workflow
|
|
|
|
ownershipService.getWorkflowOwnerCached.mockResolvedValueOnce(secondUser); // subworkflow
|
2023-12-06 04:27:11 -08:00
|
|
|
|
2024-01-16 05:15:29 -08:00
|
|
|
const check = permissionChecker.checkSubworkflowExecutePolicy(subworkflow, parentWorkflow.id);
|
2022-12-22 09:28:23 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
await expect(check).rejects.toThrow(toTargetCallErrorMsg(subworkflow.id));
|
|
|
|
|
|
|
|
try {
|
2024-01-16 05:15:29 -08:00
|
|
|
await permissionChecker.checkSubworkflowExecutePolicy(subworkflow, uuid());
|
2023-12-08 02:21:43 -08:00
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof SubworkflowOperationError) {
|
|
|
|
expect(error.description).toBe(
|
|
|
|
`${firstUser.firstName} (${firstUser.email}) can make this change. You may need to tell them the ID of this workflow, which is ${subworkflow.id}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
license.enable('feat:sharing');
|
2022-12-22 09:28:23 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
describe('workflows-from-list caller policy', () => {
|
|
|
|
test('should allow if caller list contains parent workflow ID', async () => {
|
|
|
|
const parentWorkflow = createParentWorkflow();
|
2023-12-06 04:27:11 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
const subworkflow = createSubworkflow({
|
|
|
|
policy: 'workflowsFromAList',
|
2023-12-06 04:27:11 -08:00
|
|
|
callerIds: `123,456,bcdef, ${parentWorkflow.id}`,
|
2023-12-08 02:21:43 -08:00
|
|
|
});
|
|
|
|
|
2024-01-16 05:15:29 -08:00
|
|
|
const check = permissionChecker.checkSubworkflowExecutePolicy(subworkflow, parentWorkflow.id);
|
2023-12-08 02:21:43 -08:00
|
|
|
|
|
|
|
await expect(check).resolves.not.toThrow();
|
2022-12-22 09:28:23 -08:00
|
|
|
});
|
2022-12-23 06:23:36 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
test('should deny if caller list does not contain parent workflow ID', async () => {
|
|
|
|
const parentWorkflow = createParentWorkflow();
|
2023-12-06 04:27:11 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
const subworkflow = createSubworkflow({
|
|
|
|
policy: 'workflowsFromAList',
|
|
|
|
callerIds: 'xyz',
|
|
|
|
});
|
2022-12-23 06:23:36 -08:00
|
|
|
|
2024-01-16 05:15:29 -08:00
|
|
|
const check = permissionChecker.checkSubworkflowExecutePolicy(subworkflow, parentWorkflow.id);
|
2023-12-08 02:21:43 -08:00
|
|
|
|
|
|
|
await expect(check).rejects.toThrow();
|
2022-12-23 06:23:36 -08:00
|
|
|
});
|
2022-12-22 09:28:23 -08:00
|
|
|
});
|
2023-12-06 04:27:11 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
describe('any caller policy', () => {
|
|
|
|
test('should not throw', async () => {
|
|
|
|
const parentWorkflow = createParentWorkflow();
|
|
|
|
const subworkflow = createSubworkflow({ policy: 'any' });
|
|
|
|
ownershipService.getWorkflowOwnerCached.mockResolvedValue(new User());
|
|
|
|
|
2024-01-16 05:15:29 -08:00
|
|
|
const check = permissionChecker.checkSubworkflowExecutePolicy(subworkflow, parentWorkflow.id);
|
2023-12-08 02:21:43 -08:00
|
|
|
|
|
|
|
await expect(check).resolves.not.toThrow();
|
2023-12-06 04:27:11 -08:00
|
|
|
});
|
2023-12-08 02:21:43 -08:00
|
|
|
});
|
2023-12-06 04:27:11 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
describe('workflows-from-same-owner caller policy', () => {
|
2023-12-06 04:27:11 -08:00
|
|
|
test('should deny if the two workflows are owned by different users', async () => {
|
|
|
|
const parentWorkflowOwner = Container.get(UserRepository).create({ id: uuid() });
|
|
|
|
const subworkflowOwner = Container.get(UserRepository).create({ id: uuid() });
|
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
ownershipService.getWorkflowOwnerCached.mockResolvedValueOnce(parentWorkflowOwner); // parent workflow
|
|
|
|
ownershipService.getWorkflowOwnerCached.mockResolvedValueOnce(subworkflowOwner); // subworkflow
|
2023-12-06 04:27:11 -08:00
|
|
|
|
|
|
|
const subworkflow = createSubworkflow({ policy: 'workflowsFromSameOwner' });
|
|
|
|
|
2024-01-16 05:15:29 -08:00
|
|
|
const check = permissionChecker.checkSubworkflowExecutePolicy(subworkflow, uuid());
|
2023-12-06 04:27:11 -08:00
|
|
|
|
2023-12-08 02:21:43 -08:00
|
|
|
await expect(check).rejects.toThrow(toTargetCallErrorMsg(subworkflow.id));
|
2023-12-06 04:27:11 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should allow if both workflows are owned by the same user', async () => {
|
2023-12-08 02:21:43 -08:00
|
|
|
const parentWorkflow = createParentWorkflow();
|
2023-12-06 04:27:11 -08:00
|
|
|
|
|
|
|
const bothWorkflowsOwner = Container.get(UserRepository).create({ id: uuid() });
|
|
|
|
|
|
|
|
ownershipService.getWorkflowOwnerCached.mockResolvedValueOnce(bothWorkflowsOwner); // parent workflow
|
|
|
|
ownershipService.getWorkflowOwnerCached.mockResolvedValueOnce(bothWorkflowsOwner); // subworkflow
|
|
|
|
|
|
|
|
const subworkflow = createSubworkflow({ policy: 'workflowsFromSameOwner' });
|
|
|
|
|
2024-01-16 05:15:29 -08:00
|
|
|
const check = permissionChecker.checkSubworkflowExecutePolicy(subworkflow, parentWorkflow.id);
|
2023-12-06 04:27:11 -08:00
|
|
|
|
|
|
|
await expect(check).resolves.not.toThrow();
|
|
|
|
});
|
|
|
|
});
|
2022-12-22 09:28:23 -08:00
|
|
|
});
|