diff --git a/src/plugins/json.js b/src/plugins/json.js index 7155ad74..ca1e6f24 100644 --- a/src/plugins/json.js +++ b/src/plugins/json.js @@ -11,7 +11,7 @@ export default function plugin(formidable, options) { /* istanbul ignore next */ const self = this || formidable; - if (/json/i.test(self.headers['content-type'])) { + if (/^[^;]*json/i.test(self.headers['content-type'])) { init.call(self, self, options); } diff --git a/src/plugins/multipart.js b/src/plugins/multipart.js index eebd96a7..b87f8235 100644 --- a/src/plugins/multipart.js +++ b/src/plugins/multipart.js @@ -15,7 +15,7 @@ export default function plugin(formidable, options) { const self = this || formidable; // NOTE: we (currently) support both multipart/form-data and multipart/related - const multipart = /multipart/i.test(self.headers['content-type']); + const multipart = /^[^;]*multipart/i.test(self.headers['content-type']); if (multipart) { const m = self.headers['content-type'].match( diff --git a/src/plugins/octetstream.js b/src/plugins/octetstream.js index 0bc5c67e..486e332a 100644 --- a/src/plugins/octetstream.js +++ b/src/plugins/octetstream.js @@ -11,7 +11,7 @@ export default async function plugin(formidable, options) { /* istanbul ignore next */ const self = this || formidable; - if (/octet-stream/i.test(self.headers['content-type'])) { + if (/^[^;]*octet-stream/i.test(self.headers['content-type'])) { await init.call(self, self, options); } return self; diff --git a/src/plugins/querystring.js b/src/plugins/querystring.js index ef435aa7..73113bce 100644 --- a/src/plugins/querystring.js +++ b/src/plugins/querystring.js @@ -12,7 +12,7 @@ export default function plugin(formidable, options) { /* istanbul ignore next */ const self = this || formidable; - if (/urlencoded/i.test(self.headers['content-type'])) { + if (/^[^;]*urlencoded/i.test(self.headers['content-type'])) { init.call(self, self, options); } return self; diff --git a/test/fixture/http/misc/boundary-substring-json.http b/test/fixture/http/misc/boundary-substring-json.http new file mode 100644 index 00000000..09e0601b --- /dev/null +++ b/test/fixture/http/misc/boundary-substring-json.http @@ -0,0 +1,13 @@ +POST /upload HTTP/1.1 +Host: localhost:8080 +Content-Type: multipart/form-data; boundary=uj05Dyqd7Fd5aqAJnK1j9WeJSONmNy5vSGbM1oLf +Content-Length: 211 + +--uj05Dyqd7Fd5aqAJnK1j9WeJSONmNy5vSGbM1oLf +Content-Disposition: form-data; filename="plain.txt"; name="upload" +Content-Type: text/plain + +I am a plain text file + +--uj05Dyqd7Fd5aqAJnK1j9WeJSONmNy5vSGbM1oLf-- + diff --git a/test/fixture/js/misc.js b/test/fixture/js/misc.js index 80e36eab..31cf174e 100644 --- a/test/fixture/js/misc.js +++ b/test/fixture/js/misc.js @@ -1,3 +1,12 @@ +const boundary_substring_json = [ + { + type: 'file', + name: 'upload', + originalFilename: 'plain.txt', + fixture: 'boundary-substring-json', + }, +]; + const empty_http = []; const empty_urlencoded_http = []; const empty_multipart_http = []; @@ -5,6 +14,7 @@ const empty_multipart2_http = []; const _minimal_http = []; export { + boundary_substring_json, empty_http, empty_urlencoded_http, empty_multipart_http, diff --git a/test/integration/fixtures.test.js b/test/integration/fixtures.test.js index a37840c0..9269ab90 100644 --- a/test/integration/fixtures.test.js +++ b/test/integration/fixtures.test.js @@ -58,30 +58,36 @@ test('fixtures', (done) => { const fixture = fixtureWithName.fixture; uploadFixture(fixtureName, (err, parts) => { - if (err) { - err.fixtureName = fixtureName; - throw err; - } + try { + if (err) { + err.fixtureName = fixtureName; + throw err; + } - fixture.forEach((expectedPart, i) => { - const parsedPart = parts[i]; - strictEqual(parsedPart.type, expectedPart.type); - strictEqual(parsedPart.name, expectedPart.name); - - if (parsedPart.type === 'file') { - const file = parsedPart.value; - strictEqual(file.originalFilename, expectedPart.originalFilename, - `${JSON.stringify([expectedPart, file])}`); - - if (expectedPart.sha1) { - strictEqual( - file.hash, - expectedPart.sha1, - `SHA1 error ${file.originalFilename} on ${file.filepath} ${JSON.stringify([expectedPart, file])}`, - ); + fixture.forEach((expectedPart, i) => { + const parsedPart = parts[i]; + strictEqual(parsedPart.type, expectedPart.type); + strictEqual(parsedPart.name, expectedPart.name); + + if (parsedPart.type === 'file') { + const file = parsedPart.value; + strictEqual(file.originalFilename, expectedPart.originalFilename, + `${JSON.stringify([expectedPart, file])}`); + + if (expectedPart.sha1) { + strictEqual( + file.hash, + expectedPart.sha1, + `SHA1 error ${file.originalFilename} on ${file.filepath} ${JSON.stringify([expectedPart, file])}`, + ); + } } - } - }); + }); + } catch (e) { + server.close(); + done(e); + throw e; + } testNext(results); }); @@ -93,7 +99,6 @@ test('fixtures', (done) => { uploadDir: UPLOAD_DIR, hashAlgorithm: 'sha1', }); - form.parse(req); function callback(...args) { const realCallback = cb; @@ -116,6 +121,8 @@ test('fixtures', (done) => { res.end(); callback(null, parts); }); + + form.parse(req); }); const socket = createConnection(PORT);