This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new 47e549296b Do not send a Last-Modified date later than the response 
date (#1152)
47e549296b is described below

commit 47e549296b336a45dce319f91993c7caf5f5f92c
Author: Arturo Bernal <[email protected]>
AuthorDate: Mon Nov 16 12:47:05 2026 +0100

    Do not send a Last-Modified date later than the response date (#1152)
    
    RFC 9110, section 8.8.2.1 states that an origin server MUST NOT generate
    a Last-Modified date that is later than the time of message origination
    and that, if the last modification time of the resource is in the future,
    the server MUST replace that value with the message origination date. The
    last modified time of a resource was always sent as is, so a resource with
    a modification time in the future produced a Last-Modified header field
    later than the Date header field. Use the current time in that case.
---
 .../apache/catalina/servlets/DefaultServlet.java   | 15 +++-
 .../catalina/servlets/TestDefaultServlet.java      | 81 ++++++++++++++++++++++
 webapps/docs/changelog.xml                         |  5 ++
 3 files changed, 100 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/catalina/servlets/DefaultServlet.java 
b/java/org/apache/catalina/servlets/DefaultServlet.java
index c5b1fa8d92..8f3f4a1c74 100644
--- a/java/org/apache/catalina/servlets/DefaultServlet.java
+++ b/java/org/apache/catalina/servlets/DefaultServlet.java
@@ -987,7 +987,7 @@ public class DefaultServlet extends HttpServlet {
 
         if (resource.isFile() && !isError) {
             eTag = generateETag(resource);
-            lastModifiedHttp = resource.getLastModifiedHttp();
+            lastModifiedHttp = getLastModifiedHttp(resource);
         }
 
         // Check if the conditions specified in the optional If headers are
@@ -1289,6 +1289,19 @@ public class DefaultServlet extends HttpServlet {
     }
 
 
+    /*
+     * A last modified time in the future must be replaced with the message 
origination date (RFC 9110, section
+     * 8.8.2.1). This is done for each response so the metadata of the 
resource, which may be cached, is not changed.
+     */
+    private static String getLastModifiedHttp(WebResource resource) {
+        long now = System.currentTimeMillis();
+        if (resource.getLastModified() > now) {
+            return FastHttpDateFormat.formatDate(now);
+        }
+        return resource.getLastModifiedHttp();
+    }
+
+
     /*
      * Code borrowed heavily from Jasper's EncodingDetector
      */
diff --git a/test/org/apache/catalina/servlets/TestDefaultServlet.java 
b/test/org/apache/catalina/servlets/TestDefaultServlet.java
index f0271bf9f3..2e2a6bc358 100644
--- a/test/org/apache/catalina/servlets/TestDefaultServlet.java
+++ b/test/org/apache/catalina/servlets/TestDefaultServlet.java
@@ -26,6 +26,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
+import java.nio.file.Files;
 import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Date;
@@ -51,6 +52,7 @@ import org.apache.catalina.startup.TomcatBaseTest;
 import org.apache.coyote.http11.AbstractHttp11Protocol;
 import org.apache.tomcat.util.buf.ByteChunk;
 import org.apache.tomcat.util.descriptor.web.ErrorPage;
+import org.apache.tomcat.util.http.FastHttpDateFormat;
 import org.apache.tomcat.websocket.server.WsContextListener;
 
 public class TestDefaultServlet extends TomcatBaseTest {
@@ -95,6 +97,85 @@ public class TestDefaultServlet extends TomcatBaseTest {
 
     }
 
+    /*
+     * RFC 9110, section 8.8.2.1: an origin server must not generate a 
Last-Modified date that is later than the Date
+     * of the response. If the last modification time of the resource is in 
the future the message origination date
+     * must be used.
+     */
+    @Test
+    public void testLastModifiedNotInFuture() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+
+        File appDir = new File(getTemporaryDirectory(), 
"testLastModifiedNotInFuture");
+        addDeleteOnTearDown(appDir);
+        Assert.assertTrue(appDir.mkdirs());
+        File futureFile = new File(appDir, "future.txt");
+        Files.writeString(futureFile.toPath(), "future");
+        File pastFile = new File(appDir, "past.txt");
+        Files.writeString(pastFile.toPath(), "past");
+        long now = System.currentTimeMillis();
+        Assert.assertTrue(futureFile.setLastModified(now + 24 * 60 * 60 * 
1000L));
+        Assert.assertTrue(pastFile.setLastModified(now - 24 * 60 * 60 * 
1000L));
+
+        Context ctxt = tomcat.addContext("", appDir.getAbsolutePath());
+        Tomcat.addServlet(ctxt, "default", 
"org.apache.catalina.servlets.DefaultServlet");
+        ctxt.addServletMapping("/", "default");
+
+        tomcat.start();
+
+        Map<String,List<String>> headers = new HashMap<>();
+        Assert.assertEquals(HttpServletResponse.SC_OK,
+                getUrl("http://localhost:"; + getPort() + "/future.txt", new 
ByteChunk(), headers));
+        long lastModified = 
FastHttpDateFormat.parseDate(headers.get("Last-Modified").get(0));
+        long date = FastHttpDateFormat.parseDate(headers.get("Date").get(0));
+        Assert.assertTrue("Last-Modified is later than Date", lastModified <= 
date);
+        // The date is replaced by the time of the response, not by some 
arbitrary earlier time
+        Assert.assertTrue("Last-Modified is not the message origination date", 
lastModified >= (now / 1000) * 1000);
+
+        // A last modified time in the past is not changed
+        headers = new HashMap<>();
+        Assert.assertEquals(HttpServletResponse.SC_OK,
+                getUrl("http://localhost:"; + getPort() + "/past.txt", new 
ByteChunk(), headers));
+        
Assert.assertEquals(FastHttpDateFormat.formatDate(pastFile.lastModified()), 
headers.get("Last-Modified").get(0));
+    }
+
+    /*
+     * A resource that is served from the resource cache must still be given 
the message origination date of each
+     * response, not the date of the response that populated the cache.
+     */
+    @Test
+    public void testLastModifiedNotInFutureCached() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+
+        File appDir = new File(getTemporaryDirectory(), 
"testLastModifiedNotInFutureCached");
+        addDeleteOnTearDown(appDir);
+        Assert.assertTrue(appDir.mkdirs());
+        File futureFile = new File(appDir, "future.txt");
+        Files.writeString(futureFile.toPath(), "future");
+        
Assert.assertTrue(futureFile.setLastModified(System.currentTimeMillis() + 24 * 
60 * 60 * 1000L));
+
+        Context ctxt = tomcat.addContext("", appDir.getAbsolutePath());
+        Tomcat.addServlet(ctxt, "default", 
"org.apache.catalina.servlets.DefaultServlet");
+        ctxt.addServletMapping("/", "default");
+
+        tomcat.start();
+
+        // The cache TTL (default 5 seconds) must be longer than the delay 
between the requests
+        Assert.assertTrue(ctxt.getResources().getCacheTtl() > 3000);
+
+        for (int i = 0; i < 2; i++) {
+            Map<String,List<String>> headers = new HashMap<>();
+            Assert.assertEquals(HttpServletResponse.SC_OK,
+                    getUrl("http://localhost:"; + getPort() + "/future.txt", 
new ByteChunk(), headers));
+            long lastModified = 
FastHttpDateFormat.parseDate(headers.get("Last-Modified").get(0));
+            long date = 
FastHttpDateFormat.parseDate(headers.get("Date").get(0));
+            Assert.assertTrue("Request " + i + ": Last-Modified is later than 
Date", lastModified <= date);
+            Assert.assertTrue("Request " + i + ": Last-Modified is not the 
message origination date",
+                    date - lastModified <= 1000);
+            Thread.sleep(2000);
+        }
+    }
+
     @Test
     public void testDefaultCompression() throws Exception {
 
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 9971582c73..16d3e33363 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -170,6 +170,11 @@
         Skip empty elements when parsing entity tag lists. Pull request
         <pr>1150</pr> provided by Arturo Bernal. (markt)
       </fix>
+      <fix>
+        Ensure that the <code>Last-Modified</code> header, if present, does not
+        represent a date in the future. Pull request <pr>1152</pr> provided by
+        Arturo Bernal. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to