Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-60866][JENKINS-71513] Apply Stapler update for CSP-compliant st:bind and renderOnDemand #6865

Merged
merged 24 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
645beac
[JENKINS-60866] Apply Stapler update for CSP-compliant st:bind
daniel-beck Jul 16, 2022
fbb392e
[JENKINS-60866] Make renderOnDemand CSP-compliant
daniel-beck Jul 17, 2022
9f675a4
Thanks Spotless
daniel-beck Jul 17, 2022
60051b4
Make Stapler incrementals work
daniel-beck Jul 17, 2022
ffd7c36
Update Stapler to new incremental
daniel-beck Aug 3, 2022
2debadf
Merge branch 'master' into csp-bind
daniel-beck Aug 3, 2022
c2f5ae2
Merge remote-tracking branch 'origin/master' into csp-bind
basil Aug 26, 2022
269ac92
Merge remote-tracking branch 'jenkinsci/master' into csp-bind
daniel-beck Aug 11, 2023
bd50766
Fixup bad merge
daniel-beck Aug 11, 2023
ced608e
Update Stapler, add test demonstrating st:bind working
daniel-beck Aug 15, 2023
b076c02
Address review feedback
daniel-beck Aug 16, 2023
f5c7a84
Add test for null bind, update Stapler
daniel-beck Aug 21, 2023
1e32e73
Merge branch 'master' into csp-bind
daniel-beck Aug 21, 2023
89271e7
Checkstyle
daniel-beck Aug 21, 2023
1ff5257
More tests
daniel-beck Aug 22, 2023
e778bc4
Merge branch 'master' into csp-bind
daniel-beck Sep 25, 2023
3195579
Merge branch 'master' into csp-bind
daniel-beck Sep 26, 2023
5eef5a6
Merge remote-tracking branch 'jenkinsci/master' into csp-bind
daniel-beck Oct 12, 2023
d2ab58d
Merge branch 'master' into csp-bind
daniel-beck Oct 26, 2023
844cac1
Merge branch 'master' into csp-bind
daniel-beck Nov 1, 2023
dfe2e0d
Merge commit '09905a09d95026dcd20634ed48e810b209ce46da' into csp-bind
daniel-beck Feb 12, 2024
afb4593
Merge remote-tracking branch 'origin/csp-bind' into csp-bind
daniel-beck Feb 12, 2024
a3b14ad
Merge remote-tracking branch 'jenkinsci/master' into csp-bind
daniel-beck Feb 12, 2024
dd8a075
Use released Stapler
daniel-beck Feb 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ THE SOFTWARE.
<properties>
<asm.version>9.6</asm.version>
<slf4jVersion>2.0.12</slf4jVersion>
<stapler.version>1822.v120278426e1c</stapler.version>
<stapler.version>1839.ved17667b_a_eb_5</stapler.version>
<groovy.version>2.4.21</groovy.version>
</properties>

Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/hudson/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2242,10 +2242,19 @@ public static boolean isWipeOutPermissionEnabled() {
return SystemProperties.getBoolean("hudson.security.WipeOutPermission");
}

@Deprecated
public static String createRenderOnDemandProxy(JellyContext context, String attributesToCapture) {
return Stapler.getCurrentRequest().createJavaScriptProxy(new RenderOnDemandClosure(context, attributesToCapture));
daniel-beck marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Called from renderOnDemand.jelly to generate the parameters for the proxy object generation.
*/
@Restricted(NoExternalUse.class)
public static StaplerRequest.RenderOnDemandParameters createRenderOnDemandProxyParameters(JellyContext context, String attributesToCapture) {
daniel-beck marked this conversation as resolved.
Show resolved Hide resolved
return Stapler.getCurrentRequest().createJavaScriptProxyParameters(new RenderOnDemandClosure(context, attributesToCapture));
}

public static String getCurrentDescriptorByNameUrl() {
return Descriptor.getCurrentDescriptorByNameUrl();
}
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/resources/lib/layout/renderOnDemand.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ THE SOFTWARE.
</st:attribute>
</st:documentation>

<j:set var="parameters" value="${h.createRenderOnDemandProxyParameters(context,attrs.capture)}"/>
<x:element name="${attrs.tag?:'div'}">
<x:attribute name="class">render-on-demand ${attrs.clazz}</x:attribute>
<x:attribute name="proxy">${h.createRenderOnDemandProxy(context,attrs.capture)}</x:attribute>
<x:attribute name="data-proxy-method">${parameters.proxyMethod}</x:attribute>
<x:attribute name="data-proxy-url">${parameters.url}</x:attribute>
<x:attribute name="data-proxy-crumb">${parameters.crumb}</x:attribute>
<x:attribute name="data-proxy-url-names">${parameters.urlNames}</x:attribute>
</x:element>
</j:jelly>
176 changes: 176 additions & 0 deletions test/src/test/java/org/kohsuke/stapler/BindTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package org.kohsuke.stapler;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertThrows;

import hudson.ExtensionList;
import hudson.model.InvisibleAction;
import hudson.model.RootAction;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.htmlunit.Page;
import org.htmlunit.ScriptException;
import org.htmlunit.html.HtmlPage;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.bind.JavaScriptMethod;
import org.kohsuke.stapler.bind.WithWellKnownURL;

@RunWith(Parameterized.class)
public class BindTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@Parameterized.Parameters
public static List<String> contexts() {
return Arrays.asList("/jenkins", "");
}

public BindTest(String contextPath) {
j.contextPath = contextPath;
}

@Test
public void bindNormal() throws Exception {
final RootActionImpl root = ExtensionList.lookupSingleton(RootActionImpl.class);
try (JenkinsRule.WebClient wc = j.createWebClient()) {
final HtmlPage htmlPage = wc.goTo(root.getUrlName());
final String scriptUrl = htmlPage
.getElementsByTagName("script")
.stream()
.filter(it -> it.getAttribute("src").startsWith(j.contextPath + "/$stapler/bound/script" + j.contextPath + "/$stapler/bound/"))
.findFirst()
.orElseThrow()
.getAttribute("src");

final Page script = wc.goTo(StringUtils.removeStart(scriptUrl, j.contextPath + "/"), "application/javascript");
final String content = script.getWebResponse().getContentAsString();
assertThat(content, startsWith("varname = makeStaplerProxy('" + j.contextPath + "/$stapler/bound/"));
assertThat(content, endsWith("','test',['annotatedJsMethod1','byName1']);"));
}
assertThat(root.invocations, is(1));
}

@Test
public void bindWithWellKnownURL() throws Exception {
final RootActionWithWellKnownURL root = ExtensionList.lookupSingleton(RootActionWithWellKnownURL.class);
try (JenkinsRule.WebClient wc = j.createWebClient()) {
final HtmlPage htmlPage = wc.goTo(root.getUrlName());
final String scriptUrl = htmlPage
.getElementsByTagName("script")
.stream()
.filter(it -> it.getAttribute("src").startsWith(j.contextPath + "/$stapler/bound/script" + j.contextPath + "/theWellKnownRoot?"))
.findFirst()
.orElseThrow()
.getAttribute("src");

final Page script = wc.goTo(StringUtils.removeStart(scriptUrl, j.contextPath + "/"), "application/javascript");
assertThat(script.getWebResponse().getContentAsString(), is("varname = makeStaplerProxy('" + j.contextPath + "/theWellKnownRoot','test',['annotatedJsMethod2','byName2']);"));
}
assertThat(root.invocations, is(1));
}

@Test
public void bindNull() throws Exception {
final RootActionImpl root = ExtensionList.lookupSingleton(RootActionImpl.class);
try (JenkinsRule.WebClient wc = j.createWebClient()) {
final ScriptException exception = assertThrows(ScriptException.class, () -> wc.goTo(root.getUrlName() + "/null"));
assertThat(exception.getFailingLineNumber(), is(2));
assertThat(exception.getFailingColumnNumber(), is(0));
assertThat(exception.getMessage(), containsString("TypeError: Cannot call method \"byName1\" of null"));

final HtmlPage htmlPage = exception.getPage();
final String scriptUrl = htmlPage.getElementsByTagName("script").stream().filter(it -> it.getAttribute("src").equals(j.contextPath + "/$stapler/bound/script/null?var=varname")).findFirst().orElseThrow().getAttribute("src");

final Page script = wc.goTo(StringUtils.removeStart(scriptUrl, j.contextPath + "/"), "application/javascript");
final String content = script.getWebResponse().getContentAsString();
assertThat(content, is("varname = null;"));
}
assertThat(root.invocations, is(0));
}

@Test
public void bindUnsafe() throws Exception {
final RootActionImpl root = ExtensionList.lookupSingleton(RootActionImpl.class);
try (JenkinsRule.WebClient wc = j.createWebClient()) {
final HtmlPage htmlPage = wc.goTo(root.getUrlName() + "/unsafe-var");
final String content = htmlPage
.getElementsByTagName("script")
.stream()
.filter(it -> it.getTextContent().contains("makeStaplerProxy"))
.findFirst()
.orElseThrow()
.getTextContent();

assertThat(content, startsWith("window['varname']=makeStaplerProxy('" + j.contextPath + "/$stapler/bound/"));
assertThat(content, endsWith("','test',['annotatedJsMethod1','byName1']);"));
}
assertThat(root.invocations, is(1));
}

@Test
public void bindInlineNull() throws Exception {
final RootActionImpl root = ExtensionList.lookupSingleton(RootActionImpl.class);
try (JenkinsRule.WebClient wc = j.createWebClient()) {
final HtmlPage htmlPage = wc.goTo(root.getUrlName() + "/inline-null");
final String content = htmlPage
.getElementsByTagName("script")
.stream()
.filter(it -> it.getTextContent().contains("var inline"))
.findFirst()
.orElseThrow()
.getTextContent();

assertThat(content, containsString("var inline = null"));
}
assertThat(root.invocations, is(0));
}

@TestExtension
public static class RootActionImpl extends InvisibleAction implements RootAction {
private int invocations;

@Override
public String getUrlName() {
return "theRoot";
}

@JavaScriptMethod
public void annotatedJsMethod1(String foo) {}

public void jsByName1() {
invocations++;
}
}

@TestExtension
public static class RootActionWithWellKnownURL extends InvisibleAction implements RootAction, WithWellKnownURL {
private int invocations;

@Override
public String getUrlName() {
return "theWellKnownRoot";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a very realistic example of course, because it is a singleton (the point of WithWellKnownURL would be to look up a specific object based on parsing out a path), but good enough for purposes of tests here I suppose.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is the only implementation of WithWellKnownURL, we'll survive — if it weren't for your comments recently in the context of session serialization, I might have ripped it out entirely.

(Yes, there's another one in CloudBees, but it's long dead.)

}

@Override
public String getWellKnownUrl() {
return "/" + getUrlName();
}

@JavaScriptMethod
public void annotatedJsMethod2(String foo) {}

public void jsByName2() {
invocations++;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line no-undef
varname.byName1();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:st="jelly:stapler">
<l:layout title="The Root">
<l:main-panel>
<h1>Root Action</h1>
<st:bind var="varname" value="${it}"/>
<st:adjunct includes="org.kohsuke.stapler.BindTest.RootActionImpl.adjunct"/>
</l:main-panel>
</l:layout>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:st="jelly:stapler">
<l:layout title="The Root">
<l:main-panel>
<h1>Root Action</h1>
<script>
var inline = <st:bind value="${null}"/>
</script>
</l:main-panel>
</l:layout>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:st="jelly:stapler">
<l:layout title="The Root">
<l:main-panel>
<h1>Root Action with null</h1>
<st:bind var="varname" value="${null}"/>
<st:adjunct includes="org.kohsuke.stapler.BindTest.RootActionImpl.adjunct"/>
</l:main-panel>
</l:layout>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:st="jelly:stapler">
<l:layout title="The Root">
<l:main-panel>
<h1>Root Action</h1>
<st:bind var="window['varname']" value="${it}"/>
<st:adjunct includes="org.kohsuke.stapler.BindTest.RootActionImpl.adjunct"/>
</l:main-panel>
</l:layout>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line no-undef
varname.byName2();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:st="jelly:stapler">
<l:layout title="The Root">
<l:main-panel>
<h1>Root Action</h1>
<st:bind var="varname" value="${it}"/>
<st:adjunct includes="org.kohsuke.stapler.BindTest.RootActionWithWellKnownURL.adjunct"/>
</l:main-panel>
</l:layout>
</j:jelly>
8 changes: 7 additions & 1 deletion war/src/main/webapp/scripts/hudson-behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,13 @@ function renderOnDemand(e, callback, noBehaviour) {
if (!e || !e.classList.contains("render-on-demand")) {
return;
}
var proxy = eval(e.getAttribute("proxy"));

let proxyMethod = e.getAttribute("data-proxy-method");
let proxyUrl = e.getAttribute("data-proxy-url");
let proxyCrumb = e.getAttribute("data-proxy-crumb");
let proxyUrlNames = e.getAttribute("data-proxy-url-names").split(",");
Comment on lines +1000 to +1003
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't these be const?

Suggested change
let proxyMethod = e.getAttribute("data-proxy-method");
let proxyUrl = e.getAttribute("data-proxy-url");
let proxyCrumb = e.getAttribute("data-proxy-crumb");
let proxyUrlNames = e.getAttribute("data-proxy-url-names").split(",");
const proxyMethod = e.getAttribute("data-proxy-method");
const proxyUrl = e.getAttribute("data-proxy-url");
const proxyCrumb = e.getAttribute("data-proxy-crumb");
const proxyUrlNames = e.getAttribute("data-proxy-url-names").split(",");


var proxy = window[proxyMethod](proxyUrl, proxyCrumb, proxyUrlNames);
proxy.render(function (t) {
var contextTagName = e.parentNode.tagName;
var c;
Expand Down
Loading