Skip to content

Commit ede07e6

Browse files
Treat Max-Age<=0 as immediate expiry (#805)
Align strict Max-Age parsing with RFC 6265 user-agent processing. Non-positive delta-seconds now expire cookies immediately instead of being rejected as malformed.
1 parent 70b9e94 commit ede07e6

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/impl/cookie/BasicMaxAgeHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ public void parse(final SetCookie cookie, final String value)
6969
throw new MalformedCookieException ("Invalid 'max-age' attribute: "
7070
+ value);
7171
}
72-
if (age < 0) {
73-
throw new MalformedCookieException ("Negative 'max-age' attribute: "
74-
+ value);
72+
if (age <= 0) {
73+
// RFC 6265 user-agent processing: delta-seconds <= 0 means immediate expiry.
74+
cookie.setExpiryDate(Instant.EPOCH);
75+
return;
7576
}
7677
cookie.setExpiryDate(Instant.now().plusSeconds(age));
7778
}

httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestBasicCookieAttribHandlers.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,17 @@ void testBasicMaxAgeParse() throws Exception {
270270
Assertions.assertNotNull(cookie.getExpiryInstant());
271271
}
272272

273+
@Test
274+
void testBasicMaxAgeParseDeleteNow() throws Exception {
275+
final BasicClientCookie cookie = new BasicClientCookie("name", "value");
276+
final CookieAttributeHandler h = BasicMaxAgeHandler.INSTANCE;
277+
h.parse(cookie, "0");
278+
Assertions.assertEquals(Instant.EPOCH, cookie.getExpiryInstant());
279+
final BasicClientCookie cookie2 = new BasicClientCookie("name", "value");
280+
h.parse(cookie2, "-1");
281+
Assertions.assertEquals(Instant.EPOCH, cookie2.getExpiryInstant());
282+
}
283+
273284
@Test
274285
void testBasicMaxAgeParseInvalid() {
275286
final BasicClientCookie cookie = new BasicClientCookie("name", "value");

0 commit comments

Comments
 (0)