2525package org .jenkinsci .plugins .workflow .cps ;
2626
2727import com .cloudbees .groovy .cps .CpsTransformer ;
28+ import com .gargoylesoftware .htmlunit .TextPage ;
29+ import com .gargoylesoftware .htmlunit .html .DomNodeUtil ;
30+ import com .gargoylesoftware .htmlunit .html .HtmlPage ;
2831import hudson .Functions ;
2932import hudson .model .Computer ;
3033import hudson .model .Executor ;
34+ import hudson .model .Item ;
3135import hudson .model .Result ;
36+
3237import java .util .logging .Level ;
38+
39+ import hudson .security .GlobalMatrixAuthorizationStrategy ;
40+ import jenkins .model .Jenkins ;
3341import org .jenkinsci .plugins .workflow .flow .FlowExecutionOwner ;
3442import org .jenkinsci .plugins .workflow .job .WorkflowJob ;
3543import org .jenkinsci .plugins .workflow .job .WorkflowRun ;
3644import org .jenkinsci .plugins .workflow .test .steps .SemaphoreStep ;
45+
46+ import static org .hamcrest .Matchers .containsString ;
3747import static org .junit .Assert .*;
3848
3949import org .junit .Assert ;
4454import org .junit .Test ;
4555import org .jvnet .hudson .test .BuildWatcher ;
4656import org .jvnet .hudson .test .Issue ;
57+ import org .jvnet .hudson .test .JenkinsRule ;
4758import org .jvnet .hudson .test .LoggerRule ;
4859
4960public class CpsFlowDefinition2Test extends AbstractCpsFlowTest {
@@ -169,6 +180,15 @@ public void superCallsSandboxed() throws Exception {
169180
170181 @ Test
171182 public void sandboxInvokerUsed () throws Exception {
183+ jenkins .jenkins .setSecurityRealm (jenkins .createDummySecurityRealm ());
184+ GlobalMatrixAuthorizationStrategy gmas = new GlobalMatrixAuthorizationStrategy ();
185+ // Set up a user with RUN_SCRIPTS and one without..
186+ gmas .add (Jenkins .RUN_SCRIPTS , "runScriptsUser" );
187+ gmas .add (Jenkins .READ , "runScriptsUser" );
188+ gmas .add (Item .READ , "runScriptsUser" );
189+ gmas .add (Jenkins .READ , "otherUser" );
190+ gmas .add (Item .READ , "otherUser" );
191+ jenkins .jenkins .setAuthorizationStrategy (gmas );
172192 WorkflowJob job = jenkins .jenkins .createProject (WorkflowJob .class , "p" );
173193 job .setDefinition (new CpsFlowDefinition ("[a: 1, b: 2].collectEntries { k, v ->\n " +
174194 " Jenkins.getInstance()\n " +
@@ -177,6 +197,27 @@ public void sandboxInvokerUsed() throws Exception {
177197
178198 WorkflowRun r = jenkins .assertBuildStatus (Result .FAILURE , job .scheduleBuild2 (0 ).get ());
179199 jenkins .assertLogContains ("org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance" , r );
200+ jenkins .assertLogContains ("Scripts not permitted to use staticMethod jenkins.model.Jenkins getInstance. " + Messages .SandboxContinuable_ScriptApprovalLink (), r );
201+
202+ JenkinsRule .WebClient wc = jenkins .createWebClient ();
203+
204+ wc .login ("runScriptsUser" );
205+ // make sure we see the annotation for the RUN_SCRIPTS user.
206+ HtmlPage rsp = wc .getPage (r , "console" );
207+ assertEquals (1 , DomNodeUtil .selectNodes (rsp , "//A[@href='" + jenkins .contextPath + "/scriptApproval']" ).size ());
208+
209+ // make sure raw console output doesn't include the garbage and has the right message.
210+ TextPage raw = (TextPage )wc .goTo (r .getUrl ()+"consoleText" ,"text/plain" );
211+ assertThat (raw .getContent (), containsString (" getInstance. " + Messages .SandboxContinuable_ScriptApprovalLink ()));
212+
213+ wc .login ("otherUser" );
214+ // make sure we don't see the link for the other user.
215+ HtmlPage rsp2 = wc .getPage (r , "console" );
216+ assertEquals (0 , DomNodeUtil .selectNodes (rsp2 , "//A[@href='" + jenkins .contextPath + "/scriptApproval']" ).size ());
217+
218+ // make sure raw console output doesn't include the garbage and has the right message.
219+ TextPage raw2 = (TextPage )wc .goTo (r .getUrl ()+"consoleText" ,"text/plain" );
220+ assertThat (raw2 .getContent (), containsString (" getInstance. " + Messages .SandboxContinuable_ScriptApprovalLink ()));
180221 }
181222
182223 @ Issue ("SECURITY-551" )
0 commit comments