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

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


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

commit d92068c7f1b118272414dda67b257758210350e3
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 ++++++++++++++++++++++
 2 files changed, 95 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/catalina/servlets/DefaultServlet.java 
b/java/org/apache/catalina/servlets/DefaultServlet.java
index e0cd75eae9..b7a2794288 100644
--- a/java/org/apache/catalina/servlets/DefaultServlet.java
+++ b/java/org/apache/catalina/servlets/DefaultServlet.java
@@ -984,7 +984,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
@@ -1287,6 +1287,19 @@ public class DefaultServlet extends HttpServlet {
     /*
      * Code borrowed heavily from Jasper's EncodingDetector
      */
+    /*
+     * 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();
+    }
+
+
     private static Charset processBom(InputStream is, boolean stripBom) throws 
IOException {
         // Java supported character sets do not use BOMs longer than 4 bytes
         byte[] bom = new byte[4];
diff --git a/test/org/apache/catalina/servlets/TestDefaultServlet.java 
b/test/org/apache/catalina/servlets/TestDefaultServlet.java
index 4a5e86d1dd..e84a8266fe 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 {
 


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

Reply via email to