hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r705885 - in /httpcomponents/httpcore/trunk: ./ module-main/src/main/java/org/apache/http/impl/io/ module-main/src/test/java/org/apache/http/impl/io/ module-nio/src/main/java/org/apache/http/impl/nio/codecs/ module-nio/src/test/java/org/apa...
Date Sat, 18 Oct 2008 14:26:42 GMT
Author: olegk
Date: Sat Oct 18 07:26:42 2008
New Revision: 705885

URL: http://svn.apache.org/viewvc?rev=705885&view=rev
Log:
HTTPCORE-175: Chunk decoders no longer accept truncated chunks as valid input

Modified:
    httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
    httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java
    httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/io/TestChunkCoding.java
    httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
    httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java

Modified: httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt?rev=705885&r1=705884&r2=705885&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpcore/trunk/RELEASE_NOTES.txt Sat Oct 18 07:26:42 2008
@@ -1,3 +1,9 @@
+Changes since 4.0 Beta 3
+-------------------
+
+* [HTTPCORE-175] Chunk decoders no longer accept truncated chunks as valid input.
+  Contributed by Oleg Kalnichevski <olegk at apache.org> 
+
 Release 4.0 Beta 3
 -------------------
 

Modified: httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java?rev=705885&r1=705884&r2=705885&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java
(original)
+++ httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/impl/io/ChunkedInputStream.java
Sat Oct 18 07:26:42 2008
@@ -127,8 +127,11 @@
                 return -1;
             }
         }
-        pos++;
-        return in.read();
+        int b = in.read();
+        if (b != -1) {
+            pos++;
+        }
+        return b;
     }
 
     /**
@@ -158,9 +161,13 @@
             }
         }
         len = Math.min(len, chunkSize - pos);
-        int count = in.read(b, off, len);
-        pos += count;
-        return count;
+        int bytesRead = in.read(b, off, len);
+        if (bytesRead != -1) {
+            pos += bytesRead;
+            return bytesRead;
+        } else {
+            throw new MalformedChunkCodingException("Truncated chunk");
+        }
     }
 
     /**

Modified: httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/io/TestChunkCoding.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/io/TestChunkCoding.java?rev=705885&r1=705884&r2=705885&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/io/TestChunkCoding.java
(original)
+++ httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/impl/io/TestChunkCoding.java
Sat Oct 18 07:26:42 2008
@@ -277,6 +277,22 @@
         }
     }
 
+    // Truncated chunk
+    public void testCorruptChunkedInputStreamTruncatedChunk() throws IOException {
+        String s = "3\r\n12";
+        InputStream in = new ChunkedInputStream(
+                new SessionInputBufferMockup(
+                        EncodingUtils.getBytes(s, CONTENT_CHARSET)));
+        byte[] buffer = new byte[300];
+        assertEquals(2, in.read(buffer));
+        try {
+            in.read(buffer);
+            fail("MalformedChunkCodingException should have been thrown");
+        } catch(MalformedChunkCodingException e) {
+            /* expected exception */
+        }
+    }
+
     // Invalid footer
     public void testCorruptChunkedInputStreamInvalidFooter() throws IOException {
         String s = "1\r\n0\r\n0\r\nstuff\r\n";
@@ -306,7 +322,7 @@
         assertEquals(0, out.size());
     }
     
-    public void testChunkedConsitance() throws IOException {
+    public void testChunkedConsistence() throws IOException {
         String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb";
         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         OutputStream out = new ChunkedOutputStream(new SessionOutputBufferMockup(buffer));

Modified: httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java?rev=705885&r1=705884&r2=705885&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
(original)
+++ httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkDecoder.java
Sat Oct 18 07:26:42 2008
@@ -188,9 +188,11 @@
                         break;
                     }
                 }
-                
                 int maxLen = this.chunkSize - this.pos;
                 int len = this.buffer.read(dst, maxLen);
+                if (maxLen > 0 && len == 0 && this.endOfStream) {
+                    throw new MalformedChunkCodingException("Truncated chunk");
+                }
                 this.pos += len;
                 totalRead += len;
                 

Modified: httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java?rev=705885&r1=705884&r2=705885&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java
(original)
+++ httpcomponents/httpcore/trunk/module-nio/src/test/java/org/apache/http/impl/nio/codecs/TestChunkDecoder.java
Sat Oct 18 07:26:42 2008
@@ -265,6 +265,26 @@
         }
     }
 
+    public void testMalformedChunkTruncatedChunk() throws Exception {
+        String s = "3\r\n12";
+        ReadableByteChannel channel = new ReadableByteChannelMockup(
+                new String[] {s}, "US-ASCII"); 
+        HttpParams params = new BasicHttpParams();
+        
+        SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, params); 
+        HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
+        ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
+        
+        ByteBuffer dst = ByteBuffer.allocate(1024); 
+        assertEquals(2, decoder.read(dst));
+        try {
+            decoder.read(dst);
+            fail("MalformedChunkCodingException should have been thrown");
+        } catch (MalformedChunkCodingException ex) {
+            // expected
+        }
+    }
+
     public void testFoldedFooters() throws Exception {
         String s = "10;key=\"value\"\r\n1234567890123456\r\n" +
                 "5\r\n12345\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\n   \r\n  fghij\r\n\r\n";



Mime
View raw message