|
1 | 1 | /* eslint-env mocha */ |
2 | 2 |
|
3 | 3 | var assert = require('assert') |
| 4 | +var http = require('http') |
4 | 5 |
|
5 | 6 | var multer = require('../') |
6 | 7 | var util = require('./_util') |
@@ -105,4 +106,94 @@ describe('Express Integration', function () { |
105 | 106 | done() |
106 | 107 | }) |
107 | 108 | }) |
| 109 | + |
| 110 | + it('should not crash on malformed request', function (done) { |
| 111 | + var upload = multer() |
| 112 | + |
| 113 | + app.post('/upload', upload.single('file'), function (req, res) { |
| 114 | + res.status(500).end('Request should not be processed') |
| 115 | + }) |
| 116 | + |
| 117 | + app.use(function (err, req, res, next) { |
| 118 | + assert.strictEqual(err.message, 'Unexpected end of form') |
| 119 | + res.status(200).end('Correct error') |
| 120 | + }) |
| 121 | + |
| 122 | + var boundary = 'AaB03x' |
| 123 | + var body = [ |
| 124 | + '--' + boundary, |
| 125 | + 'Content-Disposition: form-data; name="file"; filename="test.txt"', |
| 126 | + 'Content-Type: text/plain', |
| 127 | + '', |
| 128 | + 'test without end boundary' |
| 129 | + ].join('\r\n') |
| 130 | + var options = { |
| 131 | + hostname: 'localhost', |
| 132 | + port, |
| 133 | + path: '/upload', |
| 134 | + method: 'POST', |
| 135 | + headers: { |
| 136 | + 'content-type': 'multipart/form-data; boundary=' + boundary, |
| 137 | + 'content-length': body.length |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + var req = http.request(options, (res) => { |
| 142 | + assert.strictEqual(res.statusCode, 200) |
| 143 | + done() |
| 144 | + }) |
| 145 | + |
| 146 | + req.on('error', (err) => { |
| 147 | + done(err) |
| 148 | + }) |
| 149 | + |
| 150 | + req.write(body) |
| 151 | + req.end() |
| 152 | + }) |
| 153 | + |
| 154 | + it('should not crash on malformed request that causes two errors to be emitted by busboy', function (done) { |
| 155 | + var upload = multer() |
| 156 | + |
| 157 | + app.post('/upload2', upload.single('file'), function (req, res) { |
| 158 | + res.status(500).end('Request should not be processed') |
| 159 | + }) |
| 160 | + |
| 161 | + app.use(function (err, req, res, next) { |
| 162 | + assert.strictEqual(err.message, 'Malformed part header') |
| 163 | + res.status(200).end('Correct error') |
| 164 | + }) |
| 165 | + |
| 166 | + var boundary = 'AaB03x' |
| 167 | + // this payload causes two errors to be emitted by busboy: `Malformed part header` and `Unexpected end of form` |
| 168 | + var body = [ |
| 169 | + '--' + boundary, |
| 170 | + 'Content-Disposition: form-data; name="file"; filename="test.txt"', |
| 171 | + 'Content-Type: text/plain', |
| 172 | + '', |
| 173 | + '--' + boundary + '--', |
| 174 | + '' |
| 175 | + ].join('\r\n') |
| 176 | + var options = { |
| 177 | + hostname: 'localhost', |
| 178 | + port, |
| 179 | + path: '/upload2', |
| 180 | + method: 'POST', |
| 181 | + headers: { |
| 182 | + 'content-type': 'multipart/form-data; boundary=' + boundary, |
| 183 | + 'content-length': body.length |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + var req = http.request(options, (res) => { |
| 188 | + assert.strictEqual(res.statusCode, 200) |
| 189 | + done() |
| 190 | + }) |
| 191 | + |
| 192 | + req.on('error', (err) => { |
| 193 | + done(err) |
| 194 | + }) |
| 195 | + |
| 196 | + req.write(body) |
| 197 | + req.end() |
| 198 | + }) |
108 | 199 | }) |
0 commit comments