Keywords: Jenkins | Groovy Sandbox | Script Security | CI/CD Pipeline | Permission Approval
Abstract: This technical paper provides an in-depth analysis of security restrictions encountered in Jenkins CI/CD pipelines when executing Groovy scripts, specifically the Scripts not permitted to use method groovy.lang.GroovyObject error. Through detailed technical examination and comparison of multiple solutions, it helps developers understand Jenkins sandbox security mechanisms and offers complete resolution paths from quick fixes to advanced configurations. The article combines practical cases to explain different approaches including script approval, sandbox mode disabling, and complete script security disabling, along with their applicable scenarios and risk considerations.
Problem Background and Technical Analysis
In Jenkins 2.x continuous integration environments, Groovy pipeline scripts run by default within a strict security sandbox. This design aims to prevent malicious code execution but also restricts certain legitimate Groovy method calls. When users attempt to read Maven project version information using readFile("${path}/pom.xml"), they encounter the org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod exception.
The root cause of this error lies in Jenkins' static whitelist mechanism. When a script attempts to invoke Groovy methods not explicitly permitted, the sandbox interceptor denies access. During file content reading and regular expression matching, the involved GString method calls trigger security restrictions.
Core Solutions
Quick Approval Solution
The most direct and effective resolution method utilizes Jenkins' built-in script approval functionality. Administrators can follow these steps:
- Log into the Jenkins management interface and navigate to
Manage Jenkins > In-process Script Approval - Locate the relevant Groovy method calls in the pending command list
- Click the
Approvebutton to grant execution permission for the method
This approach maintains system security while resolving the current execution barrier. Approved methods are added to the whitelist, eliminating the need for repeated approvals for subsequent identical calls.
Sandbox Mode Disabling Solution
For scenarios requiring greater flexibility, consider disabling sandbox mode for specific projects:
pipeline {
agent any
options {
skipDefaultCheckout true
}
stages {
stage('Build') {
steps {
script {
// Complete script in non-sandbox mode
def version = readVersionFromPom()
echo "Project version: ${version}"
}
}
}
}
}
def readVersionFromPom() {
def matcher = readFile('pom.xml') =~ '<version>(.+)</version>'
return matcher ? matcher[0][1] : null
}Uncheck the Use Groovy Sandbox option in project configuration, and the entire script will undergo one-time approval as a whole. This method is suitable for trusted scripts but requires administrator-level privileges.
Complete Script Security Disabling
In extreme cases, completely disable script security by installing the Permissive Script Security plugin and modifying Jenkins startup parameters:
<arguments>
-Dpermissive-script-security.enabled=true
-Xrs -Xmx4096m
-Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle
-jar "%BASE%\jenkins.war"
--httpPort=80
--webroot="%BASE%\war"
</arguments>This solution completely removes all security restrictions and is recommended only for fully controlled internal environments after thorough security risk assessment.
Technical Deep Dive
Jenkins' script security mechanism is implemented based on Groovy sandbox technology, ensuring security through static code analysis and runtime interception. When a script attempts to invoke groovy.lang.GroovyObject's invokeMethod, the sandbox checks whether the method is in the whitelist.
In file reading and regular expression matching scenarios, GString's dynamic method calls trigger the security mechanism. Although this design increases development complexity, it effectively prevents potential code injection and system call risks.
Best Practice Recommendations
Based on different scenario requirements, a layered solution approach is recommended:
- Development/Testing Environments: Use script approval mechanisms to maintain security while providing flexibility
- Production Environments: Prioritize sandbox mode, with selective approval only when necessary
- Internal Trusted Environments: Evaluate using non-sandbox mode but establish strict code review processes
Through proper security policy configuration, organizations can leverage Jenkins pipeline scripts' powerful functionality while ensuring system security.