Skip to content

Commit d77c28f

Browse files
committed
add qhelp for unsafe-code-construction
1 parent d790f3c commit d77c28f

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>
8+
Dynamically constructing code with inputs from exported functions
9+
may inadvertently change the meaning of the code.
10+
11+
Clients using the functions may use characters that have special
12+
meaning, such as quotes and spaces.
13+
14+
This can result in the resulting code to misbehave, or in the worst case
15+
cause an attacker to execute arbitrary code on the system.
16+
</p>
17+
</overview>
18+
19+
<recommendation>
20+
<p>
21+
Avoid dynamically constructing code where possible.
22+
</p>
23+
</recommendation>
24+
25+
<example>
26+
<p>
27+
The following example shows two methods implemented using `eval`: a simple
28+
deserialization routine and a getter method.
29+
</p>
30+
31+
<sample src="examples/UnsafeCodeConstruction.js" />
32+
33+
<p>
34+
If untrusted inputs are used with these methods,
35+
then an attacker might be able to execute arbitrary code on the system.
36+
</p>
37+
<p>
38+
To avoid this problem, use an alternative solution such as `JSON.parse`
39+
or another library that does not allow arbitrary code to be executed.
40+
</p>
41+
42+
<sample src="examples/UnsafeCodeConstructionSafe.js" />
43+
44+
</example>
45+
46+
<references>
47+
<li>
48+
OWASP:
49+
<a href="https://www.owasp.org/index.php/Code_Injection">Code Injection</a>.
50+
</li>
51+
<li>
52+
Wikipedia: <a href="https://en.wikipedia.org/wiki/Code_injection">Code Injection</a>.
53+
</li>
54+
</references>
55+
</qhelp>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function unsafeDeserialize(value) {
2+
return eval(`(${value})`);
3+
}
4+
5+
export function unsafeGetter(obj, path) {
6+
return eval(`obj.${path}`);
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function safeDeserialize(value) {
2+
return JSON.parse(value);
3+
}
4+
5+
const _ = require("lodash");
6+
export function safeGetter(object, path) {
7+
return _.get(object, path);
8+
}

0 commit comments

Comments
 (0)