james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r880712 - in /james/mime4j/trunk/core/src: main/java/org/apache/james/mime4j/parser/AbstractEntity.java main/java/org/apache/james/mime4j/parser/MimeEntityConfig.java test/java/org/apache/james/mime4j/parser/MimeEntityTest.java
Date Mon, 16 Nov 2009 11:25:48 GMT
Author: mwiederkehr
Date: Mon Nov 16 11:25:48 2009
New Revision: 880712

URL: http://svn.apache.org/viewvc?rev=880712&view=rev
Log:
Added configuration parameter maxHeaderLen (MIME4J-140).

Modified:
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/AbstractEntity.java
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeEntityConfig.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/AbstractEntity.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/AbstractEntity.java?rev=880712&r1=880711&r2=880712&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/AbstractEntity.java
(original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/AbstractEntity.java
Mon Nov 16 11:25:48 2009
@@ -126,7 +126,7 @@
         if (endOfHeader) 
             throw new IllegalStateException();
 
-        int maxLineLen = config.getMaxLineLen();
+        int maxHeaderLen = config.getMaxHeaderLen();
         LineReaderInputStream instream = getDataStream();
         ByteArrayBuffer fieldbuf = new ByteArrayBuffer(64);
 
@@ -135,8 +135,8 @@
                 // If there's still data stuck in the line buffer
                 // copy it to the field buffer
                 int len = linebuf.length();
-                if (maxLineLen > 0 && fieldbuf.length() + len >= maxLineLen)
{
-                    throw new MaxLineLimitException("Maximum line length limit exceeded");
+                if (maxHeaderLen > 0 && fieldbuf.length() + len >= maxHeaderLen)
{
+                    throw new MaxLineLimitException("Maximum header length limit exceeded");
                 }
                 if (len > 0) {
                     fieldbuf.append(linebuf.buffer(), 0, len);

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeEntityConfig.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeEntityConfig.java?rev=880712&r1=880711&r2=880712&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeEntityConfig.java
(original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeEntityConfig.java
Mon Nov 16 11:25:48 2009
@@ -32,6 +32,7 @@
     private boolean strictParsing;
     private int maxLineLen;
     private int maxHeaderCount;
+    private int maxHeaderLen;
     private long maxContentLen;
     private boolean countLineNumbers;
     private String defaultContentType;
@@ -41,6 +42,7 @@
         this.strictParsing = false;
         this.maxLineLen = 1000;
         this.maxHeaderCount = 1000;
+        this.maxHeaderLen = 10000;
         this.maxContentLen = -1;
         this.countLineNumbers = false;
         this.defaultContentType = null;
@@ -149,6 +151,34 @@
         this.maxHeaderCount = maxHeaderCount;
     }
 
+    /**
+     * Returns the maximum header length limit
+     * @see #setMaxHeaderLen(int)
+     * 
+     * @return value of the maximum header length limit
+     */
+    public int getMaxHeaderLen() {
+        return maxHeaderLen;
+    }
+
+    /**
+     * Sets the maximum header length limit. Parsing of a MIME entity will be terminated

+     * with a {@link MimeException} if the total length of a header exceeds this limit.
+     * If this parameter is set to a non positive value the header length check will be
+     * disabled.
+     * <p>
+     * A message header may be folded across multiple lines. This configuration parameter
+     * is used to limit the total length of a header, i.e. the sum of the length of all
+     * lines the header spans across (including line terminators).
+     * <p>
+     * Default value: <code>10000</code>
+     * 
+     * @param maxHeaderLen maximum header length limit
+     */
+    public void setMaxHeaderLen(int maxHeaderLen) {
+        this.maxHeaderLen = maxHeaderLen;
+    }
+
     /** 
      * Returns the maximum content length limit
      * @see #setMaxContentLen(long)

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java?rev=880712&r1=880711&r2=880712&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeEntityTest.java
Mon Nov 16 11:25:48 2009
@@ -330,6 +330,9 @@
     }
 
     public void testMaxLineLimitCheck() throws Exception {
+        MimeEntityConfig config = new MimeEntityConfig();
+        config.setMaxLineLen(50);
+
         String message = 
             "To: Road Runner <runner@example.org>\r\n" +
             "From: Wile E. Cayote <wile@example.org>\r\n" +
@@ -342,10 +345,8 @@
         byte[] raw = message.getBytes("US-ASCII");
         ByteArrayInputStream instream = new ByteArrayInputStream(raw);
         LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12); 
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12, config.getMaxLineLen()); 
         
-        MimeEntityConfig config = new MimeEntityConfig();
-        config.setMaxLineLen(50);
         MimeEntity entity = new MimeEntity(
                 lineInput,
                 rawstream,
@@ -355,25 +356,23 @@
                 config);
         
         assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
-        entity.advance();
+        entity.advance(); // advances to T_START_HEADER
         assertEquals(EntityStates.T_START_HEADER, entity.getState());
-        entity.advance();
-        assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
+        entity.advance(); // reads To: into field buffer, From: into line buffer
         assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
+        entity.advance(); // reads Date: into line buffer
         assertEquals(EntityStates.T_FIELD, entity.getState());
-        entity.advance();
+        entity.advance(); // reads Subject: into line buffer
         assertEquals(EntityStates.T_FIELD, entity.getState());
         try {
-            entity.advance();
+            entity.advance(); // reads DoS: into line buffer
             fail("MimeException caused by MaxLineLimitException should have been thrown");
         } catch (MimeException expected) {
             assertTrue(expected.getCause() instanceof MaxLineLimitException);
         }
     }
     
-    public void testMaxLineLimitCheckFoldedLines() throws Exception {
+    public void testMaxHeaderLimitCheckFoldedLines() throws Exception {
         String message = 
             "To: Road Runner <runner@example.org>\r\n" +
             "From: Wile E. Cayote <wile@example.org>\r\n" +
@@ -400,7 +399,8 @@
         BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12); 
         
         MimeEntityConfig config = new MimeEntityConfig();
-        config.setMaxLineLen(50);
+        config.setMaxLineLen(100);
+        config.setMaxHeaderLen(200);
         MimeEntity entity = new MimeEntity(
                 lineInput,
                 rawstream,
@@ -428,6 +428,47 @@
         }
     }
 
+    public void testMaxHeaderLengthMayExceedMaxLineLength() throws Exception {
+        MimeEntityConfig config = new MimeEntityConfig();
+        config.setMaxLineLen(50);
+        config.setMaxHeaderLen(130);
+
+        String message = 
+            "To: Road Runner <runner@example.org>\r\n" +
+            "From: Wile E. Cayote <wile@example.org>\r\n" +
+            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
+            "Subject: Mail\r\n" +
+            "X-LongHeader: xxxxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "    xxxxxxxxxxxxxxxxxxxxxxx\r\n" +
+            "Content-Type: text/plain\r\n" +
+            "\r\n" +
+            "a very important message";
+        byte[] raw = message.getBytes("US-ASCII");
+        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12, config.getMaxLineLen()); 
+        
+        MimeEntity entity = new MimeEntity(
+                lineInput,
+                rawstream,
+                null,
+                EntityStates.T_START_MESSAGE,
+                EntityStates.T_END_MESSAGE,
+                config);
+        
+        assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
+        entity.advance();
+        assertEquals(EntityStates.T_START_HEADER, entity.getState());
+        for (int i = 0; i < 6; i++) {
+            entity.advance();
+            assertEquals(EntityStates.T_FIELD, entity.getState());
+        }
+        entity.advance();
+        assertEquals(EntityStates.T_END_HEADER, entity.getState());
+    }
+    
     public void testMaxHeaderCount() throws Exception {
         String message = 
             "To: Road Runner <runner@example.org>\r\n" +



Mime
View raw message