Skip to content

Commit 897fbb4

Browse files
authored
Backport 2.541.3 (#26400)
2 parents 644d737 + fed71e0 commit 897fbb4

File tree

9 files changed

+176
-108
lines changed

9 files changed

+176
-108
lines changed

core/src/main/java/hudson/diagnosis/ReverseProxySetupMonitor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.logging.Logger;
3535
import jenkins.model.Jenkins;
3636
import jenkins.security.stapler.StaplerDispatchable;
37+
import jenkins.util.ClientHttpRedirect;
3738
import org.jenkinsci.Symbol;
3839
import org.kohsuke.accmod.Restricted;
3940
import org.kohsuke.accmod.restrictions.DoNotUse;
@@ -127,7 +128,7 @@ public HttpResponse doAct(@QueryParameter String no) throws IOException {
127128
// of course the irony is that this redirect won't work
128129
return HttpResponses.redirectViaContextPath("/manage");
129130
} else {
130-
return new HttpRedirect("https://www.jenkins.io/redirect/troubleshooting/broken-reverse-proxy");
131+
return new ClientHttpRedirect("https://www.jenkins.io/redirect/troubleshooting/broken-reverse-proxy");
131132
}
132133
}
133134

core/src/main/java/hudson/util/HttpResponses.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.IOException;
3232
import java.nio.charset.StandardCharsets;
3333
import java.util.Map;
34+
import jenkins.util.ClientHttpRedirect;
3435
import net.sf.json.JSONArray;
3536
import net.sf.json.JSONObject;
3637
import org.kohsuke.stapler.HttpResponse;
@@ -51,6 +52,16 @@ public static HttpResponse staticResource(File f) throws IOException {
5152
return staticResource(f.toURI().toURL());
5253
}
5354

55+
/**
56+
* Redirect to the given URL via a client-side JS/meta tag redirect.
57+
* @param url the URL to redirect to
58+
* @return the HttpResponse
59+
* @since TODO
60+
*/
61+
public static HttpResponse clientRedirectTo(String url) {
62+
return new ClientHttpRedirect(url);
63+
}
64+
5465
/**
5566
* Create an empty "ok" response.
5667
*

core/src/main/java/jenkins/monitor/JavaVersionRecommendationAdminMonitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838
import java.util.NavigableMap;
3939
import java.util.TreeMap;
4040
import jenkins.model.Jenkins;
41+
import jenkins.util.ClientHttpRedirect;
4142
import jenkins.util.SystemProperties;
4243
import org.jenkinsci.Symbol;
4344
import org.kohsuke.accmod.Restricted;
4445
import org.kohsuke.accmod.restrictions.DoNotUse;
4546
import org.kohsuke.accmod.restrictions.NoExternalUse;
46-
import org.kohsuke.stapler.HttpRedirect;
4747
import org.kohsuke.stapler.HttpResponse;
4848
import org.kohsuke.stapler.HttpResponses;
4949
import org.kohsuke.stapler.QueryParameter;
@@ -138,7 +138,7 @@ public HttpResponse doAct(@QueryParameter String no) throws IOException {
138138
disable(true);
139139
return HttpResponses.forwardToPreviousPage();
140140
} else {
141-
return new HttpRedirect("https://jenkins.io/redirect/java-support/");
141+
return new ClientHttpRedirect("https://jenkins.io/redirect/java-support/");
142142
}
143143
}
144144

core/src/main/java/jenkins/security/csp/impl/CspFilter.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
6363
final String headerName = cspDecorator.getContentSecurityPolicyHeaderName();
6464
final boolean headerShouldBeSet = headerName != null;
6565

66-
// This is the preliminary value outside Stapler request handling (and providing a context object)
67-
final String headerValue = cspDecorator.getContentSecurityPolicyHeaderValue(req);
68-
6966
final boolean isResourceRequest = ResourceDomainConfiguration.isResourceRequest(req);
7067

68+
String headerValue = "";
69+
7170
if (headerShouldBeSet && !isResourceRequest) {
71+
// This is the preliminary value outside Stapler request handling (and providing a context object)
72+
headerValue = cspDecorator.getContentSecurityPolicyHeaderValue(req);
73+
7274
// The Filter/Decorator approach needs us to "set" headers rather than "add", so no additional endpoints are supported at the moment.
7375
final String reportingEndpoints = cspDecorator.getReportingEndpointsHeaderValue(req);
7476
if (reportingEndpoints != null) {
@@ -80,10 +82,10 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
8082
try {
8183
chain.doFilter(req, rsp);
8284
} finally {
83-
if (headerShouldBeSet) {
85+
if (headerShouldBeSet && !isResourceRequest) {
8486
try {
8587
final String actualHeader = rsp.getHeader(headerName);
86-
if (!isResourceRequest && hasUnexpectedDifference(headerValue, actualHeader)) {
88+
if (hasUnexpectedDifference(headerValue, actualHeader)) {
8789
LOGGER.log(Level.FINE, "CSP header has unexpected differences: Expected '" + headerValue + "' but got '" + actualHeader + "'");
8890
}
8991
} catch (RuntimeException e) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2026, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package jenkins.util;
26+
27+
import hudson.Util;
28+
import jakarta.servlet.ServletException;
29+
import java.io.IOException;
30+
import org.kohsuke.stapler.HttpResponse;
31+
import org.kohsuke.stapler.StaplerRequest2;
32+
import org.kohsuke.stapler.StaplerResponse2;
33+
34+
/**
35+
* An HTTP response that redirects the client to the given URL.
36+
* Unlike {@link org.kohsuke.stapler.HttpRedirect}, this implements a client-side redirect (using meta tag and/or JavaScript).
37+
* This allows the redirect to work even when Content Security Policy is enforced in Chrome
38+
* (which applies {@code form-action} to redirects after form submission).
39+
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/form-action">MDN documentation on form-action</a>
40+
* @see <a href="https://github.com/w3c/webappsec-csp/issues/8">Content Security Policy issue discussing this behavior</a>
41+
* @since TODO
42+
*/
43+
public record ClientHttpRedirect(String redirectUrl) implements HttpResponse {
44+
@Override
45+
public void generateResponse(StaplerRequest2 req, StaplerResponse2 rsp, Object o) throws IOException, ServletException {
46+
rsp.setContentType("text/html;charset=UTF-8");
47+
Util.printRedirect(req.getContextPath(), redirectUrl, redirectUrl, rsp.getWriter());
48+
}
49+
}

core/src/main/resources/jenkins/security/csp/impl/CspRecommendation/index.jelly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ THE SOFTWARE.
3838
<form method="post" action="${rootURL}/${it.url}/act" name="${it.id}">
3939
<div>
4040
<f:bottomButtonBar>
41-
<f:submit name="setup" value="${%Set up now}"/>
41+
<f:submit name="setup" value="${%Go to configuration}"/>
4242
<f:submit primary="false" name="defer" value="${%Cancel}"/>
4343
</f:bottomButtonBar>
4444
</div>

core/src/main/resources/lib/layout/keyboard-shortcut.jelly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ THE SOFTWARE.
5353
</j:choose>
5454
</div>
5555
</j:set>
56-
<j:out value="${h.formatMessage(message, h.translateModifierKeysForUsersPlatform(body))}"/>
56+
<j:out value="${h.formatMessage(attrs.message, h.translateModifierKeysForUsersPlatform(body))}"/>
5757
</div>
5858
</j:set>
5959

0 commit comments

Comments
 (0)