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

Clarify SECURITY-3315 error code on client side #9930

Merged
merged 1 commit into from
Nov 9, 2024
Merged
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
20 changes: 17 additions & 3 deletions core/src/main/java/hudson/cli/CLIAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,26 @@
return WebSockets.isSupported();
}

/**
* Unlike {@link HttpResponses#errorWithoutStack} this sends the message in a header rather than the body.
* (Currently the WebSocket CLI is unable to process the body in an error message.)
*/
private static HttpResponse statusWithExplanation(int code, String errorMessage) {
return new HttpResponse() {
@Override
public void generateResponse(StaplerRequest2 req, StaplerResponse2 rsp, Object node) {
Comment on lines +126 to +128
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that if anyone had been using this as a @FunctionalInterface before, that would break as of jenkinsci/stapler#482. Not sure if this was only source-incompatible rather than binary-incompatible? https://docs.oracle.com/javase/specs/jls/se23/html/jls-13.html#jls-13.5.4 does not seem to mention SAMs.

rsp.setStatus(code);
rsp.setHeader("X-CLI-Error", errorMessage);
}
};
}

/**
* WebSocket endpoint.
*/
public HttpResponse doWs(StaplerRequest2 req) {
if (!WebSockets.isSupported()) {
return HttpResponses.notFound();
return statusWithExplanation(HttpServletResponse.SC_NOT_FOUND, "WebSocket is not supported in this servlet container (try the built-in Jetty instead)");

Check warning on line 140 in core/src/main/java/hudson/cli/CLIAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 140 is not covered by tests
}
if (ALLOW_WEBSOCKET == null) {
final String actualOrigin = req.getHeader("Origin");
Expand All @@ -141,10 +155,10 @@

if (actualOrigin == null || !actualOrigin.equals(expectedOrigin)) {
LOGGER.log(Level.FINE, () -> "Rejecting origin: " + actualOrigin + "; expected was from request: " + expectedOrigin);
return HttpResponses.forbidden();
return statusWithExplanation(HttpServletResponse.SC_FORBIDDEN, "Unexpected request origin (check your reverse proxy settings)");
}
} else if (!ALLOW_WEBSOCKET) {
return HttpResponses.forbidden();
return statusWithExplanation(HttpServletResponse.SC_FORBIDDEN, "WebSocket support for CLI disabled for this controller");
}
Authentication authentication = Jenkins.getAuthentication2();
return WebSockets.upgrade(new WebSocketSession() {
Expand Down
Loading