This is an automated email from the ASF dual-hosted git repository. chibenwa pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-mime4j.git
commit 2242af35660d3a593053735f949e8d25ed51ddec Author: Benoit TELLIER <[email protected]> AuthorDate: Tue Nov 10 10:49:31 2026 +0100 [FIX] MimeEntity: a header line is not buffered beyond maxHeaderLen readRawField read each header line whole, then appended it to the field builder, which only then checked maxHeaderLen. Under MimeConfig.PERMISSIVE, maxLineLen is -1, so nothing bounded that read: a header made of "X-A: " followed by tens of MB without a line feed was buffered entirely, in a ByteArrayBuffer that grows by doubling (2 to 3 times its size at peak), before MaxHeaderLengthLimitException was thrown. The headers of body parts were read the same way. James parses every stored message with PERMISSIVE, often in parallel, so a burst of such messages could exhaust the heap, despite the javadoc promising the profile stays bounded. LineReaderInputStream gets readLine(dst, maxLen), which stops once dst holds more than maxLen bytes and no line feed; BufferedLineReaderInputStream, MimeBoundaryInputStream and LineReaderInputStreamAdaptor implement it, and the default implementation keeps the old behaviour for other subclasses. readRawField reads with maxHeaderLen + 1 as the bound, so it only cuts short a line no field of maxHeaderLen bytes can hold, and then throws MaxHeaderLengthLimitException. Fields of exactly maxHeaderLen bytes are still accepted, and body lines stay unbounded under PERMISSIVE. The javadoc of PERMISSIVE now says what is bounded. --- .../mime4j/io/BufferedLineReaderInputStream.java | 9 ++ .../james/mime4j/io/LineReaderInputStream.java | 31 ++++ .../mime4j/io/LineReaderInputStreamAdaptor.java | 15 +- .../james/mime4j/io/MimeBoundaryInputStream.java | 8 + .../org/apache/james/mime4j/stream/MimeConfig.java | 6 + .../org/apache/james/mime4j/stream/MimeEntity.java | 12 +- .../james/mime4j/stream/HeaderLineBoundTest.java | 169 +++++++++++++++++++++ 7 files changed, 246 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/apache/james/mime4j/io/BufferedLineReaderInputStream.java b/core/src/main/java/org/apache/james/mime4j/io/BufferedLineReaderInputStream.java index 52323276..f43c0f02 100644 --- a/core/src/main/java/org/apache/james/mime4j/io/BufferedLineReaderInputStream.java +++ b/core/src/main/java/org/apache/james/mime4j/io/BufferedLineReaderInputStream.java @@ -203,6 +203,12 @@ public class BufferedLineReaderInputStream extends LineReaderInputStream { @Override public int readLine(final ByteArrayBuffer dst) throws MaxLineLimitException, IOException { + return readLine(dst, -1); + } + + @Override + public int readLine(final ByteArrayBuffer dst, int maxLen) + throws MaxLineLimitException, IOException { if (dst == null) { throw new IllegalArgumentException("Buffer may not be null"); } @@ -234,6 +240,9 @@ public class BufferedLineReaderInputStream extends LineReaderInputStream { if (exceedsMaxLineLen(dst, this.maxLineLen)) { throw new MaxLineLimitException("Maximum line length limit (" + this.maxLineLen +") exceeded"); } + if (!found && maxLen > 0 && dst.length() > maxLen) { + break; + } } if (total == 0 && bytesRead == -1) { return -1; diff --git a/core/src/main/java/org/apache/james/mime4j/io/LineReaderInputStream.java b/core/src/main/java/org/apache/james/mime4j/io/LineReaderInputStream.java index 89127bc1..e23ae7fc 100644 --- a/core/src/main/java/org/apache/james/mime4j/io/LineReaderInputStream.java +++ b/core/src/main/java/org/apache/james/mime4j/io/LineReaderInputStream.java @@ -49,6 +49,37 @@ public abstract class LineReaderInputStream extends FilterInputStream { public abstract int readLine(final ByteArrayBuffer dst) throws MaxLineLimitException, IOException; + /** + * Reads one line of text into the given {@link ByteArrayBuffer}, but stops once + * <code>dst</code> holds more than <code>maxLen</code> bytes and no line feed: the rest of + * the line is then left unread, so that a caller with its own bound does not buffer a line + * it is bound to reject, whatever the line length limit of this stream. + * + * <p>The default implementation ignores <code>maxLen</code>.</p> + * + * @param dst Destination + * @param maxLen bound on the bytes held by <code>dst</code> before the line is cut short, + * a non positive value disables it + * @return number of bytes copied or <code>-1</code> if the end of + * the stream has been reached. + * + * @throws MaxLineLimitException if the line exceeds a limit on + * the line length imposed by a subclass. + * @throws IOException in case of an I/O error. + */ + public int readLine(final ByteArrayBuffer dst, int maxLen) + throws MaxLineLimitException, IOException { + return readLine(dst); + } + + /** + * Whether a line read with {@link #readLine(ByteArrayBuffer, int)} was cut short by its bound. + */ + public static boolean isCutShort(ByteArrayBuffer dst, int maxLen) { + int len = dst.length(); + return maxLen > 0 && len > maxLen && dst.byteAt(len - 1) != '\n'; + } + /** * Tries to unread the last read line. * diff --git a/core/src/main/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptor.java b/core/src/main/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptor.java index caf53f1c..0eef6b06 100644 --- a/core/src/main/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptor.java +++ b/core/src/main/java/org/apache/james/mime4j/io/LineReaderInputStreamAdaptor.java @@ -81,18 +81,24 @@ public class LineReaderInputStreamAdaptor extends LineReaderInputStream { @Override public int readLine(final ByteArrayBuffer dst) throws MaxLineLimitException, IOException { + return readLine(dst, -1); + } + + @Override + public int readLine(final ByteArrayBuffer dst, int maxLen) + throws MaxLineLimitException, IOException { int i; if (this.bis != null) { - i = this.bis.readLine(dst); + i = this.bis.readLine(dst, maxLen); } else { - i = doReadLine(dst); + i = doReadLine(dst, maxLen); } this.eof = i == -1; this.used = true; return i; } - private int doReadLine(final ByteArrayBuffer dst) + private int doReadLine(final ByteArrayBuffer dst, int maxLen) throws MaxLineLimitException, IOException { int total = 0; int ch; @@ -105,6 +111,9 @@ public class LineReaderInputStreamAdaptor extends LineReaderInputStream { if (ch == '\n') { break; } + if (maxLen > 0 && dst.length() > maxLen) { + break; + } } if (total == 0 && ch == -1) { return -1; diff --git a/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java b/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java index 6169d39d..69a8e5ef 100644 --- a/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java +++ b/core/src/main/java/org/apache/james/mime4j/io/MimeBoundaryInputStream.java @@ -192,6 +192,11 @@ public class MimeBoundaryInputStream extends LineReaderInputStream { @Override public int readLine(final ByteArrayBuffer dst) throws IOException { + return readLine(dst, -1); + } + + @Override + public int readLine(final ByteArrayBuffer dst, int maxLen) throws IOException { if (dst == null) { throw new IllegalArgumentException("Destination buffer may not be null"); } @@ -224,6 +229,9 @@ public class MimeBoundaryInputStream extends LineReaderInputStream { this.buffer.skip(chunk); total += chunk; } + if (!found && maxLen > 0 && dst.length() > maxLen) { + break; + } } if (total == 0 && bytesRead == -1) { return -1; diff --git a/core/src/main/java/org/apache/james/mime4j/stream/MimeConfig.java b/core/src/main/java/org/apache/james/mime4j/stream/MimeConfig.java index f35adcf0..d6131c1b 100644 --- a/core/src/main/java/org/apache/james/mime4j/stream/MimeConfig.java +++ b/core/src/main/java/org/apache/james/mime4j/stream/MimeConfig.java @@ -37,6 +37,12 @@ public final class MimeConfig { * <li>{@code james.mime4j.permissive.max.line.length} (default -1, unbounded)</li> * </ul> * A non positive value disables the corresponding check. + * + * <p>Body lines are not bounded by default, but header lines always are: a header line is + * not buffered beyond the header length limit, so a header made of one huge line fails with a + * {@link org.apache.james.mime4j.io.MaxHeaderLengthLimitException} once that limit is reached. + * The memory used for the headers of a message is thus bounded when that limit is set; the + * memory used for a body depends on the {@code BodyFactory}.</p> */ public static final MimeConfig PERMISSIVE = MimeConfig.custom() .setMaxContentLen(Long.getLong("james.mime4j.permissive.max.content.length", 100 * 1024 * 1024)) diff --git a/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java b/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java index dc02ef2c..99f9b434 100644 --- a/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java +++ b/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java @@ -35,6 +35,7 @@ import org.apache.james.mime4j.io.LineNumberSource; import org.apache.james.mime4j.io.LineReaderInputStream; import org.apache.james.mime4j.io.LineReaderInputStreamAdaptor; import org.apache.james.mime4j.io.MaxContentLengthLimitException; +import org.apache.james.mime4j.io.MaxHeaderLengthLimitException; import org.apache.james.mime4j.io.MaxHeaderLimitException; import org.apache.james.mime4j.io.MaxLineLimitException; import org.apache.james.mime4j.io.MimeBoundaryInputStream; @@ -233,6 +234,10 @@ class MimeEntity implements EntityStateMachine { if (endOfHeader) throw new IllegalStateException(); LineReaderInputStream instream = getDataStream(); + // A line of more than maxHeaderLen bytes cannot belong to an accepted field: stop + // buffering it there, even when the line length is not bounded (MimeConfig.PERMISSIVE) + int maxHeaderLen = config.getMaxHeaderLen(); + int lineBound = maxHeaderLen > 0 && maxHeaderLen < Integer.MAX_VALUE ? maxHeaderLen + 1 : -1; try { for (;;) { // If there's still data stuck in the line buffer @@ -242,7 +247,12 @@ class MimeEntity implements EntityStateMachine { fieldBuilder.append(linebuf); } linebuf.clear(); - if (instream.readLine(linebuf) == -1) { + int read = instream.readLine(linebuf, lineBound); + if (LineReaderInputStream.isCutShort(linebuf, lineBound)) { + linebuf.clear(); + throw new MaxHeaderLengthLimitException("Maximum header length limit (" + maxHeaderLen + ") exceeded"); + } + if (read == -1) { if (!stopped) { // A header cut short by stop() is not a malformed message monitor(Event.HEADERS_PREMATURE_END); diff --git a/core/src/test/java/org/apache/james/mime4j/stream/HeaderLineBoundTest.java b/core/src/test/java/org/apache/james/mime4j/stream/HeaderLineBoundTest.java new file mode 100644 index 00000000..4af1eeaf --- /dev/null +++ b/core/src/test/java/org/apache/james/mime4j/stream/HeaderLineBoundTest.java @@ -0,0 +1,169 @@ +/**************************************************************** + * 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.james.mime4j.stream; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.SequenceInputStream; +import java.nio.charset.StandardCharsets; + +import org.apache.james.mime4j.io.MaxHeaderLengthLimitException; +import org.junit.Assert; +import org.junit.Test; + +/** + * A header line is not buffered beyond maxHeaderLen, even when maxLineLen does not bound it + * (as in {@link MimeConfig#PERMISSIVE}). + */ +public class HeaderLineBoundTest { + + private static final int MAX_HEADER_LEN = 64 * 1024; + private static final long TEN_MB = 10L * 1024 * 1024; + + /** + * Serves <code>size</code> bytes of 'a' with no line feed, and counts what is read. + */ + private static class LongLine extends InputStream { + private final long size; + private long served; + + LongLine(long size) { + this.size = size; + } + + @Override + public int read() { + if (served >= size) { + return -1; + } + served++; + return 'a'; + } + + @Override + public int read(byte[] b, int off, int len) { + if (len == 0) { + return 0; + } + if (served >= size) { + return -1; + } + int n = (int) Math.min(len, size - served); + for (int i = 0; i < n; i++) { + b[off + i] = 'a'; + } + served += n; + return n; + } + } + + private static InputStream ascii(String s) { + return new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); + } + + private static void parseAll(MimeConfig config, InputStream in) throws Exception { + MimeTokenStream stream = new MimeTokenStream(config); + stream.parse(in); + for (EntityState state = stream.getState(); + state != EntityState.T_END_OF_STREAM; + state = stream.next()) { + // just walk the stream + } + } + + private static void assertRejectedEarly(InputStream prefix) throws Exception { + LongLine line = new LongLine(TEN_MB); + try { + parseAll(MimeConfig.PERMISSIVE, new SequenceInputStream(prefix, line)); + Assert.fail("Expected MaxHeaderLengthLimitException"); + } catch (MaxHeaderLengthLimitException expected) { + // a few read buffers past the limit at most, not the whole line + Assert.assertTrue("read " + line.served + " bytes of the line", line.served < MAX_HEADER_LEN + 64 * 1024); + } + } + + @Test + public void permissiveShouldStopReadingAHugeMessageHeaderLineAtTheHeaderLimit() throws Exception { + assertRejectedEarly(ascii("X-A: ")); + } + + @Test + public void permissiveShouldStopReadingAHugeFoldedHeaderLineAtTheHeaderLimit() throws Exception { + assertRejectedEarly(ascii("X-A: b\r\n ")); + } + + @Test + public void permissiveShouldStopReadingAHugeBodyPartHeaderLineAtTheHeaderLimit() throws Exception { + assertRejectedEarly(ascii("Content-Type: multipart/mixed; boundary=b\r\n\r\n--b\r\nX-A: ")); + } + + private static MimeConfig headerLimit(int maxHeaderLen) { + return MimeConfig.custom().setMaxLineLen(-1).setMaxHeaderLen(maxHeaderLen).build(); + } + + private static String field(int len) { + StringBuilder sb = new StringBuilder("X-A: "); + while (sb.length() < len) { + sb.append('a'); + } + return sb.toString(); + } + + @Test + public void aHeaderOfExactlyTheLimitShouldStillBeAccepted() throws Exception { + // measured with a single LF: 99 bytes + LF + parseAll(headerLimit(100), ascii(field(99) + "\r\n\r\nbody")); + parseAll(headerLimit(100), ascii(field(99) + "\n\nbody")); + // unterminated at the end of the input + parseAll(headerLimit(100), ascii(field(100))); + } + + @Test + public void aHeaderOneByteOverTheLimitShouldBeRejected() throws Exception { + String[] inputs = {field(100) + "\r\n\r\nbody", field(100) + "\n\nbody", field(101)}; + for (String input : inputs) { + try { + parseAll(headerLimit(100), ascii(input)); + Assert.fail("Expected MaxHeaderLengthLimitException for " + input.length() + " bytes"); + } catch (MaxHeaderLengthLimitException expected) { + // expected + } + } + } + + @Test + public void bodyLinesShouldStayUnboundedUnderPermissive() throws Exception { + MimeTokenStream stream = new MimeTokenStream(MimeConfig.PERMISSIVE); + LongLine body = new LongLine(1024 * 1024); + stream.parse(new SequenceInputStream(ascii("X-A: b\r\n\r\n"), body)); + for (EntityState state = stream.getState(); + state != EntityState.T_END_OF_STREAM; + state = stream.next()) { + if (state == EntityState.T_BODY) { + InputStream in = stream.getInputStream(); + byte[] buf = new byte[8192]; + while (in.read(buf) != -1) { + // drain + } + } + } + Assert.assertEquals(1024 * 1024, body.served); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
