Safeguard
Security Guides•

Are Jenkins shared libraries safe? Trusted versus untrusted explained

Trusted Jenkins shared libraries run outside the sandbox, so who can push to them matters. How trusted and untrusted differ, and how to pin versions.

Safeguard Security Team
3 min read

Jenkins shared libraries are safe only if you control who can change them. Trusted libraries run outside the Groovy sandbox and can call any method, so anyone who can push to that repository could obtain unlimited access to Jenkins. Untrusted libraries run in the sandbox like ordinary Pipeline code.

Trusted versus untrusted

TrustedUntrusted
SandboxNot sandboxedGroovy sandbox applies
Can callAny Java, Groovy, Jenkins internal or plugin methodOnly what the sandbox allows
Who configuresNeeds Overall/RunScripts, usually adminsOverall/Manage
ReplayNot supportedSupported for testing changes

Folder level libraries are always untrusted. The Jenkins documentation warns about trusted repositories directly: "anyone able to push commits to this SCM repository could obtain unlimited access to Jenkins." Treat push access to them like admin access.

Pin versions

Each library can have a default version, which can be a branch, tag or commit hash. Production should use tags or commit hashes, since a branch can move under you.

  • If "Allow default version to be overridden" is on, a Pipeline can pick another version with an annotation.
  • If it is off, scripts cannot override the default.
  • With no default, every Pipeline must name a version.
  • A replayed build reuses the exact revision of the original, even if a branch was requested.

Implicit loading

A library marked "Load implicitly" is available to every Pipeline with no annotation. That makes it convenient and also wide in reach, so only mark a library implicit when every job really needs it.

Hardening steps

  1. List your global libraries and mark which are trusted.
  2. Restrict push and merge rights on trusted repositories, and require review.
  3. Keep trusted code small. Wrap unsafe calls in narrow functions that Pipelines can use safely.
  4. Avoid @Grab in trusted libraries. The docs suggest a standalone executable installed on agents and called through a shell step.
  5. Pin to tags or commit hashes in production.
  6. Do not keep state in global variables, since shared state can be lost on controller restart.

A note on caching rules: "Versions to exclude" always takes precedence over "Versions to include."

Try it yourself

The demo shows a pull request gate, the control that should sit in front of any library repository.

Click through it at your own pace, or open it full screen.

Where Safeguard fits

Safeguard applies policies and gates on pull requests, which helps enforce review of changes to pipeline code. Start with the free scan.

Quick check

Why are trusted libraries risky?

They are not sandboxed, so whoever can push to the repository can run arbitrary code with Jenkins level access.

Should I pin to a branch?

For production, use a tag or commit hash. Branches move, which hurts reproducibility and review.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.