Skip to content

Commit 3a1644d

Browse files
Fix cookie-config web.xml attributes when used with quickstart (#14017)
* Preserve cookie-config comment attribute for quickstart * PR #14017 - fix for failing test * PR #14017 - changes from review Signed-off-by: Lachlan Roberts <[email protected]>
1 parent 8140aa8 commit 3a1644d

File tree

5 files changed

+97
-12
lines changed

5 files changed

+97
-12
lines changed

jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/Response.java

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ public Supplier<Map<String, String>> getSupplier()
14941494
}
14951495
}
14961496

1497-
protected static class HttpCookieFacade implements HttpCookie
1497+
public static class HttpCookieFacade implements HttpCookie
14981498
{
14991499
private final Cookie _cookie;
15001500
private final String _comment;
@@ -1617,17 +1617,17 @@ public String toString()
16171617
return HttpCookie.toString(this);
16181618
}
16191619

1620-
private static boolean isHttpOnlyInComment(String comment)
1620+
public static boolean isHttpOnlyInComment(String comment)
16211621
{
16221622
return comment != null && comment.contains(HTTP_ONLY_COMMENT);
16231623
}
16241624

1625-
protected static boolean isPartitionedInComment(String comment)
1625+
public static boolean isPartitionedInComment(String comment)
16261626
{
16271627
return comment != null && comment.contains(PARTITIONED_COMMENT);
16281628
}
16291629

1630-
protected static SameSite getSameSiteFromComment(String comment)
1630+
public static SameSite getSameSiteFromComment(String comment)
16311631
{
16321632
if (comment == null)
16331633
return null;
@@ -1640,7 +1640,7 @@ protected static SameSite getSameSiteFromComment(String comment)
16401640
return null;
16411641
}
16421642

1643-
protected static String getCommentWithoutAttributes(String comment)
1643+
public static String getCommentWithoutAttributes(String comment)
16441644
{
16451645
if (comment == null)
16461646
return null;
@@ -1655,5 +1655,43 @@ protected static String getCommentWithoutAttributes(String comment)
16551655

16561656
return strippedComment.isEmpty() ? null : strippedComment;
16571657
}
1658+
1659+
public static String getCommentWithAttributes(String comment, boolean isPartitioned, HttpCookie.SameSite sameSite)
1660+
{
1661+
if (comment == null && sameSite == null)
1662+
return null;
1663+
1664+
StringBuilder builder = new StringBuilder();
1665+
if (StringUtil.isNotBlank(comment))
1666+
{
1667+
comment = getCommentWithoutAttributes(comment);
1668+
if (StringUtil.isNotBlank(comment))
1669+
builder.append(comment);
1670+
}
1671+
if (isPartitioned)
1672+
builder.append(PARTITIONED_COMMENT);
1673+
1674+
if (sameSite != null)
1675+
{
1676+
switch (sameSite)
1677+
{
1678+
case NONE:
1679+
builder.append(SAME_SITE_NONE_COMMENT);
1680+
break;
1681+
case STRICT:
1682+
builder.append(SAME_SITE_STRICT_COMMENT);
1683+
break;
1684+
case LAX:
1685+
builder.append(SAME_SITE_LAX_COMMENT);
1686+
break;
1687+
default:
1688+
throw new IllegalArgumentException(sameSite.toString());
1689+
}
1690+
}
1691+
1692+
if (builder.isEmpty())
1693+
return null;
1694+
return builder.toString();
1695+
}
16581696
}
16591697
}

jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/SessionHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,11 @@ public void setComment(String comment)
684684

685685
boolean partitioned = Response.HttpCookieFacade.isPartitionedInComment(comment);
686686
if (partitioned)
687-
_sessionManager.setPartitioned(partitioned);
687+
_sessionManager.setPartitioned(true);
688+
689+
boolean httpOnly = Response.HttpCookieFacade.isHttpOnlyInComment(comment);
690+
if (httpOnly)
691+
_sessionManager.setHttpOnly(true);
688692

689693
_sessionManager.setSessionComment(Response.HttpCookieFacade.getCommentWithoutAttributes(comment));
690694
}

jetty-ee9/jetty-ee9-quickstart/src/main/java/org/eclipse/jetty/ee9/quickstart/QuickStartGeneratorConfiguration.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import jakarta.servlet.descriptor.TaglibDescriptor;
3434
import org.eclipse.jetty.ee9.annotations.AnnotationConfiguration;
3535
import org.eclipse.jetty.ee9.nested.ServletConstraint;
36+
import org.eclipse.jetty.ee9.nested.SessionHandler;
3637
import org.eclipse.jetty.ee9.security.Authenticator;
3738
import org.eclipse.jetty.ee9.security.ConstraintAware;
3839
import org.eclipse.jetty.ee9.security.ConstraintMapping;
@@ -65,6 +66,8 @@
6566
import org.slf4j.Logger;
6667
import org.slf4j.LoggerFactory;
6768

69+
import static org.eclipse.jetty.ee9.nested.Response.HttpCookieFacade.getCommentWithAttributes;
70+
6871
/**
6972
* QuickStartGeneratorConfiguration
7073
* <p>
@@ -424,14 +427,15 @@ public void generateQuickStartWebXml(WebAppContext context, OutputStream stream)
424427
}
425428

426429
//session-config
427-
if (context.getSessionHandler() != null)
430+
SessionHandler sessionHandler = context.getSessionHandler();
431+
if (sessionHandler != null)
428432
{
429433
out.openTag("session-config");
430-
int maxInactiveSec = context.getSessionHandler().getMaxInactiveInterval();
434+
int maxInactiveSec = sessionHandler.getMaxInactiveInterval();
431435
out.tag("session-timeout", (maxInactiveSec == 0 ? "0" : Integer.toString(maxInactiveSec / 60)));
432436

433437
//cookie-config
434-
SessionCookieConfig cookieConfig = context.getSessionHandler().getSessionCookieConfig();
438+
SessionCookieConfig cookieConfig = sessionHandler.getSessionCookieConfig();
435439
if (cookieConfig != null)
436440
{
437441
out.openTag("cookie-config");
@@ -445,8 +449,9 @@ public void generateQuickStartWebXml(WebAppContext context, OutputStream stream)
445449
if (cookieConfig.getPath() != null)
446450
out.tag("path", origin(md, "cookie-config.path"), cookieConfig.getPath());
447451

448-
if (cookieConfig.getComment() != null)
449-
out.tag("comment", origin(md, "cookie-config.comment"), cookieConfig.getComment());
452+
String comment = getCommentWithAttributes(cookieConfig.getComment(), sessionHandler.isPartitioned(), sessionHandler.getSameSite());
453+
if (comment != null)
454+
out.tag("comment", origin(md, "cookie-config.comment"), comment);
450455

451456
out.tag("http-only", origin(md, "cookie-config.http-only"), Boolean.toString(cookieConfig.isHttpOnly()));
452457
out.tag("secure", origin(md, "cookie-config.secure"), Boolean.toString(cookieConfig.isSecure()));
@@ -455,7 +460,7 @@ public void generateQuickStartWebXml(WebAppContext context, OutputStream stream)
455460
}
456461

457462
// tracking-modes
458-
Set<SessionTrackingMode> modes = context.getSessionHandler().getEffectiveSessionTrackingModes();
463+
Set<SessionTrackingMode> modes = sessionHandler.getEffectiveSessionTrackingModes();
459464
if (modes != null)
460465
{
461466
for (SessionTrackingMode mode : modes)

jetty-ee9/jetty-ee9-tests/jetty-ee9-test-quickstart/src/test/java/org/eclipse/jetty/ee9/quickstart/QuickStartTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,27 @@ public void testFilterMappings() throws Exception
291291
</filter-mapping>
292292
"""));
293293
}
294+
295+
@Test
296+
public void testSameSiteCookie() throws Exception
297+
{
298+
Path workdir = MavenPaths.targetTestDir(PreconfigureSpecWar.class.getSimpleName());
299+
FS.ensureEmpty(workdir);
300+
Path target = workdir.resolve("test-cookie_samesite");
301+
FS.ensureEmpty(target);
302+
FS.ensureDirExists(target.resolve("WEB-INF"));
303+
304+
Path sourceWebXml = MavenPaths.findTestResourceFile("cookie-web.xml");
305+
Files.copy(sourceWebXml, target.resolve("WEB-INF/web.xml"));
306+
System.setProperty("jetty.home", target.toString());
307+
308+
PreconfigureQuickStartWar.main(target.toString());
309+
310+
Path quickStartXml = target.resolve("WEB-INF/quickstart-web.xml");
311+
String quickStartContents = Files.readString(quickStartXml);
312+
assertThat(quickStartContents, containsString("""
313+
<cookie-config>
314+
<comment>foo__SAME_SITE_NONE__</comment>
315+
"""));
316+
}
294317
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app
3+
xmlns="https://jakarta.ee/xml/ns/jakartaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
6+
version="5.0">
7+
8+
<session-config>
9+
<cookie-config>
10+
<http-only>true</http-only>
11+
<comment>__SAME_SITE_NONE__foo</comment>
12+
</cookie-config>
13+
</session-config>
14+
15+
</web-app>

0 commit comments

Comments
 (0)