Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d64df30

Browse files
committed
reintroduce the reverted qhelp
1 parent ebf9ba7 commit d64df30

5 files changed

Lines changed: 106 additions & 2 deletions

File tree

javascript/ql/src/Security/CWE-770/ResourceExhaustion.qhelp

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,60 @@
3333

3434
<p>
3535

36-
The following example lets a user choose a delay after
37-
which a function is executed:
36+
<p>
37+
38+
The following example allocates a buffer with a user-controlled
39+
size.
40+
41+
</p>
42+
43+
<sample src="examples/ResourceExhaustion_buffer.js" />
44+
45+
<p>
46+
47+
This is problematic since an attacker can choose a size
48+
that makes the application run out of memory. Even worse, in older
49+
versions of Node.js, this could leak confidential memory.
50+
51+
To prevent such attacks, limit the buffer size:
52+
53+
</p>
54+
55+
<sample src="examples/ResourceExhaustion_buffer_fixed.js" />
56+
57+
</example>
58+
59+
<example>
60+
61+
<p>
62+
63+
As another example, consider an application that allocates an
64+
array with a user-controlled size, and then fills it with values:
65+
66+
</p>
67+
68+
<sample src="examples/ResourceExhaustion_array.js" />
69+
70+
<p>
71+
The allocation of the array itself is not problematic since arrays are
72+
allocated sparsely, but the subsequent filling of the array will take
73+
a long time, causing the application to be unresponsive, or even run
74+
out of memory.
75+
76+
Again, a limit on the size will prevent the attack:
77+
78+
</p>
79+
80+
<sample src="examples/ResourceExhaustion_array_fixed.js" />
81+
82+
</example>
83+
84+
<example>
85+
86+
<p>
87+
88+
Finally, the following example lets a user choose a delay after
89+
which a function is executed:
3890

3991
</p>
4092

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var http = require("http"),
2+
url = require("url");
3+
4+
var server = http.createServer(function(req, res) {
5+
var size = parseInt(url.parse(req.url, true).query.size);
6+
7+
let dogs = new Array(size).fill(x => "dog"); // BAD
8+
9+
// ... use the dog
10+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var http = require("http"),
2+
url = require("url");
3+
4+
var server = http.createServer(function(req, res) {
5+
var size = parseInt(url.parse(req.url, true).query.size);
6+
7+
if (size > 1024) {
8+
res.statusCode = 400;
9+
res.end("Bad request.");
10+
return;
11+
}
12+
13+
let dogs = new Array(size).fill(x => "dog"); // GOOD
14+
15+
// ... use the dogs
16+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var http = require("http"),
2+
url = require("url");
3+
4+
var server = http.createServer(function(req, res) {
5+
var size = parseInt(url.parse(req.url, true).query.size);
6+
7+
let buffer = Buffer.alloc(size); // BAD
8+
9+
// ... use the buffer
10+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var http = require("http"),
2+
url = require("url");
3+
4+
var server = http.createServer(function(req, res) {
5+
var size = parseInt(url.parse(req.url, true).query.size);
6+
7+
if (size > 1024) {
8+
res.statusCode = 400;
9+
res.end("Bad request.");
10+
return;
11+
}
12+
13+
let buffer = Buffer.alloc(size); // GOOD
14+
15+
// ... use the buffer
16+
});

0 commit comments

Comments
 (0)