Skip to content

Commit cd51c80

Browse files
committed
Change enum name + use null value when omitted
Signed-off-by: Filip Hrisafov <filip.hrisafov@gmail.com>
1 parent f163aa9 commit cd51c80

File tree

9 files changed

+17
-19
lines changed

9 files changed

+17
-19
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ DefaultCookieSerializer cookieSerializer(ServerProperties serverProperties,
9797
map.from(cookie::getHttpOnly).to(cookieSerializer::setUseHttpOnlyCookie);
9898
map.from(cookie::getSecure).to(cookieSerializer::setUseSecureCookie);
9999
map.from(cookie::getMaxAge).asInt(Duration::getSeconds).to(cookieSerializer::setCookieMaxAge);
100-
map.from(cookie::getSameSite)
101-
.as((sameSite) -> (sameSite == SameSite.UNSET) ? null : sameSite.attributeValue())
102-
.to(cookieSerializer::setSameSite);
100+
map.from(cookie::getSameSite).as(SameSite::attributeValue).to(cookieSerializer::setSameSite);
103101
map.from(cookie::getPartitioned).to(cookieSerializer::setPartitioned);
104102
cookieSerializerCustomizers.orderedStream().forEach((customizer) -> customizer.customize(cookieSerializer));
105103
return cookieSerializer;

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebSessionIdResolverAutoConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ private void initializeCookie(ResponseCookieBuilder builder) {
7777
map.from(cookie::getSecure).to(builder::secure);
7878
map.from(cookie::getMaxAge).to(builder::maxAge);
7979
map.from(cookie::getPartitioned).to(builder::partitioned);
80-
map.from(cookie::getSameSite)
81-
.to(((sameSite) -> builder.sameSite((sameSite == SameSite.UNSET) ? null : sameSite.attributeValue())));
80+
map.from(cookie::getSameSite).as(SameSite::attributeValue).to(builder::sameSite);
8281
}
8382

8483
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ void sessionCookieConfigurationIsAppliedToAutoConfiguredCookieSerializer() {
171171
}
172172

173173
@Test
174-
void sessionCookieSameSiteUnsetIsAppliedToAutoConfiguredCookieSerializer() {
174+
void sessionCookieSameSiteOmittedIsAppliedToAutoConfiguredCookieSerializer() {
175175
this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class)
176-
.withPropertyValues("server.servlet.session.cookie.sameSite=unset")
176+
.withPropertyValues("server.servlet.session.cookie.sameSite=omitted")
177177
.run((context) -> {
178178
DefaultCookieSerializer cookieSerializer = context.getBean(DefaultCookieSerializer.class);
179179
assertThat(cookieSerializer).hasFieldOrPropertyWithValue("sameSite", null);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,8 +677,8 @@ void customSessionCookieConfigurationShouldBeApplied() {
677677
}
678678

679679
@Test
680-
void sessionCookieUnsetConfigurationShouldBeApplied() {
681-
this.contextRunner.withPropertyValues("server.reactive.session.cookie.same-site:unset")
680+
void sessionCookieOmittedConfigurationShouldBeApplied() {
681+
this.contextRunner.withPropertyValues("server.reactive.session.cookie.same-site:omitted")
682682
.run(assertExchangeWithSession((exchange) -> {
683683
List<ResponseCookie> cookies = exchange.getResponse().getCookies().get("SESSION");
684684
assertThat(cookies).extracting(ResponseCookie::getSameSite).containsOnlyNulls();

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ protected final void configureWebAppContext(WebAppContext context, ServletContex
284284
private void configureSession(WebAppContext context) {
285285
SessionHandler handler = context.getSessionHandler();
286286
SameSite sessionSameSite = getSession().getCookie().getSameSite();
287-
if (sessionSameSite != null && sessionSameSite != SameSite.UNSET) {
287+
if (sessionSameSite != null && sessionSameSite != SameSite.OMITTED) {
288288
handler.setSameSite(HttpCookie.SameSite.valueOf(sessionSameSite.name()));
289289
}
290290
Duration sessionTimeout = getSession().getTimeout();

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,11 +998,12 @@ private static class SuppliedSameSiteCookieProcessor extends Rfc6265CookieProces
998998
@Override
999999
public String generateHeader(Cookie cookie, HttpServletRequest request) {
10001000
SameSite sameSite = getSameSite(cookie);
1001-
if (sameSite == null || sameSite == SameSite.UNSET) {
1001+
String sameSiteValue = (sameSite != null) ? sameSite.attributeValue() : null;
1002+
if (sameSiteValue == null) {
10021003
return super.generateHeader(cookie, request);
10031004
}
10041005
Rfc6265CookieProcessor delegate = new Rfc6265CookieProcessor();
1005-
delegate.setSameSiteCookies(sameSite.attributeValue());
1006+
delegate.setSameSiteCookies(sameSiteValue);
10061007
return delegate.generateHeader(cookie, request);
10071008
}
10081009

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
635635
private void beforeCommit(HttpServerExchange exchange) {
636636
for (Cookie cookie : exchange.responseCookies()) {
637637
SameSite sameSite = getSameSite(asServletCookie(cookie));
638-
if (sameSite == SameSite.UNSET) {
638+
if (sameSite == SameSite.OMITTED) {
639639
cookie.setSameSite(false);
640640
}
641641
else if (sameSite != null) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/Cookie.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ public void setPartitioned(Boolean partitioned) {
146146
public enum SameSite {
147147

148148
/**
149-
* Don't set the SameSite cookie attribute.
149+
* The SameSite cookie attribute will be omitted when creating the cookie.
150150
*/
151-
UNSET("Unset"),
151+
OMITTED(null),
152152

153153
/**
154154
* Cookies are sent in both first-party and cross-origin requests.

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ void sessionCookieConfiguration() {
881881
}
882882

883883
@ParameterizedTest
884-
@EnumSource(mode = EnumSource.Mode.EXCLUDE, names = "UNSET")
884+
@EnumSource(mode = EnumSource.Mode.EXCLUDE, names = "OMITTED")
885885
void sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookies(SameSite sameSite) throws Exception {
886886
AbstractServletWebServerFactory factory = getFactory();
887887
factory.getSession().getCookie().setSameSite(sameSite);
@@ -896,7 +896,7 @@ void sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookies(S
896896
}
897897

898898
@ParameterizedTest
899-
@EnumSource(mode = EnumSource.Mode.EXCLUDE, names = "UNSET")
899+
@EnumSource(mode = EnumSource.Mode.EXCLUDE, names = "OMITTED")
900900
void sessionCookieSameSiteAttributeCanBeConfiguredAndOnlyAffectsSessionCookiesWhenUsingCustomName(SameSite sameSite)
901901
throws Exception {
902902
AbstractServletWebServerFactory factory = getFactory();
@@ -950,9 +950,9 @@ void cookieSameSiteSuppliersShouldNotAffectSessionCookie() throws IOException, U
950950
}
951951

952952
@Test
953-
void cookieSameSiteSuppliersShouldNotAffectUnsetSameSite() throws IOException, URISyntaxException {
953+
void cookieSameSiteSuppliersShouldNotAffectOmittedSameSite() throws IOException, URISyntaxException {
954954
AbstractServletWebServerFactory factory = getFactory();
955-
factory.getSession().getCookie().setSameSite(SameSite.UNSET);
955+
factory.getSession().getCookie().setSameSite(SameSite.OMITTED);
956956
factory.getSession().getCookie().setName("SESSIONCOOKIE");
957957
factory.addCookieSameSiteSuppliers(CookieSameSiteSupplier.ofStrict());
958958
factory.addInitializers(new ServletRegistrationBean<>(new CookieServlet(false), "/"));

0 commit comments

Comments
 (0)