From 57a2122fd2b759b3d1a5c4769b1167857bc78f31 Mon Sep 17 00:00:00 2001 From: Thierry Boileau Date: Sat, 28 Nov 2026 14:08:37 +0100 Subject: [PATCH] Honour an If-Range date only if it is a strong validator (#1742) RFC 9110 section 13.1.5: a server must ignore an If-Range date unless it is a strong validator, i.e. the Last-Modified is at least one second older than the response (section 8.8.2.2). Conditions.getRangeStatus only tested that the two dates were equal, so a resource modified less than a second ago was served as a 206 instead of a full 200. DateUtils.isStrong holds the rule, shared with Conditions.unchangedSince. Co-Authored-By: Claude Sonnet 5.5 --- changes.md | 1 + .../java/org/restlet/data/Conditions.java | 28 +++++++----- .../org/restlet/engine/util/DateUtils.java | 17 +++++++ .../org/restlet/data/ConditionsTestCase.java | 18 ++++++++ .../application/RangeFilterTestCase.java | 32 ++++++++++++++ .../RangeRequestEndToEndTestCase.java | 44 ++++++++++++++++++- .../engine/util/DateUtilsTestCase.java | 21 +++++++++ 7 files changed, 149 insertions(+), 12 deletions(-) diff --git a/changes.md b/changes.md index e4da983bcc..d4b5aa66d4 100644 --- a/changes.md +++ b/changes.md @@ -3,6 +3,7 @@ Changes log - 2.7.1 (??-??-2026) - Bugs fixed + - An `If-Range` header carrying a date is now honoured only if that date is a strong validator, as RFC 9110 section 13.1.5 requires: a `Last-Modified` less than one second older than the response is weak, so the range is ignored and the whole entity is sent instead of a partial content. Issue #1742. - `DefaultSslContextFactory` now uses the JVM default algorithms for the trust manager (`TrustManagerFactory.getDefaultAlgorithm()`, `PKIX`) and the key manager (`KeyManagerFactory.getDefaultAlgorithm()`) instead of the obsolete `SunX509`, unless the `ssl.TrustManagerFactory.algorithm` or `ssl.KeyManagerFactory.algorithm` system property, the `trustManagerAlgorithm` or `keyManagerAlgorithm` parameter, or the setters say otherwise. A valid certificate chain that is not in order is no longer rejected with a "Certificate chaining error". The `PKIX` validation is the standard one, and stricter than `SunX509` on unusual chains: set the trust manager algorithm to `SunX509` to get the previous behavior. Issue #1397. - `Redirector.getTargetRef()` now removes the dot segments (`.` and `..`) of the target URI (RFC 3986, section 5.2.4), so the `Location` header of a client redirection and the URI of a server redirection are normalized. Relative-path targets such as `../x` are left untouched, as they need their base reference. A reverse proxy built on `http://backend/root/{rr}` now resolves a `..` found in the remaining part itself, which it did not before the request reached the backend. Issue #238. - The Jackson, Gson and JSON (org.json) converters now handle the `application/*+json` media types, such as `application/vnd.example+json`. They were ignored, so an incoming `+json` entity was not converted, and a `+json` response was left to another converter (such as JAXB) when it scored higher than the 0.5 given by Jackson. A custom converter that won for these media types may now lose to the JSON converters. Issue #1185. diff --git a/org.restlet/src/main/java/org/restlet/data/Conditions.java b/org.restlet/src/main/java/org/restlet/data/Conditions.java index 7f5a02bc75..76ee764355 100644 --- a/org.restlet/src/main/java/org/restlet/data/Conditions.java +++ b/org.restlet/src/main/java/org/restlet/data/Conditions.java @@ -82,7 +82,11 @@ public static Conditions ifAbsent() { * updates within the same second could not be told apart.
*
* Note that {@code new Tag(String)} creates a weak tag, which never matches an "If-Match" - * condition. + * condition.
+ *
+ * The date rule compares the modification date with the {@link Response#getDate() date of the + * response}. If the origin server sent an invalid "Date" header, the time of reception is used + * (RFC 9110 section 6.6.1), so the rule is then only as reliable as the clock of the client. * * @param read The response to the request that read the resource, with its entity. * @return The conditions of a request guarded against concurrent updates. @@ -107,12 +111,8 @@ public static Conditions unchangedSince(Response read) { return result; } - Date modified = entity.getModificationDate(); - Date responseDate = read.getDate(); - - if (modified != null - && responseDate != null - && responseDate.getTime() - modified.getTime() >= 1000) { + if (DateUtils.isStrong(entity.getModificationDate(), read.getDate())) { + Date modified = entity.getModificationDate(); result.setUnmodifiedSince(modified); return result; } @@ -230,7 +230,12 @@ public Status getRangeStatus(RepresentationInfo representationInfo) { } /** - * Returns the range conditional status of an entity. + * Returns the range conditional status of an entity.
+ *
+ * RFC 9110 section 13.1.5: a tag must match with the strong comparison, and a date is honoured + * only if it is a strong validator, that is if the modification date is at least one second + * older than the response (section 8.8.2.2). A modification date that recent never satisfies + * the condition, so the range is ignored and the whole entity is sent. * * @param tag The tag of the entity. * @param modificationDate The modification date of the entity. @@ -249,8 +254,11 @@ public Status getRangeStatus(Tag tag, Date modificationDate) { } else if (getRangeDate() != null && modificationDate != null - && DateUtils.equals(getRangeDate(), modificationDate)) { - // The dates of the headers have a precision of one second + && DateUtils.equals(getRangeDate(), modificationDate) + && DateUtils.isStrong(modificationDate, new Date())) { + // The dates of the headers have a precision of one second. RFC 9110 section 13.1.5: a + // date is honoured only if it is a strong validator (section 8.8.2.2). The "Date" of + // the response is written after this test and cannot be earlier than now. result = Status.SUCCESS_OK; } diff --git a/org.restlet/src/main/java/org/restlet/engine/util/DateUtils.java b/org.restlet/src/main/java/org/restlet/engine/util/DateUtils.java index 6102f45c49..c68295f042 100644 --- a/org.restlet/src/main/java/org/restlet/engine/util/DateUtils.java +++ b/org.restlet/src/main/java/org/restlet/engine/util/DateUtils.java @@ -54,6 +54,23 @@ public final class DateUtils { /** Remember the often used GMT time zone. */ private static final java.util.TimeZone TIMEZONE_GMT = java.util.TimeZone.getTimeZone("GMT"); + /** + * Indicates if a modification date can be used as a strong validator (RFC 9110 section + * 8.8.2.2): it must be at least one second older than the reference date, generally the date of + * the message that carries it, otherwise the representation may still change within the same + * second without the date changing. + * + * @param modificationDate The modification date (a "Last-Modified" value). + * @param referenceDate The date of the message ("Date" value). + * @return True if both dates are known and the modification date is at least one second before + * the reference date. + */ + public static boolean isStrong(final Date modificationDate, final Date referenceDate) { + return modificationDate != null + && referenceDate != null + && referenceDate.getTime() - modificationDate.getTime() >= 1000; + } + /** * Compares two date with a precision of one second. * diff --git a/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java b/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java index fef8e78d3d..d0bbda78b2 100644 --- a/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java +++ b/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java @@ -116,6 +116,24 @@ void getRangeStatus_matchingDate_returnsOk() { assertEquals(Status.SUCCESS_OK, conditions.getRangeStatus(null, date)); } + /** RFC 9110 section 13.1.5: an {@code If-Range} date is honoured only if it is strong. */ + @Test + void getRangeStatus_recentlyModified_dateIsWeakAndNeverMatches() { + Date justNow = new Date(); + Conditions conditions = new Conditions(); + conditions.setRangeDate(justNow); + assertEquals( + Status.CLIENT_ERROR_PRECONDITION_FAILED, conditions.getRangeStatus(null, justNow)); + } + + @Test + void getRangeStatus_modifiedSeveralSecondsAgo_dateIsStrongAndMatches() { + Date earlier = new Date(System.currentTimeMillis() - 3_600_000L); + Conditions conditions = new Conditions(); + conditions.setRangeDate(earlier); + assertEquals(Status.SUCCESS_OK, conditions.getRangeStatus(null, earlier)); + } + @Test void getRangeStatus_noConditionsSet_returnsPreconditionFailed() { Conditions conditions = new Conditions(); diff --git a/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java b/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java index 93f95d68ad..61cca4be43 100644 --- a/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java +++ b/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java @@ -13,6 +13,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.Date; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -320,4 +321,35 @@ void afterHandle_rangeOnGet_stillGivesPartialContent() { assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus()); } + + /** + * RFC 9110 section 13.1.5: a weak {@code If-Range} date is ignored, the whole entity is sent. + */ + @Test + void afterHandle_ifRangeDateOfARecentModification_servesTheWholeEntity() { + Date justNow = new Date(); + StringRepresentation entity = new StringRepresentation("0123456789"); + entity.setModificationDate(justNow); + Response response = successResponse(Method.GET, entity); + response.getRequest().getRanges().add(new Range(0, 4)); + response.getRequest().getConditions().setRangeDate(justNow); + + filter.afterHandle(response.getRequest(), response); + + assertEquals(Status.SUCCESS_OK, response.getStatus()); + } + + @Test + void afterHandle_ifRangeDateOfAnOldModification_givesPartialContent() { + Date earlier = new Date(System.currentTimeMillis() - 3_600_000L); + StringRepresentation entity = new StringRepresentation("0123456789"); + entity.setModificationDate(earlier); + Response response = successResponse(Method.GET, entity); + response.getRequest().getRanges().add(new Range(0, 4)); + response.getRequest().getConditions().setRangeDate(earlier); + + filter.afterHandle(response.getRequest(), response); + + assertEquals(Status.SUCCESS_PARTIAL_CONTENT, response.getStatus()); + } } diff --git a/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java b/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java index cd656f3b18..6ce1eafba7 100644 --- a/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java +++ b/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java @@ -16,6 +16,7 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; +import java.util.Date; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -30,6 +31,7 @@ import org.restlet.data.MediaType; import org.restlet.data.Protocol; import org.restlet.engine.Engine; +import org.restlet.engine.util.DateUtils; import org.restlet.representation.StringRepresentation; /** @@ -47,6 +49,9 @@ class RangeRequestEndToEndTestCase { private URI uri; + /** Modification date of the served entity, null for none. */ + private volatile Date modified; + @BeforeEach void setUpEach() throws Exception { component = new Component(); @@ -60,9 +65,11 @@ public Restlet createInboundRoot() { return new Restlet() { @Override public void handle(Request request, Response response) { - response.setEntity( + StringRepresentation entity = new StringRepresentation( - BODY, MediaType.TEXT_PLAIN)); + BODY, MediaType.TEXT_PLAIN); + entity.setModificationDate(modified); + response.setEntity(entity); } }; } @@ -192,4 +199,37 @@ void options_withRange_ignoresTheRange() throws Exception { void get_withRange_stillReturnsPartialContent() throws Exception { assertPartial("bytes=0-3", "0123", "bytes 0-3/10"); } + + private HttpResponse getIfRange(Date ifRange) throws Exception { + HttpRequest request = + HttpRequest.newBuilder(uri) + .GET() + .header("Range", "bytes=0-3") + .header("If-Range", DateUtils.format(ifRange)) + .build(); + return client.send(request, BodyHandlers.ofString()); + } + + /** RFC 9110 section 13.1.5: an {@code If-Range} date that is not strong is ignored (#1742). */ + @Test + void ifRangeDate_ofARecentModification_returnsTheWholeEntity() throws Exception { + modified = new Date(); + + HttpResponse response = getIfRange(modified); + + assertEquals(200, response.statusCode()); + assertEquals(BODY, response.body()); + assertEquals(null, header(response, "Content-Range")); + } + + @Test + void ifRangeDate_ofAnOldModification_returnsPartialContent() throws Exception { + modified = new Date(System.currentTimeMillis() - 3_600_000L); + + HttpResponse response = getIfRange(modified); + + assertEquals(206, response.statusCode()); + assertEquals("0123", response.body()); + assertEquals("bytes 0-3/10", header(response, "Content-Range")); + } } diff --git a/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java b/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java index 0f76cf71a7..0fae163d0d 100644 --- a/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java +++ b/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java @@ -9,6 +9,7 @@ package org.restlet.engine.util; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Calendar; @@ -112,4 +113,24 @@ void unmodifiableDates() { assertTrue(DateUtils.unmodifiable(yesterday).before(DateUtils.unmodifiable(now))); assertTrue(DateUtils.unmodifiable(yesterday).before(now)); } + + @Test + void isStrong_requiresTheReferenceToBeAtLeastOneSecondLater() { + Date modified = new Date(1_700_000_000_000L); + + assertFalse(DateUtils.isStrong(modified, new Date(modified.getTime()))); + assertFalse(DateUtils.isStrong(modified, new Date(modified.getTime() + 999))); + assertTrue(DateUtils.isStrong(modified, new Date(modified.getTime() + 1000))); + assertTrue(DateUtils.isStrong(modified, new Date(modified.getTime() + 3_600_000L))); + assertFalse(DateUtils.isStrong(modified, new Date(modified.getTime() - 5000))); + } + + @Test + void isStrong_withoutADate_isNotStrong() { + Date date = new Date(); + + assertFalse(DateUtils.isStrong(null, date)); + assertFalse(DateUtils.isStrong(date, null)); + assertFalse(DateUtils.isStrong(null, null)); + } }