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

Set domain on session cookie in session handler #2687

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.
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 @@ -47,6 +47,12 @@ public interface SessionHandler extends PlatformHandler {
*/
String DEFAULT_SESSION_COOKIE_NAME = "vertx-web.session";

/**
* Default domain of session cookie i.e. no domain is set. More info:
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#domaindomain-value
*/
String DEFAULT_SESSION_COOKIE_DOMAIN = null;

/**
* Default path of session cookie
*/
Expand Down Expand Up @@ -148,6 +154,19 @@ static SessionHandler create(SessionStore sessionStore) {
@Fluent
SessionHandler setSessionCookieName(String sessionCookieName);

/**
* Set the session cookie domain. Only the current domain can be set as the
* value, or a domain of a higher order, unless it is a public suffix. Setting
* the domain will make the cookie available to it, as well as to all its
* subdomains. If omitted, this attribute defaults to the host of the current
* document URL, not including subdomains.
*
* @param sessionCookieDomain the session cookie domain
* @return a reference to this, so the API can be used fluently
*/
@Fluent
SessionHandler setSessionCookieDomain(String sessionCookieDomain);

/**
* Set the session cookie path
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class SessionHandlerImpl implements SessionHandler {
private final SessionStore sessionStore;

private String sessionCookieName = DEFAULT_SESSION_COOKIE_NAME;
private String sessionCookieDomain = DEFAULT_SESSION_COOKIE_DOMAIN;
private String sessionCookiePath = DEFAULT_SESSION_COOKIE_PATH;
private long sessionTimeout = DEFAULT_SESSION_TIMEOUT;
private boolean nagHttps = DEFAULT_NAG_HTTPS;
Expand Down Expand Up @@ -90,6 +91,12 @@ public SessionHandler setCookieHttpOnlyFlag(boolean httpOnly) {
return this;
}

@Override
public SessionHandler setSessionCookieDomain(String sessionCookieDomain) {
this.sessionCookieDomain = sessionCookieDomain;
return this;
}

@Override
public SessionHandler setSessionCookieName(String sessionCookieName) {
this.sessionCookieName = sessionCookieName;
Expand Down Expand Up @@ -150,6 +157,9 @@ public Future<Void> flush(RoutingContext context, boolean ignoreStatus) {
* @param cookie the cookie to set
*/
private void setCookieProperties(Cookie cookie, boolean expired) {
if (sessionCookieDomain != null) {
cookie.setDomain(sessionCookieDomain);
}
cookie.setPath(sessionCookiePath);
cookie.setSecure(sessionCookieSecure);
cookie.setHttpOnly(sessionCookieHttpOnly);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ public void testSessionCookieName() throws Exception {
}, 200, "OK", null);
}

@Test
public void testSessionCookieDomain() throws Exception {
router.route().handler(SessionHandler.create(store).setSessionCookieDomain("example.com"));
router.route().handler(rc -> rc.response().end());
testRequest(HttpMethod.GET, "/", null, resp -> {
String setCookie = resp.headers().get("set-cookie");
assertTrue(setCookie.contains("Domain=example.com"));
}, 200, "OK", null);
}

@Test
public void testSessionCookieDefaultDomain() throws Exception {
router.route().handler(SessionHandler.create(store));
router.route().handler(rc -> rc.response().end());
testRequest(HttpMethod.GET, "/", null, resp -> {
String setCookie = resp.headers().get("set-cookie");
assertFalse(setCookie.contains("Domain"));
}, 200, "OK", null);
}

@Test
public void testSessionCookiePath() throws Exception {
router.route().handler(SessionHandler.create(store).setSessionCookiePath("/path"));
Expand Down
Loading