From d60a1e08adad717299abb940670bc8bcc3a4e3d7 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 | 29 ++++++++---- .../org/restlet/engine/util/DateUtils.java | 19 ++++++++ .../org/restlet/data/ConditionsTestCase.java | 18 ++++++++ .../application/RangeFilterTestCase.java | 32 ++++++++++++++ .../RangeRequestEndToEndTestCase.java | 44 ++++++++++++++++++- .../engine/util/DateUtilsTestCase.java | 21 +++++++++ 7 files changed, 153 insertions(+), 11 deletions(-) diff --git a/changes.md b/changes.md index e472eae0ad..8bf0afac48 100644 --- a/changes.md +++ b/changes.md @@ -3,6 +3,7 @@ Changes log - 2.6.2 (??-??-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.java/org.restlet/src/main/java/org/restlet/data/Conditions.java b/org.restlet.java/org.restlet/src/main/java/org/restlet/data/Conditions.java index b1843e52f8..ef36c0d0eb 100644 --- a/org.restlet.java/org.restlet/src/main/java/org/restlet/data/Conditions.java +++ b/org.restlet.java/org.restlet/src/main/java/org/restlet/data/Conditions.java @@ -94,7 +94,12 @@ 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. @@ -119,12 +124,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; } @@ -247,7 +248,13 @@ 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. @@ -268,7 +275,11 @@ public Status getRangeStatus(Tag tag, Date modificationDate) { } else if (getRangeDate() != null) { // If a modification date exists. The dates of the headers have a // precision of one second. - if (modificationDate != null && DateUtils.equals(getRangeDate(), modificationDate)) { + if (modificationDate != null && DateUtils.equals(getRangeDate(), modificationDate) + && DateUtils.isStrong(modificationDate, new Date())) { + // 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.java/org.restlet/src/main/java/org/restlet/engine/util/DateUtils.java b/org.restlet.java/org.restlet/src/main/java/org/restlet/engine/util/DateUtils.java index df8cfba2e4..f8d0dd7c2e 100644 --- a/org.restlet.java/org.restlet/src/main/java/org/restlet/engine/util/DateUtils.java +++ b/org.restlet.java/org.restlet/src/main/java/org/restlet/engine/util/DateUtils.java @@ -55,6 +55,25 @@ 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.java/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java b/org.restlet.java/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java index 2b5a896866..66afde41ad 100644 --- a/org.restlet.java/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java +++ b/org.restlet.java/org.restlet/src/test/java/org/restlet/data/ConditionsTestCase.java @@ -117,6 +117,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.java/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java b/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java index 9012a7c827..9de4543e8e 100644 --- a/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java +++ b/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeFilterTestCase.java @@ -14,6 +14,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; @@ -321,4 +322,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.java/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java b/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java index 7dfccad006..e4db13891b 100644 --- a/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java +++ b/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/application/RangeRequestEndToEndTestCase.java @@ -17,6 +17,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; @@ -31,6 +32,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; /** @@ -48,6 +50,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(); @@ -61,9 +66,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); } }; } @@ -193,4 +200,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.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java b/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java index a4966e7204..b058ceaaf2 100644 --- a/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java +++ b/org.restlet.java/org.restlet/src/test/java/org/restlet/engine/util/DateUtilsTestCase.java @@ -16,6 +16,7 @@ import java.util.GregorianCalendar; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -143,4 +144,24 @@ public void unmodifiableDates() { DateUtils.unmodifiable(now))); assertTrue(DateUtils.unmodifiable(yesterday).before(now)); } + + @Test + public 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 + public void isStrong_withoutADate_isNotStrong() { + Date date = new Date(); + + assertFalse(DateUtils.isStrong(null, date)); + assertFalse(DateUtils.isStrong(date, null)); + assertFalse(DateUtils.isStrong(null, null)); + } }