Skip to content

Commit f65d986

Browse files
committed
fixed support for Automatically Generated DSL when using script security sandbox
[FIXES JENKINS-47560]
1 parent b3a34b5 commit f65d986

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

docs/Home.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Browse the Jenkins issue tracker to see any [open issues](https://issues.jenkins
2929

3030
## Release Notes
3131
* 1.67 (unreleased)
32+
* Fixed support for [[Automatically Generated DSL]] when using script security sandbox
33+
([JENKINS-47560](https://issues.jenkins-ci.org/browse/JENKINS-47560))
3234
* Enhanced support for the [Groovy Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin)
3335
([JENKINS-44256](https://issues.jenkins-ci.org/browse/JENKINS-44256))
3436
* Support for the older versions of the [Groovy Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin) is
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package javaposse.jobdsl.plugin
22

3+
import javaposse.jobdsl.dsl.AbstractExtensibleContext
34
import javaposse.jobdsl.dsl.Context
5+
import javaposse.jobdsl.plugin.structs.DescribableContext
6+
import javaposse.jobdsl.plugin.structs.DescribableListContext
47
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.AbstractWhitelist
58

69
import java.lang.reflect.Method
@@ -9,8 +12,15 @@ import java.lang.reflect.Method
912
* Allows methods defined in {@link Context}.
1013
*/
1114
class JobDslWhitelist extends AbstractWhitelist {
15+
private static final Method INVOKE_METHOD = GroovyObject.getDeclaredMethod('invokeMethod', String, Object)
16+
private static final Set<Class> DYNAMIC_CONTEXTS = [
17+
AbstractExtensibleContext, DescribableContext, DescribableListContext
18+
]
19+
1220
@Override
1321
boolean permitsMethod(Method method, Object receiver, Object[] args) {
14-
Context.isAssignableFrom(method.declaringClass)
22+
Context.isAssignableFrom(method.declaringClass) ||
23+
(method == INVOKE_METHOD && receiver.class.classLoader == JobDslWhitelist.classLoader &&
24+
DYNAMIC_CONTEXTS.any { context -> context.isInstance(receiver) })
1525
}
1626
}

job-dsl-plugin/src/test/groovy/javaposse/jobdsl/plugin/ExecuteDslScriptsSpec.groovy

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,32 @@ class ExecuteDslScriptsSpec extends Specification {
14231423
assert ScriptApproval.get().pendingScripts*.script == []
14241424
}
14251425
1426+
def 'run script with dynamic DSL in sandbox'() {
1427+
setup:
1428+
String script = 'job("test") { triggers { cron { spec("@daily") } } }'
1429+
1430+
jenkinsRule.instance.securityRealm = jenkinsRule.createDummySecurityRealm()
1431+
jenkinsRule.instance.authorizationStrategy = new MockAuthorizationStrategy()
1432+
.grant(Jenkins.READ, Item.READ, Item.CONFIGURE, Item.CREATE, Computer.BUILD).everywhere().to('dev')
1433+
1434+
FreeStyleProject job = jenkinsRule.createFreeStyleProject('seed')
1435+
job.buildersList.add(new ExecuteDslScripts(scriptText: script, sandbox: true))
1436+
setupQIA('dev', job)
1437+
1438+
when:
1439+
jenkinsRule.submit(jenkinsRule.createWebClient().login('dev').getPage(job, 'configure').getFormByName('config'))
1440+
1441+
then:
1442+
assert ScriptApproval.get().pendingScripts*.script == []
1443+
1444+
when:
1445+
FreeStyleBuild build = job.scheduleBuild2(0).get()
1446+
1447+
then:
1448+
build.result == SUCCESS
1449+
assert ScriptApproval.get().pendingScripts*.script == []
1450+
}
1451+
14261452
def 'run script in sandbox with unapproved signature'() {
14271453
setup:
14281454
String script = 'System.exit(0)'

0 commit comments

Comments
 (0)