-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): Add credential runtime checks and prevent tampering in manual run #4481
Merged
ivov
merged 20 commits into
master
from
n8n-5257-implement-runtime-checks-for-credentials
Nov 11, 2022
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
bf22cb7
:sparkles: Create `PermissionChecker`
ivov 770ba20
:zap: Adjust helper
ivov 1749342
:fire: Remove superseded helpers
ivov 698e4f6
:zap: Use `PermissionChecker`
ivov e7f2fe0
:test_tube: Add test for dynamic router switching
ivov 8e8d490
:twisted_rightwards_arrows: Merge master
ivov 89b3acf
:zap: Simplify checks
ivov 0dbede7
:zap: Export utils
ivov 944cd92
:zap: Add missing `init` method
ivov 045547e
:test_tube: Write tests for `PermissionChecker`
ivov c8f64ec
:blue_book: Update types
ivov cbf4c57
:test_tube: Fix tests
ivov 82b996c
:sparkles: Set up `runManually()`
ivov 787c648
:zap: Refactor to reuse methods
ivov 36294dd
:twisted_rightwards_arrows: Merge master
ivov aa8568b
:test_tube: Clear shared tables first
ivov 59cd871
:twisted_rightwards_arrows: Merge master
ivov 25e8ad7
:twisted_rightwards_arrows: Adjust merge
ivov c17711d
:zap: Adjust imports
ivov 1a6d63c
:twisted_rightwards_arrows: Merge master
ivov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { INode, NodeOperationError, Workflow } from 'n8n-workflow'; | ||
import { In } from 'typeorm'; | ||
import * as Db from '@/Db'; | ||
|
||
export class PermissionChecker { | ||
/** | ||
* Check if a user is permitted to execute a workflow. | ||
*/ | ||
static async check(workflow: Workflow, userId: string) { | ||
// allow if no nodes in this workflow use creds | ||
|
||
const credIdsToNodes = PermissionChecker.mapCredIdsToNodes(workflow); | ||
|
||
const workflowCredIds = Object.keys(credIdsToNodes); | ||
|
||
if (workflowCredIds.length === 0) return; | ||
|
||
// allow if requesting user is instance owner | ||
|
||
const user = await Db.collections.User.findOneOrFail(userId, { | ||
relations: ['globalRole'], | ||
}); | ||
|
||
if (user.globalRole.name === 'owner') return; | ||
|
||
// allow if all creds used in this workflow are a subset of | ||
// all creds accessible to users who have access to this workflow | ||
|
||
const workflowSharings = await Db.collections.SharedWorkflow.find({ | ||
relations: ['workflow'], | ||
where: { workflow: { id: Number(workflow.id) } }, | ||
}); | ||
|
||
const workflowUserIds = workflowSharings.map((s) => s.userId); | ||
|
||
const credentialSharings = await Db.collections.SharedCredentials.find({ | ||
where: { user: In(workflowUserIds) }, | ||
}); | ||
|
||
const accessibleCredIds = credentialSharings.map((s) => s.credentialId.toString()); | ||
|
||
const inaccessibleCredIds = workflowCredIds.filter((id) => !accessibleCredIds.includes(id)); | ||
|
||
if (inaccessibleCredIds.length === 0) return; | ||
|
||
// if disallowed, flag only first node using first inaccessible cred | ||
|
||
const nodeToFlag = credIdsToNodes[inaccessibleCredIds[0]][0]; | ||
|
||
throw new NodeOperationError(nodeToFlag, 'Node has no access to credential', { | ||
description: 'Please recreate the credential or ask its owner to share it with you.', | ||
}); | ||
} | ||
|
||
private static mapCredIdsToNodes(workflow: Workflow) { | ||
return Object.values(workflow.nodes).reduce<{ [credentialId: string]: INode[] }>( | ||
(map, node) => { | ||
if (node.disabled || !node.credentials) return map; | ||
|
||
Object.values(node.credentials).forEach((cred) => { | ||
if (!cred.id) { | ||
throw new NodeOperationError(node, 'Node uses invalid credential', { | ||
description: 'Please recreate the credential.', | ||
}); | ||
} | ||
|
||
map[cred.id] = map[cred.id] ? [...map[cred.id], node] : [node]; | ||
}); | ||
|
||
return map; | ||
}, | ||
{}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I loved the creation of this function, this is very useful in so many places.