Repository: james-mime4j Updated Branches: refs/heads/master a07e48939 -> 06852dd8b
MIME4J-269 Expose a convenient 100 Mo limited "permissive" mime config limit This aims at remaining very permissive while still denying a single email to use all JVM memory. Project: http://git-wip-us.apache.org/repos/asf/james-mime4j/repo Commit: http://git-wip-us.apache.org/repos/asf/james-mime4j/commit/06852dd8 Tree: http://git-wip-us.apache.org/repos/asf/james-mime4j/tree/06852dd8 Diff: http://git-wip-us.apache.org/repos/asf/james-mime4j/diff/06852dd8 Branch: refs/heads/master Commit: 06852dd8b3f6b55f41aba12a02a7aa0c0c0e8aea Parents: 9d0a79d Author: benwa <btell...@linagora.com> Authored: Tue Mar 13 09:36:55 2018 +0700 Committer: benwa <btell...@linagora.com> Committed: Tue Mar 13 16:55:59 2018 +0700 ---------------------------------------------------------------------- .../apache/james/mime4j/stream/MimeConfig.java | 4 +- .../mime4j/dom/LargeMessageParsingTest.java | 82 ++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-mime4j/blob/06852dd8/core/src/main/java/org/apache/james/mime4j/stream/MimeConfig.java ---------------------------------------------------------------------- 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 09c4b28..e58d063 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 @@ -26,8 +26,8 @@ import org.apache.james.mime4j.MimeException; */ public final class MimeConfig { - public static final MimeConfig LENIENT = MimeConfig.custom() - .setMaxContentLen(-1) + public static final MimeConfig PERMISSIVE = MimeConfig.custom() + .setMaxContentLen(100 * 1024 * 1024) .setMaxHeaderCount(-1) .setMaxHeaderLen(-1) .setMaxLineLen(-1) http://git-wip-us.apache.org/repos/asf/james-mime4j/blob/06852dd8/dom/src/test/java/org/apache/james/mime4j/dom/LargeMessageParsingTest.java ---------------------------------------------------------------------- diff --git a/dom/src/test/java/org/apache/james/mime4j/dom/LargeMessageParsingTest.java b/dom/src/test/java/org/apache/james/mime4j/dom/LargeMessageParsingTest.java new file mode 100644 index 0000000..abde787 --- /dev/null +++ b/dom/src/test/java/org/apache/james/mime4j/dom/LargeMessageParsingTest.java @@ -0,0 +1,82 @@ +/**************************************************************** + * 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.dom; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +import org.apache.james.mime4j.message.DefaultMessageBuilder; +import org.apache.james.mime4j.stream.MimeConfig; +import org.junit.Test; + +public class LargeMessageParsingTest { + + @Test + public void parsingALargeMessageWithPermissiveConfigShouldSucceed() throws Exception { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(100 * 1024 * 1024); + // 32 * 1.000.000 = ~ 30,5 Mo of headers + for (int i = 0; i < 1000000; i++) { + outputStream.write(String.format("header: static important value\r\n", i, i).getBytes()); + } + outputStream.write("\r\n".getBytes()); + // 38 * 1.600.000 = ~ 58 Mo of body + for (int i = 0; i < 1600000; i++) { + outputStream.write(String.format("abcdeghijklmnopqrstuvwxyz0123456789\r\n", i, i).getBytes()); + } + + DefaultMessageBuilder messageBuilder = new DefaultMessageBuilder(); + messageBuilder.setMimeEntityConfig(MimeConfig.PERMISSIVE); + messageBuilder.parseMessage(new ByteArrayInputStream(outputStream.toByteArray())); + } + + @Test + public void parsingAMessageWithLongLinesWithPermissiveConfigShouldSucceed() throws Exception { + ByteArrayOutputStream longLineOutputStream = new ByteArrayOutputStream( 1024 * 1024); + ByteArrayOutputStream longHeaderOutputStream = new ByteArrayOutputStream( 1024 * 1024); + + longHeaderOutputStream.write("header: ".getBytes()); + // Each header is ~ 500 Ko + for (int i = 0; i < 50 * 1024; i++) { + longHeaderOutputStream.write("0123456789".getBytes()); + } + longHeaderOutputStream.write("\r\n".getBytes()); + + // Each line is ~ 1Mo + for (int i = 0; i < 100 * 1024; i++) { + longLineOutputStream.write("0123456789".getBytes()); + } + longLineOutputStream.write("\r\n".getBytes()); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(100 * 1024 * 1024); + // 60 * 0.5 = ~ 30 Mo of headers + for (int i = 0; i < 60; i++) { + outputStream.write(longHeaderOutputStream.toByteArray()); + } + outputStream.write("\r\n".getBytes()); + // 60 * 1 = ~ 60 Mo of body + for (int i = 0; i < 60; i++) { + outputStream.write(longLineOutputStream.toByteArray()); + } + + DefaultMessageBuilder messageBuilder = new DefaultMessageBuilder(); + messageBuilder.setMimeEntityConfig(MimeConfig.PERMISSIVE); + messageBuilder.parseMessage(new ByteArrayInputStream(outputStream.toByteArray())); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org