Skip to content

Commit

Permalink
Fix #11934 Servlet 6.1 Cookies
Browse files Browse the repository at this point in the history
Partitioned is set if any attribute that is not "false" is set.
Avoid equal sign for empty valued attributes
  • Loading branch information
gregw committed Jun 20, 2024
1 parent 2336dbf commit 0247620
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ default String getPath()
*/
default boolean isSecure()
{
return Boolean.parseBoolean(getAttributes().get(SECURE_ATTRIBUTE));
return isSetToNotFalse(SECURE_ATTRIBUTE);
}

/**
Expand All @@ -146,7 +146,7 @@ default SameSite getSameSite()
*/
default boolean isHttpOnly()
{
return Boolean.parseBoolean(getAttributes().get(HTTP_ONLY_ATTRIBUTE));
return isSetToNotFalse(HTTP_ONLY_ATTRIBUTE);
}

/**
Expand All @@ -155,8 +155,13 @@ default boolean isHttpOnly()
*/
default boolean isPartitioned()
{
String partitioned = getAttributes().get(PARTITIONED_ATTRIBUTE);
return partitioned != null && !StringUtil.asciiEqualsIgnoreCase("false", partitioned);
return isSetToNotFalse(PARTITIONED_ATTRIBUTE);
}

private boolean isSetToNotFalse(String attribute)
{
String value = getAttributes().get(attribute);
return value != null && !StringUtil.asciiEqualsIgnoreCase("false", value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.Index;
import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.util.StringUtil;

/**
* <p>Utility methods for server-side HTTP cookie handling.</p>
Expand Down Expand Up @@ -291,8 +292,9 @@ public static String getRFC6265SetCookie(HttpCookie httpCookie)
{
if (KNOWN_ATTRIBUTES.contains(e.getKey()))
continue;
builder.append("; ").append(e.getKey()).append("=");
builder.append(e.getValue());
builder.append("; ").append(e.getKey());
if (StringUtil.isNotBlank(e.getValue()))
builder.append("=").append(e.getValue());
}

return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void testMatchCookie()
}

@Test
public void testSetRFC2965Cookie() throws Exception
public void testSetRFC2965Cookie()
{
HttpCookie httpCookie;

Expand Down Expand Up @@ -162,10 +162,12 @@ public void testSetRFC6265Cookie()
assertEquals("everything=value; Path=path; Domain=domain; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; HttpOnly; Partitioned; SameSite=Strict", HttpCookieUtils.getRFC6265SetCookie(httpCookie));

httpCookie = HttpCookie.from("everything", "value", Map.of(HttpCookie.DOMAIN_ATTRIBUTE, "domain", HttpCookie.PATH_ATTRIBUTE, "path", HttpCookie.MAX_AGE_ATTRIBUTE, Long.toString(1), HttpCookie.HTTP_ONLY_ATTRIBUTE, Boolean.toString(true), HttpCookie.SECURE_ATTRIBUTE, Boolean.toString(true), HttpCookie.SAME_SITE_ATTRIBUTE, SameSite.STRICT.getAttributeValue(), HttpCookie.PARTITIONED_ATTRIBUTE, Boolean.toString(true)));

String rfc6265SetCookie = HttpCookieUtils.getRFC6265SetCookie(httpCookie);
assertThat(rfc6265SetCookie, startsWith("everything=value; Path=path; Domain=domain; Expires="));
assertThat(rfc6265SetCookie, endsWith(" GMT; Max-Age=1; Secure; HttpOnly; Partitioned; SameSite=Strict"));

httpCookie = HttpCookie.from("everything", "value", -1, Map.of(HttpCookie.DOMAIN_ATTRIBUTE, "domain", HttpCookie.PATH_ATTRIBUTE, "path", HttpCookie.MAX_AGE_ATTRIBUTE, Long.toString(0), HttpCookie.HTTP_ONLY_ATTRIBUTE, Boolean.toString(true), HttpCookie.SECURE_ATTRIBUTE, Boolean.toString(true), "Other", "attribute", "Single", ""));
assertEquals("everything=value; Path=path; Domain=domain; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; HttpOnly; Other=attribute; Single", HttpCookieUtils.getRFC6265SetCookie(httpCookie));
}

public static Stream<String> rfc6265BadNameSource()
Expand Down

0 comments on commit 0247620

Please sign in to comment.