FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Added content for 'Limit request body size' of security best practices · javascriptit/nodebestpractices@d1fec3c · GitHub

Repository navigation

Commit d1fec3c

Browse files
Bruno Scheufler
committed
Added content for 'Limit request body size' of security best practices
1 parent cc90ac6 commit d1fec3c

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,12 +798,12 @@ Developers in the project may not follow consistent code security practices, lea
798798

799799
## ![✔] 6.14. Limit payload size using a reverse-proxy or a middleware
800800

801-
**TL;DR:** Here we will talk about the easiness of exhausting the system using large request payloads
801+
**TL;DR:** DOS attacks of many varieties can be mitigated by limiting the body size of incoming requests to your web application.
802802

803-
**Otherwise:**
803+
**Otherwise:** requests with no restriction towards their payload/body size may simply cause a denial-of-service in various parts of your application, leading to problems and other unwanted and risky side-effects.
804804

805805

806-
🔗 [**Read More:**](#)
806+
🔗 [**Read More:**](/sections/security/requestpayloadsizelimit.md)
807807

808808
<br/><br/>
809809

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Limit payload size using a reverse-proxy or a middleware
2+
3+
### One Paragraph Explainer
4+
5+
When handling incoming requests in your web application, you should limit the size of the request body. Incoming requests with
6+
unlimited body sizes can lead to your application crashing due to a denial-of-service outage or other unwanted side-effects.
7+
Many popular middleware-solutions for parsing request bodies, such as already-included `body-parser` package for express, expose
8+
options to limit the sizes of request payloads, making it easy for developers to implement this functionality. You can also
9+
integrate a request body size limit in your reverse-proxy/web server software if supported. Below are examples for limiting request sizes using
10+
`express` and/or `nginx`.
11+
12+
### Example code for `express`
13+
14+
```javascript
15+
const express = require('express');
16+
17+
const app = express();
18+
19+
const parseJsonBody = express.json({ limit: '300kb' }); // body-parser defaults to a body size limit of 100kb
20+
const handleErrors = (err, req, res, next) => res.sendStatus(400); // simple express middleware to handle body parsing error
21+
22+
// Request with json body
23+
app.post('/json', parseJsonBody, handleErrors, (req, res) => {
24+
25+
// Check if request payload content-type matches json, because body-parser does not check for content types
26+
if (!req.is('json')) {
27+
return res.sendStatus(415); // -> Unsupported media type if request doesn't have JSON body
28+
}
29+
30+
res.send('Hooray, it worked!');
31+
});
32+
33+
app.listen(3000, () => console.log('Example app listening on port 3000!'));
34+
```
35+
36+
🔗 [**Express docs for express.json()**](http://expressjs.com/en/4x/api.html#express.json)
37+
38+
### Example configuration for `nginx`
39+
40+
```
41+
http {
42+
...
43+
# Limit the body size for ALL incoming requests to 1 MB
44+
client_max_body_size 1m;
45+
}
46+
47+
server {
48+
...
49+
# Limit the body size for incoming requests to this specific server block to 1 MB
50+
client_max_body_size 1m;
51+
}
52+
53+
location /upload {
54+
...
55+
# Limit the body size for incoming requests to this route to 1 MB
56+
client_max_body_size 200M;
57+
}
58+
```
59+
60+
🔗 [**Nginx docs for client_max_body_size**](http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL