n8n
Version:
n8n Workflow Automation Tool
175 lines • 9.04 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CredentialsPermissionChecker = void 0;
const db_1 = require("@n8n/db");
const di_1 = require("@n8n/di");
const permissions_1 = require("@n8n/permissions");
const n8n_workflow_1 = require("n8n-workflow");
const credentials_finder_service_1 = require("../../credentials/credentials-finder.service");
const node_types_1 = require("../../node-types");
const ownership_service_1 = require("../../services/ownership.service");
const project_service_ee_1 = require("../../services/project.service.ee");
class InvalidCredentialError extends n8n_workflow_1.UserError {
constructor(node) {
super(`Node "${node.name}" uses invalid credential`);
this.node = node;
this.description = 'Please recreate the credential.';
}
}
class InaccessibleCredentialForUserError extends n8n_workflow_1.UserError {
constructor(node) {
super(`Node "${node.name}" uses a credential you do not have access to`);
this.node = node;
this.description = 'This node uses a credential you do not have access to. Ask its owner to share it with you.';
}
}
class InaccessibleCredentialError extends n8n_workflow_1.UserError {
constructor(node, project) {
super(`Node "${node.name}" does not have access to the credential`);
this.node = node;
this.project = project;
this.description = this.project.type === 'personal'
? 'Please recreate the credential or ask its owner to share it with you.'
: `Please make sure that the credential is shared with the project "${this.project.name}"`;
}
}
let CredentialsPermissionChecker = class CredentialsPermissionChecker {
constructor(sharedCredentialsRepository, credentialsRepository, ownershipService, projectService, nodeTypes, userRepository, credentialsFinderService) {
this.sharedCredentialsRepository = sharedCredentialsRepository;
this.credentialsRepository = credentialsRepository;
this.ownershipService = ownershipService;
this.projectService = projectService;
this.nodeTypes = nodeTypes;
this.userRepository = userRepository;
this.credentialsFinderService = credentialsFinderService;
}
async checkForUser(userId, nodes) {
const credIdsToNodes = this.mapCredIdsToNodes(nodes);
const workflowCredIds = Object.keys(credIdsToNodes);
if (workflowCredIds.length === 0)
return;
const user = await this.userRepository.findOne({
where: { id: userId },
relations: ['role'],
});
if (!user) {
throw new InaccessibleCredentialForUserError(credIdsToNodes[workflowCredIds[0]][0]);
}
const unavailableCredentials = await this.credentialsRepository.findNonProjectCredentialsByIds(workflowCredIds);
if (unavailableCredentials.length > 0) {
throw new InaccessibleCredentialForUserError(credIdsToNodes[unavailableCredentials[0].id][0]);
}
if ((0, permissions_1.hasGlobalScope)(user, 'credential:list'))
return;
const accessibleCredentials = await this.credentialsFinderService.findCredentialsForUser(user, [
'credential:read',
]);
const accessibleSet = new Set(accessibleCredentials.map((cred) => cred.id));
for (const credentialsId of workflowCredIds) {
if (!accessibleSet.has(credentialsId)) {
throw new InaccessibleCredentialForUserError(credIdsToNodes[credentialsId][0]);
}
}
}
async check(workflowId, nodes) {
const homeProject = await this.ownershipService.getWorkflowProjectCached(workflowId);
const credIdsToNodes = this.mapCredIdsToNodes(nodes);
const workflowCredIds = Object.keys(credIdsToNodes);
if (workflowCredIds.length === 0)
return;
await this.ensureOnlyWorkflowCredentials(workflowCredIds, credIdsToNodes, homeProject);
const homeProjectOwner = await this.ownershipService.getPersonalProjectOwnerCached(homeProject.id);
if (homeProject.type === 'personal' &&
homeProjectOwner &&
(0, permissions_1.hasGlobalScope)(homeProjectOwner, 'credential:list')) {
return;
}
const projectIds = await this.projectService.findProjectsWorkflowIsIn(workflowId);
const accessible = await this.sharedCredentialsRepository.getFilteredAccessibleCredentials(projectIds, workflowCredIds);
const accessibleSet = await this.addGlobalCredentialsToAccessibleSet(accessible);
for (const credentialsId of workflowCredIds) {
if (!accessibleSet.has(credentialsId)) {
const nodeToFlag = credIdsToNodes[credentialsId][0];
throw new InaccessibleCredentialError(nodeToFlag, homeProject);
}
}
}
async ensureOnlyWorkflowCredentials(workflowCredIds, credIdsToNodes, homeProject) {
const unavailableCredentials = await this.credentialsRepository.findNonProjectCredentialsByIds(workflowCredIds);
if (unavailableCredentials.length > 0) {
const nodeToFlag = credIdsToNodes[unavailableCredentials[0].id][0];
throw new InaccessibleCredentialError(nodeToFlag, homeProject);
}
}
async addGlobalCredentialsToAccessibleSet(accessibleCredentialIds) {
const accessibleSet = new Set(accessibleCredentialIds);
const globalCredentials = await this.credentialsRepository.find({
where: { isGlobal: true, usageScope: 'project' },
select: ['id'],
});
for (const globalCred of globalCredentials) {
accessibleSet.add(globalCred.id);
}
return accessibleSet;
}
mapCredIdsToNodes(nodes) {
return nodes.reduce((map, node) => {
if (node.disabled || !node.credentials)
return map;
const activeCredTypes = this.getActiveCredentialTypes(node);
for (const [credType, cred] of Object.entries(node.credentials)) {
if (!cred.id) {
if (cred.__aiGatewayManaged === true)
continue;
throw new InvalidCredentialError(node);
}
if (activeCredTypes !== null && !activeCredTypes.has(credType))
continue;
map[cred.id] = map[cred.id] ? [...map[cred.id], node] : [node];
}
return map;
}, {});
}
getActiveCredentialTypes(node) {
try {
const nodeType = this.nodeTypes.getByNameAndVersion(node.type, node.typeVersion);
const activeTypes = new Set();
for (const credDef of nodeType.description.credentials ?? []) {
if ((0, n8n_workflow_1.displayParameter)(node.parameters, credDef, node, nodeType.description)) {
activeTypes.add(credDef.name);
}
}
const { nodeCredentialType } = node.parameters;
if (typeof nodeCredentialType === 'string' && nodeCredentialType) {
if ((0, n8n_workflow_1.isExpression)(nodeCredentialType))
return null;
activeTypes.add(nodeCredentialType);
}
const { genericAuthType } = node.parameters;
if (typeof genericAuthType === 'string' && genericAuthType) {
if ((0, n8n_workflow_1.isExpression)(genericAuthType))
return null;
activeTypes.add(genericAuthType);
}
return activeTypes;
}
catch {
return null;
}
}
};
exports.CredentialsPermissionChecker = CredentialsPermissionChecker;
exports.CredentialsPermissionChecker = CredentialsPermissionChecker = __decorate([
(0, di_1.Service)(),
__metadata("design:paramtypes", [db_1.SharedCredentialsRepository, db_1.CredentialsRepository, ownership_service_1.OwnershipService, project_service_ee_1.ProjectService, node_types_1.NodeTypes, db_1.UserRepository, credentials_finder_service_1.CredentialsFinderService])
], CredentialsPermissionChecker);
//# sourceMappingURL=credentials-permission-checker.js.map