Skip to content

Commit

Permalink
Merge pull request #199 from rudolfwalter/JENKINS-50843
Browse files Browse the repository at this point in the history
Allow calling Closure elements of Maps as methods
  • Loading branch information
svanoort committed Apr 26, 2018
2 parents d6b52e9 + b841fc0 commit 23fdbbb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Expand Up @@ -25,6 +25,7 @@
package org.jenkinsci.plugins.scriptsecurity.sandbox.groovy;

import com.google.common.collect.ImmutableSet;
import groovy.lang.Closure;
import groovy.lang.GroovyRuntimeException;
import groovy.lang.MetaMethod;
import groovy.lang.MissingMethodException;
Expand All @@ -35,6 +36,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -114,6 +116,14 @@ final class SandboxInterceptor extends GroovyInterceptor {
throw StaticWhitelist.rejectStaticMethod(foundDgmMethod);
}

// allow calling Closure elements of Maps as methods
if (receiver instanceof Map) {
Object element = onMethodCall(invoker, receiver, "get", method);
if (element instanceof Closure) {
return onMethodCall(invoker, element, "call", args);
}
}

// if no matching method, look for catchAll "invokeMethod"
try {
receiver.getClass().getMethod("invokeMethod", String.class, Object.class);
Expand Down
Expand Up @@ -1080,4 +1080,13 @@ public void checkedGetPropertyOnCollection() throws Exception {
"def l = [new " + snb + "('a'), new " + snb +"('b'), new " + snb + "('c')]\n" +
"return l.other\n");
}

@Issue("JENKINS-50843")
@Test
public void callClosureElementOfMapAsMethod() throws Exception {
assertEvaluate(new GenericWhitelist(), "hello", "def m = [ f: {return 'hello'} ]; m.f()");
assertEvaluate(new GenericWhitelist(), 15, "def m = [ f: {a -> return a*3} ]; m.f(5)");
assertEvaluate(new GenericWhitelist(), "a=hello,b=10", "def m = [ f: {a,b -> return \"a=${a},b=${b}\"} ]; m.f('hello',10)");
assertEvaluate(new GenericWhitelist(), 2, "def m = [ f: {it.size()} ]; m.f(foo:0, bar:1)");
}
}

0 comments on commit 23fdbbb

Please sign in to comment.