-
Notifications
You must be signed in to change notification settings - Fork 799
feat: add plugin directory trust guard to harden shared plugin caches #7308
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
Open
aknownuser
wants to merge
6
commits into
master
Choose a base branch
from
security-plugin-dir-trust-guard
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ad72e21
Add plugin directory trust guard to harden shared plugin caches
aknownuser df2e3cf
Validate and normalise NXF_PLUGINS_STRICT_MODE value
aknownuser 31118ad
Add test coverage for the untrusted-owner branch
aknownuser 9c9459c
Harden plugin-dir trust predicate to avoid false positives
aknownuser 71ff931
Update docs/plugins/using-plugins.mdx
aknownuser 54b91cb
Update docs/plugins/using-plugins.mdx
aknownuser 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
116 changes: 116 additions & 0 deletions
116
modules/nf-commons/src/main/nextflow/plugin/PluginSecurity.groovy
This file contains hidden or 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,116 @@ | ||
| /* | ||
| * Copyright 2013-2026, Seqera Labs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package nextflow.plugin | ||
|
|
||
| import java.nio.file.Files | ||
| import java.nio.file.Path | ||
| import java.nio.file.attribute.PosixFileAttributeView | ||
| import java.nio.file.attribute.PosixFileAttributes | ||
| import java.nio.file.attribute.PosixFilePermission | ||
| import java.nio.file.attribute.PosixFilePermissions | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import groovy.util.logging.Slf4j | ||
| import nextflow.SysEnv | ||
| import nextflow.exception.AbortOperationException | ||
| /** | ||
| * Validate that a plugin directory can be trusted before its content is loaded and | ||
| * executed inside the Nextflow process. | ||
| * | ||
| * The plugin cache ({@code NXF_PLUGINS_DIR}, {@code $NXF_HOME/plugins}) is expected to be a | ||
| * per-user, private trust boundary. When it is shared and writable by other users, a lower-trust | ||
| * user can pre-populate an official pinned plugin coordinate (e.g. {@code nf-amazon-3.10.0}) with | ||
| * attacker-controlled code that Nextflow would then load from the cache without any origin check. | ||
| * | ||
| * A directory is considered trusted only when it is owned by the current user (or root) AND it is | ||
| * not writable by group or others. This accepts the two legitimate deployment shapes - a private | ||
| * user cache (e.g. {@code 0700}) and an admin-managed read-only shared cache (e.g. {@code root:root | ||
| * 0755}) - while rejecting an attacker-owned directory or a group/world-writable cache. | ||
| * | ||
| * The behaviour is controlled by the {@code NXF_PLUGINS_STRICT_MODE} environment variable: | ||
| * {@code warn} (default) logs a warning and continues, {@code strict} aborts the run, and | ||
| * {@code off} disables the check. On filesystems that do not support POSIX attributes the check is | ||
| * a no-op. | ||
| * | ||
| * @author Claude <noreply@anthropic.com> | ||
| */ | ||
| @Slf4j | ||
| @CompileStatic | ||
| class PluginSecurity { | ||
|
|
||
| static final String MODE_WARN = 'warn' | ||
| static final String MODE_STRICT = 'strict' | ||
| static final String MODE_OFF = 'off' | ||
|
|
||
| /** | ||
| * Resolve the strict mode from the {@code NXF_PLUGINS_STRICT_MODE} environment variable. | ||
| * | ||
| * @return one of {@code warn} (default), {@code strict} or {@code off} | ||
| */ | ||
| static String getMode() { | ||
| return SysEnv.get('NXF_PLUGINS_STRICT_MODE', MODE_WARN) | ||
|
aknownuser marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * Verify that the given plugin directory can be trusted, using the mode resolved from the | ||
| * environment. | ||
| * | ||
| * @param dir The plugin directory (the plugins root or a {@code $id-$version} directory) | ||
| */ | ||
| static void checkTrustedDir(Path dir) { | ||
| checkTrustedDir(dir, getMode()) | ||
| } | ||
|
|
||
| /** | ||
| * Verify that the given plugin directory can be trusted. | ||
| * | ||
| * @param dir The plugin directory (the plugins root or a {@code $id-$version} directory) | ||
| * @param mode The strict mode: {@code warn}, {@code strict} or {@code off} | ||
| */ | ||
| static void checkTrustedDir(Path dir, String mode) { | ||
| if( mode == MODE_OFF || dir == null ) | ||
| return | ||
| try { | ||
| final view = Files.getFileAttributeView(dir, PosixFileAttributeView) | ||
| // non-POSIX filesystem (e.g. Windows, some object stores) - nothing to check | ||
| if( view == null ) | ||
| return | ||
| final PosixFileAttributes attrs = view.readAttributes() | ||
| final perms = attrs.permissions() | ||
| final owner = attrs.owner().name | ||
| final me = System.getProperty('user.name') | ||
|
|
||
| final writableByOthers = perms.contains(PosixFilePermission.GROUP_WRITE) || perms.contains(PosixFilePermission.OTHERS_WRITE) | ||
| final untrustedOwner = owner != me && owner != 'root' | ||
|
|
||
| if( writableByOthers || untrustedOwner ) { | ||
| final msg = "Plugin directory '${dir}' is not secure (owner=${owner}, mode=${PosixFilePermissions.toString(perms)}) " + | ||
| "- it may be modified by other users, which could allow untrusted plugin code to run in your Nextflow process" | ||
| if( mode == MODE_STRICT ) | ||
| throw new AbortOperationException("${msg} -- refusing to load plugins. Use a private directory (chmod 700) owned by you, or set NXF_PLUGINS_STRICT_MODE=warn to override") | ||
| log.warn "${msg} -- set NXF_PLUGINS_STRICT_MODE=strict to refuse loading, or use a private plugins directory owned by you" | ||
| } | ||
| } | ||
| catch( AbortOperationException e ) { | ||
| throw e | ||
| } | ||
| catch( Exception e ) { | ||
| // never let a permission-inspection failure break a run | ||
| log.debug "Unable to verify plugin directory permissions for '${dir}' - ${e.message}" | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
141 changes: 141 additions & 0 deletions
141
modules/nf-commons/src/test/nextflow/plugin/PluginSecurityTest.groovy
This file contains hidden or 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,141 @@ | ||
| /* | ||
| * Copyright 2013-2026, Seqera Labs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package nextflow.plugin | ||
|
|
||
| import java.nio.file.Files | ||
| import java.nio.file.Path | ||
| import java.nio.file.attribute.PosixFilePermissions | ||
|
|
||
| import nextflow.SysEnv | ||
| import nextflow.exception.AbortOperationException | ||
| import spock.lang.IgnoreIf | ||
| import spock.lang.Specification | ||
| import spock.lang.Unroll | ||
| /** | ||
| * | ||
| * @author Claude <noreply@anthropic.com> | ||
| */ | ||
| @IgnoreIf({ os.windows }) | ||
| class PluginSecurityTest extends Specification { | ||
|
|
||
| private Path tempDirWithPerms(String perms) { | ||
| final dir = Files.createTempDirectory('test-plugin-sec') | ||
| Files.setPosixFilePermissions(dir, PosixFilePermissions.fromString(perms)) | ||
| return dir | ||
| } | ||
|
|
||
| @Unroll | ||
| def 'should accept a private dir owned by current user - mode=#MODE perms=#PERMS' () { | ||
|
aknownuser marked this conversation as resolved.
Outdated
|
||
| given: | ||
| def dir = tempDirWithPerms(PERMS) | ||
|
|
||
| when: | ||
| PluginSecurity.checkTrustedDir(dir, MODE) | ||
| then: | ||
| noExceptionThrown() | ||
|
|
||
| cleanup: | ||
| dir?.deleteDir() | ||
|
|
||
| where: | ||
| MODE | PERMS | ||
| 'strict' | 'rwx------' | ||
| 'warn' | 'rwx------' | ||
| 'strict' | 'rwxr-xr-x' | ||
| 'warn' | 'rwxr-xr-x' | ||
| } | ||
|
|
||
| @Unroll | ||
| def 'should reject a group/world-writable dir in strict mode - perms=#PERMS' () { | ||
| given: | ||
| def dir = tempDirWithPerms(PERMS) | ||
|
|
||
| when: | ||
| PluginSecurity.checkTrustedDir(dir, 'strict') | ||
| then: | ||
| def e = thrown(AbortOperationException) | ||
| e.message.contains('is not secure') | ||
|
|
||
| cleanup: | ||
| dir?.deleteDir() | ||
|
|
||
| where: | ||
| PERMS << ['rwxrwx---', 'rwxrwxrwx', 'rwx-w----', 'rwx----w-'] | ||
| } | ||
|
|
||
| @Unroll | ||
| def 'should only warn (not throw) for a group/world-writable dir in warn mode - perms=#PERMS' () { | ||
| given: | ||
| def dir = tempDirWithPerms(PERMS) | ||
|
|
||
| when: | ||
| PluginSecurity.checkTrustedDir(dir, 'warn') | ||
| then: | ||
| noExceptionThrown() | ||
|
|
||
| cleanup: | ||
| dir?.deleteDir() | ||
|
|
||
| where: | ||
| PERMS << ['rwxrwx---', 'rwxrwxrwx'] | ||
| } | ||
|
|
||
| def 'should skip the check entirely when mode is off' () { | ||
| given: | ||
| def dir = tempDirWithPerms('rwxrwxrwx') | ||
|
|
||
| when: | ||
| PluginSecurity.checkTrustedDir(dir, 'off') | ||
| then: | ||
| noExceptionThrown() | ||
|
|
||
| cleanup: | ||
| dir?.deleteDir() | ||
| } | ||
|
|
||
| def 'should be a no-op for a null dir' () { | ||
| when: | ||
| PluginSecurity.checkTrustedDir(null, 'strict') | ||
| then: | ||
| noExceptionThrown() | ||
| } | ||
|
|
||
| def 'should default to warn mode from the environment' () { | ||
| given: | ||
| SysEnv.push([:]) | ||
| expect: | ||
| PluginSecurity.getMode() == 'warn' | ||
|
|
||
| cleanup: | ||
| SysEnv.pop() | ||
| } | ||
|
|
||
| @Unroll | ||
| def 'should resolve mode #VALUE from the environment' () { | ||
| given: | ||
| SysEnv.push([NXF_PLUGINS_STRICT_MODE: VALUE]) | ||
| expect: | ||
| PluginSecurity.getMode() == VALUE | ||
|
|
||
| cleanup: | ||
| SysEnv.pop() | ||
|
|
||
| where: | ||
| VALUE << ['warn', 'strict', 'off'] | ||
| } | ||
|
|
||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.