add qhelp for unsafe-code-construction

This commit is contained in:
Erik Krogh Kristensen
2021-05-05 19:34:53 +02:00
parent d790f3ccbb
commit d77c28f6a7
3 changed files with 70 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
Dynamically constructing code with inputs from exported functions
may inadvertently change the meaning of the code.
Clients using the functions may use characters that have special
meaning, such as quotes and spaces.
This can result in the resulting code to misbehave, or in the worst case
cause an attacker to execute arbitrary code on the system.
</p>
</overview>
<recommendation>
<p>
Avoid dynamically constructing code where possible.
</p>
</recommendation>
<example>
<p>
The following example shows two methods implemented using `eval`: a simple
deserialization routine and a getter method.
</p>
<sample src="examples/UnsafeCodeConstruction.js" />
<p>
If untrusted inputs are used with these methods,
then an attacker might be able to execute arbitrary code on the system.
</p>
<p>
To avoid this problem, use an alternative solution such as `JSON.parse`
or another library that does not allow arbitrary code to be executed.
</p>
<sample src="examples/UnsafeCodeConstructionSafe.js" />
</example>
<references>
<li>
OWASP:
<a href="https://www.owasp.org/index.php/Code_Injection">Code Injection</a>.
</li>
<li>
Wikipedia: <a href="https://en.wikipedia.org/wiki/Code_injection">Code Injection</a>.
</li>
</references>
</qhelp>

View File

@@ -0,0 +1,7 @@
export function unsafeDeserialize(value) {
return eval(`(${value})`);
}
export function unsafeGetter(obj, path) {
return eval(`obj.${path}`);
}

View File

@@ -0,0 +1,8 @@
export function safeDeserialize(value) {
return JSON.parse(value);
}
const _ = require("lodash");
export function safeGetter(object, path) {
return _.get(object, path);
}