Skip to content

Commit

Permalink
[FIXED JENKINS-26541] Need to check for RejectedAccessException wrapp…
Browse files Browse the repository at this point in the history
…ed in ParallelStepException.

Originally-Committed-As: fb174fdabbb46f9f84ef84a83dcccaa85252713f
  • Loading branch information
jglick committed Jan 22, 2015
1 parent 963cfbe commit c99b957
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Expand Up @@ -27,7 +27,10 @@
import hudson.model.Result;
import hudson.model.Slave;
import hudson.model.queue.QueueTaskFuture;
import java.util.Collections;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
import org.jenkinsci.plugins.workflow.actions.LogAction;
import org.jenkinsci.plugins.workflow.cps.AbstractCpsFlowTest;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
Expand All @@ -40,6 +43,7 @@
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

/**
Expand Down Expand Up @@ -182,14 +186,27 @@ public void sandbox() throws Exception {
/**
* If a prohibited method is called, execution should fail.
*/
@Issue("JENKINS-26541")
@Test
public void sandboxRejection() throws Exception {
p.setDefinition(new CpsFlowDefinition("Jenkins.getInstance();", true));

assertRejected("Jenkins.getInstance()");
assertRejected("parallel(main: {Jenkins.getInstance()})");
assertRejected("parallel(main: {parallel(main2: {Jenkins.getInstance()})})");
assertRejected("node {parallel(main: {ws {parallel(main2: {ws {Jenkins.getInstance()}})}})}");
}
private void assertRejected(String script) throws Exception {
String signature = "staticMethod jenkins.model.Jenkins getInstance";
ScriptApproval scriptApproval = ScriptApproval.get();
scriptApproval.denySignature(signature);
assertEquals(Collections.emptySet(), scriptApproval.getPendingSignatures());
p.setDefinition(new CpsFlowDefinition(script, true));
WorkflowRun b = p.scheduleBuild2(0).get();

jenkins.assertLogContains("org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance", b);
jenkins.assertLogContains("org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use " + signature, b);
jenkins.assertBuildStatus(Result.FAILURE, b);
Set<ScriptApproval.PendingSignature> pendingSignatures = scriptApproval.getPendingSignatures();
assertEquals(script, 1, pendingSignatures.size());
assertEquals(signature, pendingSignatures.iterator().next().signature);

}

/**
Expand Down
Expand Up @@ -9,6 +9,7 @@
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;

import java.util.concurrent.Callable;
import javax.annotation.CheckForNull;

/**
* {@link Continuable} that executes code inside sandbox execution.
Expand All @@ -30,9 +31,9 @@ public Outcome run0(final Outcome cn) {
@Override
public Outcome call() {
Outcome outcome = SandboxContinuable.super.run0(cn);
Throwable t = outcome.getAbnormal();
if (t instanceof RejectedAccessException) {
ScriptApproval.get().accessRejected((RejectedAccessException) t, ApprovalContext.create());
RejectedAccessException x = findRejectedAccessException(outcome.getAbnormal());
if (x != null) {
ScriptApproval.get().accessRejected(x, ApprovalContext.create());
}
return outcome;
}
Expand All @@ -43,4 +44,15 @@ public Outcome call() {
throw new AssertionError(e); // Callable doesn't throw anything
}
}

private static @CheckForNull RejectedAccessException findRejectedAccessException(@CheckForNull Throwable t) {
if (t == null) {
return null;
} else if (t instanceof RejectedAccessException) {
return (RejectedAccessException) t;
} else {
return findRejectedAccessException(t.getCause());
}
}

}

0 comments on commit c99b957

Please sign in to comment.