Skip to content

Commit

Permalink
Adds additional checks during cookie parsing. Fixes comment.
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas committed Feb 11, 2025
1 parent 24a0501 commit 93b401a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private void clearCookie(SetCookie cookie, Predicate<SetCookie> predicate) {
String currentValue = currentValues.get(i);
SetCookie currentCookie = SetCookie.parse(currentValue);
if (predicate.test(currentCookie)) {
newValues[i] = expiredCookie.text(); // replace with expired
newValues[i] = expiredCookie.text(); // replaces with expired cookie
found = true;
} else {
newValues[i] = currentValue;
Expand All @@ -77,7 +77,7 @@ private void clearCookie(SetCookie cookie, Predicate<SetCookie> predicate) {
if (!found) {
String[] values = new String[newValues.length + 1];
System.arraycopy(newValues, 0, values, 0, newValues.length);
values[values.length - 1] = expiredCookie.text(); // replace with expired
values[values.length - 1] = expiredCookie.text(); // adds expired cookie
newValues = values;
}
set(HeaderValues.create(HeaderNames.SET_COOKIE, newValues));
Expand Down
8 changes: 6 additions & 2 deletions http/http/src/main/java/io/helidon/http/SetCookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,15 @@ public static Builder builder(SetCookie setCookie) {
* @return new instance
*/
public static SetCookie parse(String setCookie) {
Objects.requireNonNull(setCookie);
String[] cookieParts = setCookie.split(PARAM_SEPARATOR);
String nameAndValue = cookieParts[0];
int equalsIndex = nameAndValue.indexOf('=');
String name = nameAndValue.substring(0, equalsIndex);
String value = nameAndValue.substring(equalsIndex + 1);
String name = (equalsIndex == -1) ? nameAndValue : nameAndValue.substring(0, equalsIndex);
if (name.isEmpty()) {
throw new IllegalArgumentException("Cookie name cannot be empty");
}
String value = (equalsIndex == -1) ? "" : nameAndValue.substring(equalsIndex + 1);
Builder builder = builder(name, value);

for (int i = 1; i < cookieParts.length; i++) {
Expand Down
27 changes: 17 additions & 10 deletions http/http/src/test/java/io/helidon/http/SetCookieTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class SetCookieTest {
+ "SameSite=Lax";

@Test
public void testSetCookiesFromString() {
void testSetCookiesFromString() {
SetCookie setCookie = SetCookie.parse(TEMPLATE);

assertThat(setCookie.name(), is("some-cookie"));
Expand All @@ -57,7 +57,7 @@ public void testSetCookiesFromString() {
}

@Test
public void testSetCookiesInvalidValue() {
void testSetCookiesInvalidValue() {
String template = "some-cookie=some-cookie-value; "
+ "Invalid=value";
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
Expand All @@ -66,13 +66,7 @@ public void testSetCookiesInvalidValue() {
}

@Test
public void testEmptyValue() {
SetCookie setCookie = SetCookie.parse("some-cookie=");
assertThat(setCookie.value(), is(""));
}

@Test
public void testEquals() {
void testEquals() {
SetCookie setCookie1 = SetCookie.parse(TEMPLATE);
SetCookie setCookie2 = SetCookie.builder("some-cookie", "").build();
SetCookie setCookie3 = SetCookie.builder("some-cookie", "")
Expand All @@ -90,7 +84,7 @@ public void testEquals() {
}

@Test
public void testCookieBuilder() {
void testCookieBuilder() {
SetCookie setCookie1 = SetCookie.parse(TEMPLATE);
SetCookie setCookie2 = SetCookie.builder(setCookie1).build(); // from setCookie1

Expand All @@ -106,4 +100,17 @@ public void testCookieBuilder() {
assertThat(setCookie2.httpOnly(), is(true));
assertThat(setCookie2.sameSite(), optionalValue(is(SetCookie.SameSite.LAX)));
}

@Test
void testParse() {
assertThrows(NullPointerException.class,() -> SetCookie.parse(null));
assertThrows(IllegalArgumentException.class,() -> SetCookie.parse(""));
SetCookie setCookie1 = SetCookie.parse("some-cookie");
assertThat(setCookie1.name(), is("some-cookie"));
assertThat(setCookie1.value(), is(""));
SetCookie setCookie2 = SetCookie.parse("some-cookie=");
assertThat(setCookie2.name(), is("some-cookie"));
assertThat(setCookie2.value(), is(""));
assertThat(setCookie1.equals(setCookie2), is(true));
}
}

0 comments on commit 93b401a

Please sign in to comment.