Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import jenkins.security.stapler.StaplerDispatchable;
import jenkins.util.ClientHttpRedirect;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
Expand Down Expand Up @@ -127,7 +128,7 @@
// of course the irony is that this redirect won't work
return HttpResponses.redirectViaContextPath("/manage");
} else {
return new HttpRedirect("https://www.jenkins.io/redirect/troubleshooting/broken-reverse-proxy");
return new ClientHttpRedirect("https://www.jenkins.io/redirect/troubleshooting/broken-reverse-proxy");

Check warning on line 131 in core/src/main/java/hudson/diagnosis/ReverseProxySetupMonitor.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 131 is not covered by tests
}
}

Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/hudson/util/HttpResponses.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import jenkins.util.ClientHttpRedirect;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.HttpResponse;
Expand All @@ -51,6 +52,16 @@
return staticResource(f.toURI().toURL());
}

/**
* Redirect to the given URL via a client-side JS/meta tag redirect.
* @param url the URL to redirect to
* @return the HttpResponse
* @since TODO
*/
public static HttpResponse clientRedirectTo(String url) {
Copy link
Member

Choose a reason for hiding this comment

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

Ideally @Restricted(NoExternalUse.class) but not sure what current LTS backporting practices of new API are. Ignore if we haven't bothered with that recently.

Copy link
Member

Choose a reason for hiding this comment

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

not bothered with it recently

return new ClientHttpRedirect(url);

Check warning on line 62 in core/src/main/java/hudson/util/HttpResponses.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 62 is not covered by tests
}

/**
* Create an empty "ok" response.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
import java.util.NavigableMap;
import java.util.TreeMap;
import jenkins.model.Jenkins;
import jenkins.util.ClientHttpRedirect;
import jenkins.util.SystemProperties;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.HttpRedirect;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -138,7 +138,7 @@
disable(true);
return HttpResponses.forwardToPreviousPage();
} else {
return new HttpRedirect("https://jenkins.io/redirect/java-support/");
return new ClientHttpRedirect("https://jenkins.io/redirect/java-support/");

Check warning on line 141 in core/src/main/java/jenkins/monitor/JavaVersionRecommendationAdminMonitor.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 141 is not covered by tests
}
}

Expand Down
12 changes: 7 additions & 5 deletions core/src/main/java/jenkins/security/csp/impl/CspFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
final String headerName = cspDecorator.getContentSecurityPolicyHeaderName();
final boolean headerShouldBeSet = headerName != null;

// This is the preliminary value outside Stapler request handling (and providing a context object)
final String headerValue = cspDecorator.getContentSecurityPolicyHeaderValue(req);

final boolean isResourceRequest = ResourceDomainConfiguration.isResourceRequest(req);

String headerValue = "";

if (headerShouldBeSet && !isResourceRequest) {
// This is the preliminary value outside Stapler request handling (and providing a context object)
headerValue = cspDecorator.getContentSecurityPolicyHeaderValue(req);

// The Filter/Decorator approach needs us to "set" headers rather than "add", so no additional endpoints are supported at the moment.
final String reportingEndpoints = cspDecorator.getReportingEndpointsHeaderValue(req);
if (reportingEndpoints != null) {
Expand All @@ -80,10 +82,10 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
try {
chain.doFilter(req, rsp);
} finally {
if (headerShouldBeSet) {
if (headerShouldBeSet && !isResourceRequest) {
try {
final String actualHeader = rsp.getHeader(headerName);
if (!isResourceRequest && hasUnexpectedDifference(headerValue, actualHeader)) {
if (hasUnexpectedDifference(headerValue, actualHeader)) {
LOGGER.log(Level.FINE, "CSP header has unexpected differences: Expected '" + headerValue + "' but got '" + actualHeader + "'");
}
} catch (RuntimeException e) {
Expand Down
49 changes: 49 additions & 0 deletions core/src/main/java/jenkins/util/ClientHttpRedirect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* The MIT License
*
* Copyright (c) 2026, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package jenkins.util;

import hudson.Util;
import jakarta.servlet.ServletException;
import java.io.IOException;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;

/**
* An HTTP response that redirects the client to the given URL.
* Unlike {@link org.kohsuke.stapler.HttpRedirect}, this implements a client-side redirect (using meta tag and/or JavaScript).
* This allows the redirect to work even when Content Security Policy is enforced in Chrome
* (which applies {@code form-action} to redirects after form submission).
* @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>
* @see <a href="https://github.com/w3c/webappsec-csp/issues/8">Content Security Policy issue discussing this behavior</a>
* @since TODO
*/
public record ClientHttpRedirect(String redirectUrl) implements HttpResponse {
Copy link
Member

Choose a reason for hiding this comment

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

Ideally @Restricted(NoExternalUse.class) but not sure what current LTS backporting practices of new API are. Ignore if we haven't bothered with that recently.

@Override
public void generateResponse(StaplerRequest2 req, StaplerResponse2 rsp, Object o) throws IOException, ServletException {
rsp.setContentType("text/html;charset=UTF-8");
Util.printRedirect(req.getContextPath(), redirectUrl, redirectUrl, rsp.getWriter());
}

Check warning on line 48 in core/src/main/java/jenkins/util/ClientHttpRedirect.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 43-48 are not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ THE SOFTWARE.
<form method="post" action="${rootURL}/${it.url}/act" name="${it.id}">
<div>
<f:bottomButtonBar>
<f:submit name="setup" value="${%Set up now}"/>
<f:submit name="setup" value="${%Go to configuration}"/>
<f:submit primary="false" name="defer" value="${%Cancel}"/>
</f:bottomButtonBar>
</div>
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/lib/layout/keyboard-shortcut.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ THE SOFTWARE.
</j:choose>
</div>
</j:set>
<j:out value="${h.formatMessage(message, h.translateModifierKeysForUsersPlatform(body))}"/>
<j:out value="${h.formatMessage(attrs.message, h.translateModifierKeysForUsersPlatform(body))}"/>
</div>
</j:set>

Expand Down
Loading
Loading