Skip to content

Commit a5cbf4a

Browse files
committed
update bodyParser post to take into account updates to express
1 parent 3fb6bda commit a5cbf4a

1 file changed

Lines changed: 56 additions & 27 deletions

File tree

posts/do-not-use-bodyparser-with-express-js.html

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
<h1>Do Not Use bodyParser with Express.js</h1>
22
<p>
3+
Note: this post has
4+
<a href="https://github.com/superjoe30/andrewkelley.me/commits/master">been edited</a>
5+
to take into account
6+
<a href="https://github.com/visionmedia/">TJ</a>'s
7+
<a href="https://groups.google.com/forum/#!topic/express-js/iP2VyhkypHo">diligent work</a>
8+
in response to this.
9+
</p>
10+
<p>
311
I came across
412
<a href="https://plus.google.com/106706438172517329683/posts/4kQiD8L1D36">this Google+ post</a>
513
mentioning
@@ -9,15 +17,11 @@ <h1>Do Not Use bodyParser with Express.js</h1>
917
enough to use for production applications.
1018
</p>
1119
<p>
12-
This reminds me of one vulnerability in particular that I've been trying to get solved
13-
but <a href="https://github.com/visionmedia/">the maintainer</a> of express is too busy
14-
to care. Rightfully so. I mean if you look at how much stuff this guy has to maintain it's
15-
amazing that he's able to get around to as many issues as he does.
16-
I just wish he'd recruit some trusted additional maintainers to keep the issue counts
17-
from getting out of hand.
20+
This reminds me of one "gotcha" in particular that you could be bitten by if
21+
you're not careful.
1822
</p>
1923
<p>
20-
Anyway, all servers using
24+
All servers using
2125
<a href="http://expressjs.com/api.html#bodyParser">express.bodyParser</a>
2226
are vulnerable to an attack which creates an unlimited number of temp files
2327
on the server, potentially filling up all the disk space, which is likely
@@ -85,8 +89,12 @@ <h3>Always delete the temp files when you use bodyParser or multipart middleware
8589
every multipart upload that comes its way, creating a temp file, writing it to disk,
8690
and then deleting the temp file. Why do all that when you don't want to accept uploads?
8791
</li>
92+
<li>
93+
As of express 3.4.0 (connect 2.9.0) bodyParser is deprecated.
94+
It goes without saying that deprecated things should be avoided.
95+
</li>
8896
</ol>
89-
<h3>Use a utility such as tmpwatch</h3>
97+
<h3>Use a utility such as tmpwatch or reap</h3>
9098
<p>
9199
<a href="https://github.com/jfromaniello">jfromaniello</a>
92100
<a href="https://groups.google.com/d/msg/nodejs/6KOlfk5cpcM/SCJ9jZZfP-UJ">pointed out</a>
@@ -108,37 +116,58 @@ <h3>Use a utility such as tmpwatch</h3>
108116
of free space, an attacker would need an Internet connection with
109117
145 KB/s upload bandwidth to crash your server.
110118
</p>
119+
<p>
120+
TJ pointed out that he also has a utility for this purpose called
121+
<a href="https://github.com/visionmedia/reap">reap</a>.
122+
</p>
111123
<h3>Avoid bodyParser and explicitly use the middleware that you need</h3>
112124
<p>
113125
If you want to parse json in your endpoint, use <code>express.json()</code> middleware.
114126
If you want json and urlencoded endpoint, use <code>[express.json(), express.urlencoded()]</code>
115127
for your middleware.
116-
If you want users to upload files to your endpoint, use <code>express.multipart()</code> and be
128+
</p>
129+
<p>
130+
If you want users to upload files to your endpoint, you could use <code>express.multipart()</code> and be
117131
sure to clean up all the temp files that are created.
132+
This would still stuffer from problem #3 previously mentioned.
133+
</p>
134+
<h3>Use the defer option in the multipart middleware</h3>
135+
<p>
136+
When you create your multipart middleware, you can use the <code>defer</code>
137+
option like this:
138+
</p>
139+
<pre>
140+
<code class="language-javascript">express.multipart({defer: true})</code>
141+
</pre>
142+
<p>
143+
According to the documentation:
118144
</p>
145+
<blockquote>
146+
defers processing and exposes the multiparty form object as `req.form`.<br>
147+
`next()` is called without waiting for the form's "end" event.<br>
148+
This option is useful if you need to bind to the "progress" or "part" events, for example.<br>
149+
</blockquote>
119150
<p>
120-
This still suffers from problem #3 previously mentioned.
151+
So if you do this you will use <a href="https://github.com/superjoe30/node-multiparty/blob/master/README.md#api">multiparty's API</a> assuming that <code>req.form</code>
152+
is an instantiated <code>Form</code> instance.
121153
</p>
122-
<h3>Consider using alternatives to formidable</h3>
154+
<h3>Use an upload parsing module directly</h3>
123155
<p>
124-
<code>bodyParser</code> depends on <code>multipart</code>, which depends on
125-
<a href="https://github.com/felixge/node-formidable">formidable</a>, which
126-
is hardcoded to send uploads to a temp directory.
156+
<code>bodyParser</code> depends on <code>multipart</code>, which behind the
157+
scenes uses
158+
<a href="https://github.com/superjoe30/node-multiparty">multiparty</a> to
159+
parse uploads.
127160
</p>
128161
<p>
129-
Consider using an alternative, such as
130-
<a href="https://github.com/superjoe30/node-multiparty">multiparty</a>.
131-
In addition to solving some bugs, it gives you much more flexibility over your file
132-
upload, most notably not creating temp files unless you want it to.
133-
(It also is flexible enough to for example
134-
<a href="https://github.com/superjoe30/node-multiparty/blob/master/examples/s3.js">stream an upload directly to S3</a>).
135-
There's also a
136-
<a href="https://github.com/superjoe30/connect-multiparty">drop-in replacement</a>
137-
for <code>express.multipart()</code> using multiparty.
162+
You can use this module directly to handle the request. In this case you can
163+
look at
164+
<a href="https://github.com/superjoe30/node-multiparty/blob/master/README.md#api">multiparty's API</a>
165+
and do the right thing.
138166
</p>
139167
<p>
140-
<a href="https://github.com/mscdex/">mscdex</a>
141-
<a href="https://groups.google.com/d/msg/nodejs/6KOlfk5cpcM/KJURooRp5dsJ">mentioned</a>
142-
that he created an alternative named
143-
<a href="https://github.com/mscdex/busboy">busboy</a>.
168+
There are also alternatives such as
169+
<a href="https://github.com/mscdex/busboy">busyboy</a>,
170+
<a href="https://github.com/chjj/parted">parted</a>,
171+
and
172+
<a href="https://github.com/felixge/node-formidable">formidable</a>.
144173
</p>

0 commit comments

Comments
 (0)