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

[UNDERTOW-2325] change SecureCookieCommitListener to also look throug… #1540

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.undertow.server;

import io.undertow.server.handlers.Cookie;
import io.undertow.server.handlers.CookieImpl;
import io.undertow.util.HeaderValues;
import io.undertow.util.Headers;

/**
* Sets the <pre>secure</pre> attribute on all response cookies.
Expand All @@ -11,8 +14,28 @@ public enum SecureCookieCommitListener implements ResponseCommitListener {

@Override
public void beforeCommit(HttpServerExchange exchange) {
handleResponseCookies(exchange);
handleCookiesSetViaHeaders(exchange);
}

private void handleResponseCookies(HttpServerExchange exchange) {
for (Cookie cookie : exchange.responseCookies()) {
cookie.setSecure(true);
}
}

private void handleCookiesSetViaHeaders(HttpServerExchange exchange) {
HeaderValues cookieHeaders = exchange.getResponseHeaders().get(Headers.SET_COOKIE);
if (cookieHeaders != null) {
for (String cookieHeader : cookieHeaders) {
String[] parts = cookieHeader.split("=", 2);
String cookieName = parts[0];
String cookieValue = (parts.length > 1) ? parts[1] : null;
CookieImpl cookie = new CookieImpl(cookieName, cookieValue);
cookie.setSecure(true);
exchange.setResponseCookie(cookie);
}
exchange.getResponseHeaders().remove(Headers.SET_COOKIE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.security.GeneralSecurityException;

import io.undertow.util.Headers;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
Expand Down Expand Up @@ -74,4 +75,31 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
}
}

@Test
public void testSecureCookieHandlerWithManuallySetCookie() throws IOException, GeneralSecurityException {

DefaultServer.setRootHandler(new SecureCookieHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.SET_COOKIE, "cookie=value");
}
}));

DefaultServer.startSSLServer();
TestHttpClient client = new TestHttpClient();
client.setSSLContext(DefaultServer.getClientSSLContext());
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());

Header header = result.getFirstHeader("set-cookie");
Assert.assertEquals("cookie=value; secure", header.getValue());
FileUtils.readFile(result.getEntity().getContent());
} finally {
client.getConnectionManager().shutdown();
DefaultServer.stopSSLServer();
}
}

}