Skip to content
This repository
  • 3 commits
  • 4 files changed
  • 0 comments
  • 1 contributor
Jun 22, 2010
Felix Geisendörfer Fix: Trailing boundary-like data caused parse error
When we discard a started boundary at the end of a part's data block,
we need to re-consider the current character as it could be the
beginning of a new boundary sequence itself.

See: http://github.com/felixge/node-formidable/issues#issue/2
7429179
Felix Geisendörfer Handle empty header fields correctly
Discovered by: Hongli Lai
30e715a
Felix Geisendörfer Bump version a12f8a9
10  lib/formidable/multipart_parser.js
@@ -143,6 +143,7 @@ MultipartParser.prototype.write = function(buffer) {
143 143
       case S.HEADER_FIELD_START:
144 144
         state = S.HEADER_FIELD;
145 145
         mark('headerField');
  146
+        index = 0;
146 147
       case S.HEADER_FIELD:
147 148
         if (c == CR) {
148 149
           clear('headerField');
@@ -150,11 +151,16 @@ MultipartParser.prototype.write = function(buffer) {
150 151
           break;
151 152
         }
152 153
 
  154
+        index++;
153 155
         if (c == HYPHEN) {
154 156
           break;
155 157
         }
156 158
 
157 159
         if (c == COLON) {
  160
+          if (index == 1) {
  161
+            // empty header field
  162
+            return i;
  163
+          }
158 164
           dataCallback('headerField', true);
159 165
           state = S.HEADER_VALUE_START;
160 166
           break;
@@ -275,6 +281,10 @@ MultipartParser.prototype.write = function(buffer) {
275 281
           callback('partData', lookbehind, 0, prevIndex);
276 282
           prevIndex = 0;
277 283
           mark('partData');
  284
+
  285
+          // reconsider the current character even so it interrupted the sequence
  286
+          // it could be the beginning of a new sequence
  287
+          i--;
278 288
         }
279 289
         
280 290
         break;
2  package.json
... ...
@@ -1,5 +1,5 @@
1 1
 { "name" : "formidable"
2  
-, "version": "0.9.1"
  2
+, "version": "0.9.2"
3 3
 , "dependencies": {"gently": ">=0.7.0"}
4 4
 , "directories" : { "lib" : "./lib/formidable" }
5 5
 , "main" : "./lib/formidable/index"
21  test/fixture/multipart.js
@@ -12,7 +12,7 @@ exports['rfc1867'] =
12 12
       'content-disposition: form-data; name="pics"; filename="file1.txt"\r\n'+
13 13
       'Content-Type: text/plain\r\n'+
14 14
       '\r\n'+
15  
-      '... contents of file1.txt ...\r\n'+
  15
+      '... contents of file1.txt ...\r\r\n'+
16 16
       '--AaB03x--\r\n'
17 17
   , parts:
18 18
     [ { headers:
@@ -24,7 +24,24 @@ exports['rfc1867'] =
24 24
         { 'content-disposition': 'form-data; name="pics"; filename="file1.txt"'
25 25
         , 'Content-Type': 'text/plain'
26 26
         }
27  
-      , data: '... contents of file1.txt ...'
  27
+      , data: '... contents of file1.txt ...\r'
28 28
       }
29 29
     ]
  30
+  };
  31
+
  32
+exports['emptyHeader'] =
  33
+  { boundary: 'AaB03x'
  34
+  , raw:
  35
+      '--AaB03x\r\n'+
  36
+      'content-disposition: form-data; name="field1"\r\n'+
  37
+      ': foo\r\n'+
  38
+      '\r\n'+
  39
+      'Joe Blow\r\nalmost tricked you!\r\n'+
  40
+      '--AaB03x\r\n'+
  41
+      'content-disposition: form-data; name="pics"; filename="file1.txt"\r\n'+
  42
+      'Content-Type: text/plain\r\n'+
  43
+      '\r\n'+
  44
+      '... contents of file1.txt ...\r\r\n'+
  45
+      '--AaB03x--\r\n'
  46
+  , expectError: true
30 47
   };
7  test/integration/test-multipart-parser.js
@@ -68,12 +68,19 @@ Object.keys(fixtures).forEach(function(name) {
68 68
 
69 69
     nparsed = parser.write(chunk);
70 70
     if (nparsed != chunk.length) {
  71
+      if (fixture.expectError) {
  72
+        return;
  73
+      }
71 74
       puts('-- ERROR --');
72 75
       p(chunk.toString('ascii'));
73 76
       throw new Error(chunk.length+' bytes written, but only '+nparsed+' bytes parsed!');
74 77
     }
75 78
   }
76 79
 
  80
+  if (fixture.expectError) {
  81
+    throw new Error('expected parse error did not happen');
  82
+  }
  83
+
77 84
   assert.ok(endCalled);
78 85
   assert.deepEqual(parts, fixture.parts);
79 86
 });

No commit comments for this range

Something went wrong with that request. Please try again.