Skip to content
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
44 changes: 40 additions & 4 deletions client/src/main/java/org/asynchttpclient/StoreCookies.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.asynchttpclient;

import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.CookieHeaderNames.SameSite;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import org.asynchttpclient.cookie.CookieStore;
import org.jetbrains.annotations.Nullable;

Expand All @@ -31,8 +33,9 @@
/**
* Tells the cookies the client added to a request from its {@link CookieStore} from those the application or a
* filter set. The store selected them for the URI the request had then, so a request sent to another URI must not
* carry them. They are marked rather than recognised by value: the response that leads to another request may have
* rotated the value in the store already. A marked cookie delegates to the store's instance.
* carry them. They are marked rather than recognised by value or by instance: the response that leads to another
* request may have rotated the value in the store already, and a cookie the application adds is its own, even the
* store's instance. A marked cookie is a {@link DefaultCookie} that delegates to the store's instance.
* <p>
* Internal to the client; applications have no use for it.
*/
Expand Down Expand Up @@ -154,11 +157,12 @@ private static boolean hasCookieNamed(List<Cookie> cookies, String name) {
return false;
}

private static final class Marked implements Cookie {
private static final class Marked extends DefaultCookie {

private final Cookie cookie;

Marked(Cookie cookie) {
super(cookie.name(), cookie.value());
this.cookie = cookie;
}

Expand All @@ -174,7 +178,10 @@ public String value() {

@Override
public void setValue(String value) {
cookie.setValue(value);
// DefaultCookie's constructor calls this before the delegate is assigned.
if (cookie != null) {
cookie.setValue(value);
}
}

@Override
Expand Down Expand Up @@ -237,6 +244,35 @@ public void setHttpOnly(boolean httpOnly) {
cookie.setHttpOnly(httpOnly);
}

// A store's cookie of another type has no SameSite or Partitioned attribute, so the mark holds those.
@Override
public @Nullable SameSite sameSite() {
return cookie instanceof DefaultCookie ? ((DefaultCookie) cookie).sameSite() : super.sameSite();
}

@Override
public void setSameSite(@Nullable SameSite sameSite) {
if (cookie instanceof DefaultCookie) {
((DefaultCookie) cookie).setSameSite(sameSite);
} else {
super.setSameSite(sameSite);
}
}

@Override
public boolean isPartitioned() {
return cookie instanceof DefaultCookie ? ((DefaultCookie) cookie).isPartitioned() : super.isPartitioned();
}

@Override
public void setPartitioned(boolean partitioned) {
if (cookie instanceof DefaultCookie) {
((DefaultCookie) cookie).setPartitioned(partitioned);
} else {
super.setPartitioned(partitioned);
}
}

// Unwrapped, so that a marked cookie compares and equals as the store's own does, against either.
@Override
public int compareTo(Cookie other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ public AsyncHandler<T> getAsyncHandler() {
/**
* The request carries the cookies the client took from its cookie store for its URI, and a request built from it
* carries them as the store's: wherever that request is sent or replayed, they are taken from the store again.
* Cookies copied from it by name and value are the caller's own and are sent as they are.
* A request sent in place of this one to the same origin gets none of the store's cookies under the names of
* those it no longer carries, as a filter removed them: one built from scratch gets only the store's cookies of
* other names. Any other cookie a caller or a filter adds is theirs and is not taken from the store again, even
* a copy of a store cookie or the store's own instance.
*
* @return the original or decorated {@link Request}
*/
Expand Down
52 changes: 52 additions & 0 deletions client/src/test/java/org/asynchttpclient/StoreCookiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,24 @@

import io.netty.handler.codec.http.cookie.ClientCookieDecoder;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.CookieHeaderNames.SameSite;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import org.asynchttpclient.cookie.CookieStore;
import org.asynchttpclient.cookie.ThreadSafeCookieStore;
import org.asynchttpclient.uri.Uri;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -102,6 +107,53 @@ public void aMarkedCookieWritesThroughToTheStoresOwn() {
assertTrue(stored.isHttpOnly());
}

@Test
public void aMarkedCookieIsADefaultCookieWithTheStoresSameSiteAndPartitioned() {
DefaultCookie stored = (DefaultCookie) decode("SID=v; Path=/; Secure; SameSite=Strict; Partitioned");
DefaultCookie marked = assertInstanceOf(DefaultCookie.class, StoreCookies.mark(stored));

assertEquals(SameSite.Strict, marked.sameSite());
assertTrue(marked.isPartitioned());

marked.setSameSite(SameSite.Lax);
marked.setPartitioned(false);

assertEquals(SameSite.Lax, stored.sameSite());
assertFalse(stored.isPartitioned());
}

@Test
public void aMarkOfACookieOfAnotherTypeHoldsSameSiteAndPartitionedItself() {
DefaultCookie target = new DefaultCookie("SID", "v");
AtomicInteger valueWrites = new AtomicInteger();
Cookie stored = (Cookie) Proxy.newProxyInstance(Cookie.class.getClassLoader(), new Class<?>[]{Cookie.class},
(proxy, method, args) -> {
if ("setValue".equals(method.getName())) {
valueWrites.incrementAndGet();
}
return method.invoke(target, args);
});
DefaultCookie marked = assertInstanceOf(DefaultCookie.class, StoreCookies.mark(stored));

assertEquals("v", marked.value());
assertEquals(0, valueWrites.get(), "marking wrote the value back");
assertNull(marked.sameSite());
assertFalse(marked.isPartitioned());

marked.setSameSite(SameSite.Lax);
marked.setPartitioned(true);
marked.setValue("w");
marked.setSecure(true);

assertEquals(SameSite.Lax, marked.sameSite());
assertTrue(marked.isPartitioned());
assertNull(target.sameSite());
assertFalse(target.isPartitioned());
assertEquals("w", target.value());
assertEquals(1, valueWrites.get());
assertTrue(target.isSecure());
}

@Test
public void twoMarksOfACookieThatOnlyEqualsItsOwnTypeAreEqual() {
Cookie stored = new OwnTypeOnlyCookie("SID", "v");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.netty.channel.nio.NioIoHandler;
import io.netty.handler.codec.http.cookie.ClientCookieDecoder;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.CookieHeaderNames.SameSite;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -66,8 +67,10 @@
import static org.asynchttpclient.test.TestUtils.addHttpConnector;
import static org.asynchttpclient.test.TestUtils.addHttpsConnector;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* The cookies the client takes from its store for a request were selected for that request's URI. A request a
Expand Down Expand Up @@ -584,6 +587,28 @@ public void aRequestFilterThatChangesOnlyThePathGetsTheStoresCookiesForTheNewPat
assertEquals(Set.of("A_SESSION=alice"), cookiesReceived(response, "127.0.0.1"));
}

@Test
public void aRequestFilterReadsTheStoresCookiesAsDefaultCookiesWithTheirAttributes() throws Exception {
add(store, secureA("/"), "S_SESSION=top; Path=/; Secure; SameSite=Strict; Partitioned");
AtomicReference<List<Cookie>> seen = new AtomicReference<>();
RequestFilter inspect = new RequestFilter() {
@Override
public <T> FilterContext<T> filter(FilterContext<T> ctx) {
seen.set(ctx.getRequest().getCookies());
return ctx;
}
};

Response response = get(config().setCookieStore(store).setUseInsecureTrustManager(true)
.addRequestFilter(inspect),
new RequestBuilder().setUrl(secureA("/sink")));

assertEquals(Set.of("S_SESSION=top"), cookiesReceived(response, "127.0.0.1"));
DefaultCookie cookie = assertInstanceOf(DefaultCookie.class, seen.get().get(0));
assertEquals(SameSite.Strict, cookie.sameSite());
assertTrue(cookie.isPartitioned());
}

@Test
public void aReplayToTheSameHostSendsACookieTheFailingResponseSetUnderANewName() throws Exception {
add(store, a("/"), "A_SESSION=alice; Path=/");
Expand Down
Loading