From d9a81374b2e6995a5118f76cf15cba67a67be66d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Tue, 5 Jan 2010 22:22:18 +0100 Subject: [PATCH] Throw exception for invalid multipart streams When using the multipart parser with an regular http request, the parser did not complain and just never finished. --- lib/multipart.js | 15 ++++++++++++--- test/mjsunit/test-multipart.js | 32 ++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/lib/multipart.js b/lib/multipart.js index 899b2d14997..3f74af9f623 100644 --- a/lib/multipart.js +++ b/lib/multipart.js @@ -43,11 +43,16 @@ proto.init = function(options) { if ('headers' in options) { var req = options, contentType = req.headers['content-type']; - if (contentType) { - contentType = contentType.split(/; ?boundary=/) - this.boundary = '--'+contentType[1]; + if (!contentType) { + throw new Error('Content-Type header not set'); } + var boundary = contentType.match(/^multipart\/form-data; ?boundary=(.+)$/i) + if (!boundary) { + throw new Error('Content-Type is not multipart: "'+contentType+'"'); + } + + this.boundary = '--'+boundary[1]; this.bytesTotal = req.headers['content-length']; var self = this; @@ -59,6 +64,10 @@ proto.init = function(options) { self.emit('complete'); }); } else { + if (!options.boundary) { + throw new Error('No boundary option given'); + } + this.boundary = options.boundary; this.write(options.data || ''); } diff --git a/test/mjsunit/test-multipart.js b/test/mjsunit/test-multipart.js index ea80232cb00..93ab7ab0481 100644 --- a/test/mjsunit/test-multipart.js +++ b/test/mjsunit/test-multipart.js @@ -7,10 +7,28 @@ var fixture = require('./fixtures/multipart'); var port = 8222; var parts_reveived = 0; var parts_complete = 0; +var requests = 0; +var badRequests = 0; var parts = {}; +var respond = function(res, text) { + requests++; + if (requests == 2) { + server.close(); + } + + res.sendHeader(200, {"Content-Type": "text/plain"}); + res.sendBody(text); + res.finish(); +}; var server = http.createServer(function(req, res) { - var stream = new multipart.Stream(req); + try { + var stream = new multipart.Stream(req); + } catch (e) { + badRequests++; + respond(res, 'no thanks'); + return; + } stream.addListener('part', function(part) { parts_reveived++; @@ -39,10 +57,7 @@ var server = http.createServer(function(req, res) { }); stream.addListener('complete', function() { - res.sendHeader(200, {"Content-Type": "text/plain"}); - res.sendBody('thanks'); - res.finish(); - server.close(); + respond(res, 'thanks'); }); }); server.listen(port); @@ -52,8 +67,13 @@ var request = client.request('POST', '/', {'Content-Type': 'multipart/form-data; request.sendBody(fixture.reply, 'binary'); request.finish(); +var badRequest = client.request('POST', '/', {'Content-Type': 'something', 'Content-Length': fixture.reply.length}); +badRequest.sendBody(fixture.reply, 'binary'); +badRequest.finish(); + process.addListener('exit', function() { puts("done"); assert.equal(2, parts_complete); assert.equal(2, parts_reveived); -}); + assert.equal(1, badRequests); +}); \ No newline at end of file