Skip to content

Commit ace05e0

Browse files
authored
fix: switch to using crypto random for boundary values (#28)
form-data/form-data@3d17230 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Updated continuous integration workflow to include additional Node.js version and new event triggers. * Removed contributor management scripts and dependencies from project configuration. * **Documentation** * Simplified the contributors section in the documentation with a dynamic badge and removed the static contributors table. * **Refactor** * Improved multipart form boundary generation for enhanced security and simplicity. * **Tests** * Updated test assertions to support a broader range of boundary formats in multipart form-data headers. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 38b2623 commit ace05e0

File tree

5 files changed

+13
-26
lines changed

5 files changed

+13
-26
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ on:
55
branches: [ master ]
66
pull_request:
77
branches: [ master ]
8+
merge_group:
89

910
jobs:
1011
Job:
1112
name: Node.js
1213
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1314
with:
1415
os: 'ubuntu-latest, macos-latest'
15-
version: '14, 16, 18, 20, 22'
16+
version: '14, 16, 18, 20, 22, 24'
1617
secrets:
1718
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,8 @@ See [Node.js Documentation](http://nodejs.org/api/stream.html#stream_event_end)
204204

205205
[MIT](LICENSE)
206206

207-
<!-- GITCONTRIBUTOR_START -->
208-
209207
## Contributors
210208

211-
|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/288288?v=4" width="100px;"/><br/><sub><b>xingrz</b></sub>](https://github.com/xingrz)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/13151189?v=4" width="100px;"/><br/><sub><b>fjc0k</b></sub>](https://github.com/fjc0k)<br/>|[<img src="https://avatars.githubusercontent.com/u/18096247?v=4" width="100px;"/><br/><sub><b>mrspeiser</b></sub>](https://github.com/mrspeiser)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|
212-
| :---: | :---: | :---: | :---: | :---: | :---: |
213-
[<img src="https://avatars.githubusercontent.com/u/7326406?v=4" width="100px;"/><br/><sub><b>shaozj</b></sub>](https://github.com/shaozj)<br/>
214-
215-
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Wed May 15 2024 00:34:12 GMT+0800`.
209+
[![Contributors](https://contrib.rocks/image?repo=node-modules/formstream)](https://github.com/node-modules/formstream/graphs/contributors)
216210

217-
<!-- GITCONTRIBUTOR_END -->
211+
Made with [contributors-img](https://contrib.rocks).

lib/formstream.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Content-Type: image/png\r\n
3535

3636
var debug = require('util').debuglog('formstream');
3737
var Stream = require('stream');
38+
var crypto = require('crypto');
3839
var parseStream = require('pause-stream');
3940
var util = require('util');
4041
var mime = require('mime');
@@ -71,15 +72,8 @@ util.inherits(FormStream, Stream);
7172
module.exports = FormStream;
7273

7374
FormStream.prototype._generateBoundary = function() {
74-
// https://github.com/felixge/node-form-data/blob/master/lib/form_data.js#L162
75-
// This generates a 50 character boundary similar to those used by Firefox.
76-
// They are optimized for boyer-moore parsing.
77-
var boundary = '--------------------------';
78-
for (var i = 0; i < 24; i++) {
79-
boundary += Math.floor(Math.random() * 10).toString(16);
80-
}
81-
82-
return boundary;
75+
// https://github.com/form-data/form-data/blob/16e00765342106876f98a1c9703314006c9e937a/lib/form_data.js#L345
76+
return '--------------------------' + crypto.randomBytes(12).toString('hex');
8377
};
8478

8579
FormStream.prototype.setTotalStreamSize = function (size) {

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"cov": "egg-bin cov",
1414
"ci": "npm run lint && npm run tsd && npm run cov && NODE_DEBUG=formstream npm run cov",
1515
"lint": "jshint .",
16-
"tsd": "tsd",
17-
"contributor": "git-contributor"
16+
"tsd": "tsd"
1817
},
1918
"repository": {
2019
"type": "git",
@@ -40,7 +39,6 @@
4039
"connect-multiparty": "1",
4140
"egg-bin": "^5.6.1",
4241
"express": "^4.16.4",
43-
"git-contributor": "^2.1.5",
4442
"jshint": "^2.13.6",
4543
"pedding": "1",
4644
"should": "4",

test/formstream.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ describe('formstream.test.js', function () {
246246
pwd: '哈哈pwd'
247247
});
248248
data.headers.should.have.property('content-type')
249-
.with.match(/multipart\/form-data; boundary=--------------------------\d{24}/);
249+
.with.match(/multipart\/form-data; boundary=--------------------------\w{24}/);
250250
var files = data.files;
251251
files.should.have.keys('stream1', 'stream2', 'file');
252252
var stream1 = files.stream1;
@@ -288,7 +288,7 @@ describe('formstream.test.js', function () {
288288
pwd: '哈哈pwd'
289289
});
290290
data.headers.should.have.property('content-type')
291-
.with.match(/multipart\/form-data; boundary=--------------------------\d{24}/);
291+
.with.match(/multipart\/form-data; boundary=--------------------------\w{24}/);
292292
var files = data.files;
293293
files.should.have.keys('stream1', 'stream2', 'file');
294294
var stream1 = files.stream1;
@@ -323,7 +323,7 @@ describe('formstream.test.js', function () {
323323
pwd: '哈哈pwd'
324324
});
325325
data.headers.should.have.property('content-type')
326-
.with.match(/^multipart\/form-data; boundary=--------------------------\d{24}$/);
326+
.with.match(/^multipart\/form-data; boundary=--------------------------\w{24}$/);
327327
var files = data.files;
328328
files.should.have.keys('file', 'logo');
329329
files.file.filename.should.equal('formstream.test.js');
@@ -351,7 +351,7 @@ describe('formstream.test.js', function () {
351351
});
352352
data.headers.should.have.property('content-length').with.equal(form._contentLength + '');
353353
data.headers.should.have.property('content-type')
354-
.with.match(/^multipart\/form-data; boundary=--------------------------\d{24}$/);
354+
.with.match(/^multipart\/form-data; boundary=--------------------------\w{24}$/);
355355
var files = data.files;
356356
files.should.have.keys('file', 'logo');
357357
files.file.filename.should.equal('formstream.test.js');
@@ -448,7 +448,7 @@ describe('formstream.test.js', function () {
448448
form.field('foo', 'bar');
449449
var headers = form.headers({ 'X-Test': 'hello' });
450450
headers.should.have.keys('Content-Type', 'Content-Length', 'X-Test');
451-
headers['Content-Type'].should.match(/^multipart\/form-data; boundary=--------------------------\d{24}$/);
451+
headers['Content-Type'].should.match(/^multipart\/form-data; boundary=--------------------------\w{24}$/);
452452
headers['X-Test'].should.equal('hello');
453453
headers['Content-Length'].should.equal('161');
454454
});

0 commit comments

Comments
 (0)