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 SameSite Strict only on OIDC session cookie #30722

Merged
merged 1 commit into from
Jan 30, 2023
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 @@ -603,7 +603,7 @@ public static enum Source {
public static class Authentication {

/**
* SameSite attribute values for the session, state and post logout cookies.
* SameSite attribute values for the session cookie.
*/
public enum CookieSameSite {
STRICT,
Expand Down Expand Up @@ -767,7 +767,7 @@ public enum ResponseMode {
public Optional<String> cookieDomain = Optional.empty();

/**
* SameSite attribute for the session, state and post logout cookies.
* SameSite attribute for the session cookie.
*/
@ConfigItem(defaultValue = "strict")
public CookieSameSite cookieSameSite = CookieSameSite.STRICT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ public Uni<? extends Void> apply(Void t) {
public Void apply(String cookieValue) {
String sessionCookie = createCookie(context, configContext.oidcConfig,
getSessionCookieName(configContext.oidcConfig),
cookieValue, sessionMaxAge).getValue();
cookieValue, sessionMaxAge, true).getValue();
if (sessionCookie.length() >= MAX_COOKIE_VALUE_LENGTH) {
LOG.warnf(
"Session cookie length for the tenant %s is equal or greater than %d bytes."
Expand Down Expand Up @@ -914,6 +914,11 @@ private String generatePostLogoutState(RoutingContext context, TenantConfigConte

static ServerCookie createCookie(RoutingContext context, OidcTenantConfig oidcConfig,
String name, String value, long maxAge) {
return createCookie(context, oidcConfig, name, value, maxAge, false);
}

static ServerCookie createCookie(RoutingContext context, OidcTenantConfig oidcConfig,
String name, String value, long maxAge, boolean sessionCookie) {
ServerCookie cookie = new CookieImpl(name, value);
cookie.setHttpOnly(true);
cookie.setSecure(oidcConfig.authentication.cookieForceSecure || context.request().isSSL());
Expand All @@ -924,7 +929,9 @@ static ServerCookie createCookie(RoutingContext context, OidcTenantConfig oidcCo
if (auth.cookieDomain.isPresent()) {
cookie.setDomain(auth.getCookieDomain().get());
}
cookie.setSameSite(CookieSameSite.valueOf(auth.cookieSameSite.name()));
if (sessionCookie) {
cookie.setSameSite(CookieSameSite.valueOf(auth.cookieSameSite.name()));
}
context.response().addCookie(cookie);
return cookie;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public void testCodeFlowNoConsent() throws IOException {
.loadWebResponse(new WebRequest(URI.create("http://localhost:8081/index.html").toURL()));
verifyLocationHeader(webClient, webResponse.getResponseHeaderValue("location"), null, "web-app", false);

String stateCookieString = webResponse.getResponseHeaderValue("Set-Cookie");
assertTrue(stateCookieString.startsWith("q_auth_Default_test="));
assertTrue(stateCookieString.contains("SameSite=Strict"));
Cookie stateCookie = getStateCookie(webClient, null);
assertNotNull(stateCookie);
assertNull(stateCookie.getSameSite());

webClient.getCookieManager().clearCookies();

Expand Down Expand Up @@ -95,6 +95,7 @@ public void testCodeFlowNoConsent() throws IOException {

Cookie sessionCookie = getSessionCookie(webClient, null);
assertNotNull(sessionCookie);
assertEquals("strict", sessionCookie.getSameSite());

webClient.getCookieManager().clearCookies();
}
Expand Down Expand Up @@ -176,10 +177,6 @@ public void testCodeFlowForceHttpsRedirectUriAndPkce() throws Exception {
verifyLocationHeader(webClient, keycloakUrl, "tenant-https_test", "xforwarded%2Ftenant-https",
true);

String stateCookieString = webResponse.getResponseHeaderValue("Set-Cookie");
assertTrue(stateCookieString.startsWith("q_auth_tenant-https_test="));
assertTrue(stateCookieString.contains("SameSite=Lax"));

HtmlPage page = webClient.getPage(keycloakUrl);

assertEquals("Sign in to quarkus", page.getTitleText());
Expand All @@ -195,6 +192,7 @@ public void testCodeFlowForceHttpsRedirectUriAndPkce() throws Exception {
String endpointLocation = webResponse.getResponseHeaderValue("location");

Cookie stateCookie = getStateCookie(webClient, "tenant-https_test");
assertNull(stateCookie.getSameSite());
verifyCodeVerifier(stateCookie, keycloakUrl);

assertTrue(endpointLocation.startsWith("https"));
Expand Down Expand Up @@ -222,6 +220,7 @@ public void testCodeFlowForceHttpsRedirectUriAndPkce() throws Exception {
assertEquals("tenant-https:reauthenticated", page.getBody().asNormalizedText());
Cookie sessionCookie = getSessionCookie(webClient, "tenant-https_test");
assertNotNull(sessionCookie);
assertEquals("lax", sessionCookie.getSameSite());
webClient.getCookieManager().clearCookies();
}
}
Expand Down