Skip to content

Commit fed71e0

Browse files
daniel-beckkrisstern
authored andcommitted
Use client-side redirects in admin monitors for Chrome (#26207)
Co-authored-by: Daniel Beck <daniel-beck@users.noreply.github.com> (cherry picked from commit dc03336)
1 parent b1f5e8d commit fed71e0

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
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

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+
}

0 commit comments

Comments
 (0)