Skip to content

Commit 442749b

Browse files
committed
JS: add heuristic variants of queries that use RemoteFlowSource
1 parent 2f84b21 commit 442749b

File tree

68 files changed

+1701
-968
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1701
-968
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Using unsanitized untrusted data in an external API can cause a variety of security issues. This query reports
7+
external APIs that use untrusted data. The results are not filtered so that you can audit all examples. The query provides data for security reviews of the application and you can also use it to identify external APIs that should be modeled as either taint steps, or sinks for specific problems.</p>
8+
9+
<p>An external API is defined as a method call to a method that is not defined in the source code, not overridden
10+
in the source code, and is not modeled as a taint step in the default taint library. External APIs may be from the
11+
third-party dependencies or from internal dependencies. The query reports uses of
12+
untrusted data one of the arguments of external API call or in the return value from a callback passed to an external API.</p>
13+
14+
</overview>
15+
<recommendation>
16+
17+
<p>For each result:</p>
18+
19+
<ul>
20+
<li>If the result highlights a known sink, confirm that the result is reported by the relevant query, or
21+
that the result is a false positive because this data is sanitized.</li>
22+
<li>If the result highlights an unknown sink for a problem, then add modeling for the sink to the relevant query,
23+
and confirm that the result is either found, or is safe due to appropriate sanitization.</li>
24+
<li>If the result represents a call to an external API that transfers taint, add the appropriate modeling, and
25+
re-run the query to determine what new results have appeared due to this additional modeling.</li>
26+
</ul>
27+
28+
<p>Otherwise, the result is likely uninteresting. Custom versions of this query can extend the <code>SafeExternalAPIMethod</code>
29+
class to exclude known safe external APIs from future analysis.</p>
30+
31+
</recommendation>
32+
<example>
33+
34+
<p>In this first example, a query parameter is read from the <code>req</code> parameter and then ultimately used in a call to the
35+
<code>res.send</code> external API:</p>
36+
37+
<sample src="ExternalAPISinkExample.js" />
38+
39+
<p>This is a reflected XSS sink. The XSS query should therefore be reviewed to confirm that this sink is appropriately modeled,
40+
and if it is, to confirm that the query reports this particular result, or that the result is a false positive due to
41+
some existing sanitization.</p>
42+
43+
<p>In this second example, again a query parameter is read from <code>req</code>.</p>
44+
45+
<sample src="ExternalAPITaintStepExample.js" />
46+
47+
<p>If the query reported the call to <code>path.join</code> on line 4, this would suggest that this external API is
48+
not currently modeled as a taint step in the taint tracking library. The next step would be to model this as a taint step, then
49+
re-run the query to determine what additional results might be found. In this example, it seems the result of the
50+
<code>path.join</code> will be used as a file path, leading to a path traversal vulnerability.</p>
51+
52+
<p>Note that both examples are correctly handled by the standard taint tracking library and security queries.</p>
53+
</example>
54+
<references>
55+
56+
</references>
57+
</qhelp>
Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,6 @@
11
<!DOCTYPE qhelp PUBLIC
2-
"-//Semmle//qhelp//EN"
3-
"qhelp.dtd">
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
44
<qhelp>
5-
<overview>
6-
<p>Using unsanitized untrusted data in an external API can cause a variety of security issues. This query reports
7-
external APIs that use untrusted data. The results are not filtered so that you can audit all examples. The query provides data for security reviews of the application and you can also use it to identify external APIs that should be modeled as either taint steps, or sinks for specific problems.</p>
8-
9-
<p>An external API is defined as a method call to a method that is not defined in the source code, not overridden
10-
in the source code, and is not modeled as a taint step in the default taint library. External APIs may be from the
11-
third-party dependencies or from internal dependencies. The query reports uses of
12-
untrusted data one of the arguments of external API call or in the return value from a callback passed to an external API.</p>
13-
14-
</overview>
15-
<recommendation>
16-
17-
<p>For each result:</p>
18-
19-
<ul>
20-
<li>If the result highlights a known sink, confirm that the result is reported by the relevant query, or
21-
that the result is a false positive because this data is sanitized.</li>
22-
<li>If the result highlights an unknown sink for a problem, then add modeling for the sink to the relevant query,
23-
and confirm that the result is either found, or is safe due to appropriate sanitization.</li>
24-
<li>If the result represents a call to an external API that transfers taint, add the appropriate modeling, and
25-
re-run the query to determine what new results have appeared due to this additional modeling.</li>
26-
</ul>
27-
28-
<p>Otherwise, the result is likely uninteresting. Custom versions of this query can extend the <code>SafeExternalAPIMethod</code>
29-
class to exclude known safe external APIs from future analysis.</p>
30-
31-
</recommendation>
32-
<example>
33-
34-
<p>In this first example, a query parameter is read from the <code>req</code> parameter and then ultimately used in a call to the
35-
<code>res.send</code> external API:</p>
36-
37-
<sample src="ExternalAPISinkExample.js" />
38-
39-
<p>This is a reflected XSS sink. The XSS query should therefore be reviewed to confirm that this sink is appropriately modeled,
40-
and if it is, to confirm that the query reports this particular result, or that the result is a false positive due to
41-
some existing sanitization.</p>
42-
43-
<p>In this second example, again a query parameter is read from <code>req</code>.</p>
44-
45-
<sample src="ExternalAPITaintStepExample.js" />
46-
47-
<p>If the query reported the call to <code>path.join</code> on line 4, this would suggest that this external API is
48-
not currently modeled as a taint step in the taint tracking library. The next step would be to model this as a taint step, then
49-
re-run the query to determine what additional results might be found. In this example, it seems the result of the
50-
<code>path.join</code> will be used as a file path, leading to a path traversal vulnerability.</p>
51-
52-
<p>Note that both examples are correctly handled by the standard taint tracking library and security queries.</p>
53-
</example>
54-
<references>
55-
56-
</references>
57-
</qhelp>
5+
<include src="UntrustedDataToExternalAPI.inc.qhelp" />
6+
</qhelp>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Code that passes user input directly to
7+
<code>require('child_process').exec</code>, or some other library
8+
routine that executes a command, allows the user to execute malicious
9+
code.</p>
10+
11+
</overview>
12+
<recommendation>
13+
14+
<p>If possible, use hard-coded string literals to specify the command to run
15+
or library to load. Instead of passing the user input directly to the
16+
process or library function, examine the user input and then choose
17+
among hard-coded string literals.</p>
18+
19+
<p>If the applicable libraries or commands cannot be determined at
20+
compile time, then add code to verify that the user input string is
21+
safe before using it.</p>
22+
23+
</recommendation>
24+
<example>
25+
26+
<p>The following example shows code that takes a shell script that can be changed
27+
maliciously by a user, and passes it straight to <code>child_process.exec</code>
28+
without examining it first.</p>
29+
30+
<sample src="examples/command-injection.js" />
31+
32+
</example>
33+
<references>
34+
35+
<li>
36+
OWASP:
37+
<a href="https://www.owasp.org/index.php/Command_Injection">Command Injection</a>.
38+
</li>
39+
40+
<!-- LocalWords: CWE untrusted unsanitized Runtime
41+
-->
42+
43+
</references>
44+
</qhelp>
Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,6 @@
11
<!DOCTYPE qhelp PUBLIC
2-
"-//Semmle//qhelp//EN"
3-
"qhelp.dtd">
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
44
<qhelp>
5-
<overview>
6-
<p>Code that passes user input directly to
7-
<code>require('child_process').exec</code>, or some other library
8-
routine that executes a command, allows the user to execute malicious
9-
code.</p>
10-
11-
</overview>
12-
<recommendation>
13-
14-
<p>If possible, use hard-coded string literals to specify the command to run
15-
or library to load. Instead of passing the user input directly to the
16-
process or library function, examine the user input and then choose
17-
among hard-coded string literals.</p>
18-
19-
<p>If the applicable libraries or commands cannot be determined at
20-
compile time, then add code to verify that the user input string is
21-
safe before using it.</p>
22-
23-
</recommendation>
24-
<example>
25-
26-
<p>The following example shows code that takes a shell script that can be changed
27-
maliciously by a user, and passes it straight to <code>child_process.exec</code>
28-
without examining it first.</p>
29-
30-
<sample src="examples/command-injection.js" />
31-
32-
</example>
33-
<references>
34-
35-
<li>
36-
OWASP:
37-
<a href="https://www.owasp.org/index.php/Command_Injection">Command Injection</a>.
38-
</li>
39-
40-
<!-- LocalWords: CWE untrusted unsanitized Runtime
41-
-->
42-
43-
</references>
44-
</qhelp>
5+
<include src="CommandInjection.inc.qhelp" />
6+
</qhelp>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>
8+
Directly writing user input (for example, a URL query parameter) to a webpage
9+
without properly sanitizing the input first, allows for a cross-site scripting vulnerability.
10+
</p>
11+
<p>
12+
This kind of vulnerability is also called <i>DOM-based</i> cross-site scripting, to distinguish
13+
it from other types of cross-site scripting.
14+
</p>
15+
</overview>
16+
17+
<recommendation>
18+
<p>
19+
To guard against cross-site scripting, consider using contextual output encoding/escaping before
20+
writing user input to the page, or one of the other solutions that are mentioned in the
21+
references.
22+
</p>
23+
</recommendation>
24+
25+
<example>
26+
<p>
27+
The following example shows part of the page URL being written directly to the document,
28+
leaving the website vulnerable to cross-site scripting.
29+
</p>
30+
<sample src="examples/Xss.js" />
31+
</example>
32+
33+
<references>
34+
<li>
35+
OWASP:
36+
<a href="https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html">DOM based
37+
XSS Prevention Cheat Sheet</a>.
38+
</li>
39+
<li>
40+
OWASP:
41+
<a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html">XSS
42+
(Cross Site Scripting) Prevention Cheat Sheet</a>.
43+
</li>
44+
<li>
45+
OWASP
46+
<a href="https://www.owasp.org/index.php/DOM_Based_XSS">DOM Based XSS</a>.
47+
</li>
48+
<li>
49+
OWASP
50+
<a href="https://www.owasp.org/index.php/Types_of_Cross-Site_Scripting">Types of Cross-Site
51+
Scripting</a>.
52+
</li>
53+
<li>
54+
Wikipedia: <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross-site scripting</a>.
55+
</li>
56+
</references>
57+
</qhelp>
Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,6 @@
11
<!DOCTYPE qhelp PUBLIC
2-
"-//Semmle//qhelp//EN"
3-
"qhelp.dtd">
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
44
<qhelp>
5-
6-
<overview>
7-
<p>
8-
Directly writing user input (for example, a URL query parameter) to a webpage
9-
without properly sanitizing the input first, allows for a cross-site scripting vulnerability.
10-
</p>
11-
<p>
12-
This kind of vulnerability is also called <i>DOM-based</i> cross-site scripting, to distinguish
13-
it from other types of cross-site scripting.
14-
</p>
15-
</overview>
16-
17-
<recommendation>
18-
<p>
19-
To guard against cross-site scripting, consider using contextual output encoding/escaping before
20-
writing user input to the page, or one of the other solutions that are mentioned in the
21-
references.
22-
</p>
23-
</recommendation>
24-
25-
<example>
26-
<p>
27-
The following example shows part of the page URL being written directly to the document,
28-
leaving the website vulnerable to cross-site scripting.
29-
</p>
30-
<sample src="examples/Xss.js" />
31-
</example>
32-
33-
<references>
34-
<li>
35-
OWASP:
36-
<a href="https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html">DOM based
37-
XSS Prevention Cheat Sheet</a>.
38-
</li>
39-
<li>
40-
OWASP:
41-
<a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html">XSS
42-
(Cross Site Scripting) Prevention Cheat Sheet</a>.
43-
</li>
44-
<li>
45-
OWASP
46-
<a href="https://www.owasp.org/index.php/DOM_Based_XSS">DOM Based XSS</a>.
47-
</li>
48-
<li>
49-
OWASP
50-
<a href="https://www.owasp.org/index.php/Types_of_Cross-Site_Scripting">Types of Cross-Site
51-
Scripting</a>.
52-
</li>
53-
<li>
54-
Wikipedia: <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross-site scripting</a>.
55-
</li>
56-
</references>
57-
</qhelp>
5+
<include src="Xss.inc.qhelp" />
6+
</qhelp>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>
8+
If a database query (such as a SQL or NoSQL query) is built from
9+
user-provided data without sufficient sanitization, a malicious user
10+
may be able to run malicious database queries.
11+
</p>
12+
</overview>
13+
14+
<recommendation>
15+
<p>
16+
Most database connector libraries offer a way of safely
17+
embedding untrusted data into a query by means of query parameters
18+
or prepared statements.
19+
</p>
20+
<p>
21+
For NoSQL queries, make use of an operator like MongoDB's <code>$eq</code>
22+
to ensure that untrusted data is interpreted as a literal value and not as
23+
a query object.
24+
</p>
25+
</recommendation>
26+
27+
<example>
28+
<p>
29+
In the following example, assume the function <code>handler</code> is
30+
an HTTP request handler in a web application, whose parameter
31+
<code>req</code> contains the request object.
32+
</p>
33+
34+
<p>
35+
The handler constructs two copies of the same SQL query involving
36+
user input taken from the request object, once unsafely using
37+
string concatenation, and once safely using query parameters.
38+
</p>
39+
40+
<p>
41+
In the first case, the query string <code>query1</code> is built by
42+
directly concatenating a user-supplied request parameter with some
43+
string literals. The parameter may include quote characters, so this
44+
code is vulnerable to a SQL injection attack.
45+
</p>
46+
47+
<p>
48+
In the second case, the parameter is embedded into the query string
49+
<code>query2</code> using query parameters. In this example, we use
50+
the API offered by the <code>pg</code> Postgres database connector
51+
library, but other libraries offer similar features. This version is
52+
immune to injection attacks.
53+
</p>
54+
55+
<sample src="examples/SqlInjection.js" />
56+
</example>
57+
58+
<references>
59+
<li>Wikipedia: <a href="https://en.wikipedia.org/wiki/SQL_injection">SQL injection</a>.</li>
60+
<li>MongoDB: <a href="https://docs.mongodb.com/manual/reference/operator/query/eq">$eq operator</a>.</li>
61+
</references>
62+
</qhelp>

0 commit comments

Comments
 (0)