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 0f5a809378 Accept an unknown complete length in the Content-Range
header (#1107)
0f5a809378 is described below
commit 0f5a80937897c8326244a4166f62d1e1d52bbc89
Author: Arturo Bernal <[email protected]>
AuthorDate: Tue Nov 17 11:57:09 2026 +0100
Accept an unknown complete length in the Content-Range header (#1107)
RFC 9110, section 14.4 allows an asterisk in place of the complete
length of a Content-Range header, such as "bytes 0-9/*". The parser
rejected it, so a partial PUT with an unknown complete length got a 400
response. Parse the asterisk as an unknown length and leave the length
of the resource unchanged in that case.
---
.../apache/catalina/servlets/DefaultServlet.java | 4 +-
.../tomcat/util/http/parser/ContentRange.java | 23 +++++--
.../catalina/servlets/TestDefaultServletPut.java | 3 +
.../tomcat/util/http/parser/TestContentRange.java | 71 ++++++++++++++++++++++
4 files changed, 94 insertions(+), 7 deletions(-)
diff --git a/java/org/apache/catalina/servlets/DefaultServlet.java
b/java/org/apache/catalina/servlets/DefaultServlet.java
index 361abfe422..eb44507696 100644
--- a/java/org/apache/catalina/servlets/DefaultServlet.java
+++ b/java/org/apache/catalina/servlets/DefaultServlet.java
@@ -777,7 +777,9 @@ public class DefaultServlet extends HttpServlet {
}
}
- randAccessContentFile.setLength(range.getLength());
+ if (range.getLength() != -1) {
+ randAccessContentFile.setLength(range.getLength());
+ }
// Append data in request input stream to contentFile
randAccessContentFile.seek(range.getStart());
diff --git a/java/org/apache/tomcat/util/http/parser/ContentRange.java
b/java/org/apache/tomcat/util/http/parser/ContentRange.java
index 9bae83c905..a720bbf58e 100644
--- a/java/org/apache/tomcat/util/http/parser/ContentRange.java
+++ b/java/org/apache/tomcat/util/http/parser/ContentRange.java
@@ -37,7 +37,7 @@ public class ContentRange {
* @param units the range units (e.g., "bytes"), lowercased
* @param start the start position of the range
* @param end the end position of the range
- * @param length the total length of the resource
+ * @param length the total length of the resource, or -1 if it is unknown
*/
public ContentRange(String units, long start, long end, long length) {
// Units are lower case (RFC 9110, section 14.1)
@@ -85,7 +85,7 @@ public class ContentRange {
/**
* Returns the total length of the resource.
*
- * @return the total resource length
+ * @return the total resource length, or -1 if it is unknown
*/
public long getLength() {
return length;
@@ -133,8 +133,15 @@ public class ContentRange {
return null;
}
- // Length
- length = HttpParser.readLong(input);
+ // Length, or '*' if it is unknown (RFC 9110, section 14.4)
+ if (HttpParser.skipConstant(input, "*") == SkipResult.FOUND) {
+ length = -1;
+ } else {
+ length = HttpParser.readLong(input);
+ if (length == -1) {
+ return null;
+ }
+ }
} catch (NumberFormatException nfe) {
// A value that doesn't fit in a long can't be a valid content
range
return null;
@@ -159,10 +166,14 @@ public class ContentRange {
/**
* Validates this content range according to RFC 9110 section 14.4.
+ * When the complete length is unknown, {@code end} must be less than
{@link Long#MAX_VALUE} to
+ * prevent integer overflow in computations such as {@code end - start +
1}.
*
- * @return {@code true} if start >= 0, end >= start, and length >
end
+ * @return {@code true} if start >= 0, end >= start, and either the
length is unknown with
+ * end < {@link Long#MAX_VALUE}, or the length is known and
length > end
*/
public boolean isValid() {
- return start >= 0 && end >= start && length > end;
+ return start >= 0 && end >= start &&
+ (length == -1 ? end < Long.MAX_VALUE : length > end);
}
}
diff --git a/test/org/apache/catalina/servlets/TestDefaultServletPut.java
b/test/org/apache/catalina/servlets/TestDefaultServletPut.java
index 5788a34656..5094d528db 100644
--- a/test/org/apache/catalina/servlets/TestDefaultServletPut.java
+++ b/test/org/apache/catalina/servlets/TestDefaultServletPut.java
@@ -55,6 +55,9 @@ public class TestDefaultServletPut extends TomcatBaseTest {
"Content-Range: bytes 0-" + (PATCH_LEN-1) + "/" + START_LEN +
CRLF, Boolean.TRUE, END_TEXT, Boolean.TRUE });
parameterSets.add(new Object[] {
"Content-Range: ByTeS 0-" + (PATCH_LEN-1) + "/" + START_LEN +
CRLF, Boolean.TRUE, END_TEXT, Boolean.TRUE });
+ // Valid partial PUT, complete length unknown (RFC 9110, section 14.4)
+ parameterSets.add(new Object[] {
+ "Content-Range: bytes 0-" + (PATCH_LEN-1) + "/*" + CRLF,
Boolean.TRUE, END_TEXT, Boolean.TRUE });
// Full PUT
parameterSets.add(new Object[] {
"", null, PATCH_TEXT, Boolean.TRUE });
diff --git a/test/org/apache/tomcat/util/http/parser/TestContentRange.java
b/test/org/apache/tomcat/util/http/parser/TestContentRange.java
new file mode 100644
index 0000000000..775a98e6ee
--- /dev/null
+++ b/test/org/apache/tomcat/util/http/parser/TestContentRange.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.util.http.parser;
+
+import java.io.IOException;
+import java.io.StringReader;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestContentRange {
+
+ @Test
+ public void testValid() throws Exception {
+ ContentRange r = parse("bytes 0-9/10");
+ Assert.assertEquals("bytes", r.getUnits());
+ Assert.assertEquals(0, r.getStart());
+ Assert.assertEquals(9, r.getEnd());
+ Assert.assertEquals(10, r.getLength());
+ }
+
+
+ @Test
+ public void testUnknownLength() throws Exception {
+ // RFC 9110, section 14.4: '*' means the complete length is unknown
+ ContentRange r = parse("bytes 0-9/*");
+ Assert.assertEquals("bytes", r.getUnits());
+ Assert.assertEquals(0, r.getStart());
+ Assert.assertEquals(9, r.getEnd());
+ Assert.assertEquals(-1, r.getLength());
+ }
+
+
+ @Test
+ public void testUnknownLengthMaxEnd() throws Exception {
+ // end == Long.MAX_VALUE with unknown length must be rejected: end+1
overflows
+ ContentRange r = new ContentRange("bytes", 0, Long.MAX_VALUE, -1);
+ Assert.assertFalse(r.isValid());
+ }
+
+
+ @Test
+ public void testInvalid() throws Exception {
+ Assert.assertNull(parse(""));
+ Assert.assertNull(parse("bytes 0-9/"));
+ Assert.assertNull(parse("bytes 0-9/**"));
+ Assert.assertNull(parse("bytes 0-9/*x"));
+ Assert.assertNull(parse("bytes 9-0/*"));
+ Assert.assertNull(parse("bytes 0-9/9"));
+ Assert.assertNull(parse("bytes */*"));
+ }
+
+
+ private ContentRange parse(String s) throws IOException {
+ return ContentRange.parse(new StringReader(s));
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]