Skip to content

Commit 63672ca

Browse files
authored
Merge pull request #7616 from github/henrymercer/js-atm-add-query-help
JS: Add query help for ML-powered queries
2 parents 024bd27 + e912846 commit 63672ca

File tree

5 files changed

+174
-6
lines changed

5 files changed

+174
-6
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# NoSQL database query built from user-controlled sources (experimental)
2+
This is an experimental query. Experimental queries generate alerts using machine learning. They might include more false positives but they will improve over time.
3+
4+
If a database query (such as a SQL or NoSQL query) is built from user-provided data without sufficient sanitization, a malicious user may be able to run malicious database queries.
5+
6+
7+
## Recommendation
8+
Most database connector libraries offer a way of safely embedding untrusted data into a query by means of query parameters or prepared statements.
9+
10+
For NoSQL queries, make use of an operator like MongoDB's `$eq` to ensure that untrusted data is interpreted as a literal value and not as a query object.
11+
12+
13+
## Example
14+
In the following example, assume the function `handler` is an HTTP request handler in a web application, whose parameter `req` contains the request object.
15+
16+
The handler constructs two copies of the same SQL query involving user input taken from the request object, once unsafely using string concatenation, and once safely using query parameters.
17+
18+
In the first case, the query string `query1` is built by directly concatenating a user-supplied request parameter with some string literals. The parameter may include quote characters, so this code is vulnerable to a SQL injection attack.
19+
20+
In the second case, the parameter is embedded into the query string `query2` using query parameters. In this example, we use the API offered by the `pg` Postgres database connector library, but other libraries offer similar features. This version is immune to injection attacks.
21+
22+
23+
```javascript
24+
const app = require("express")(),
25+
pg = require("pg"),
26+
pool = new pg.Pool(config);
27+
28+
app.get("search", function handler(req, res) {
29+
// BAD: the category might have SQL special characters in it
30+
var query1 =
31+
"SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" +
32+
req.params.category +
33+
"' ORDER BY PRICE";
34+
pool.query(query1, [], function(err, results) {
35+
// process results
36+
});
37+
38+
// GOOD: use parameters
39+
var query2 =
40+
"SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY=$1" + " ORDER BY PRICE";
41+
pool.query(query2, [req.params.category], function(err, results) {
42+
// process results
43+
});
44+
});
45+
46+
```
47+
48+
## References
49+
* Wikipedia: [SQL injection](https://en.wikipedia.org/wiki/SQL_injection).
50+
* MongoDB: [$eq operator](https://docs.mongodb.com/manual/reference/operator/query/eq).
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# SQL database query built from user-controlled sources (experimental)
2+
This is an experimental query. Experimental queries generate alerts using machine learning. They might include more false positives but they will improve over time.
3+
4+
If a database query (such as a SQL or NoSQL query) is built from user-provided data without sufficient sanitization, a malicious user may be able to run malicious database queries.
5+
6+
7+
## Recommendation
8+
Most database connector libraries offer a way of safely embedding untrusted data into a query by means of query parameters or prepared statements.
9+
10+
For NoSQL queries, make use of an operator like MongoDB's `$eq` to ensure that untrusted data is interpreted as a literal value and not as a query object.
11+
12+
13+
## Example
14+
In the following example, assume the function `handler` is an HTTP request handler in a web application, whose parameter `req` contains the request object.
15+
16+
The handler constructs two copies of the same SQL query involving user input taken from the request object, once unsafely using string concatenation, and once safely using query parameters.
17+
18+
In the first case, the query string `query1` is built by directly concatenating a user-supplied request parameter with some string literals. The parameter may include quote characters, so this code is vulnerable to a SQL injection attack.
19+
20+
In the second case, the parameter is embedded into the query string `query2` using query parameters. In this example, we use the API offered by the `pg` Postgres database connector library, but other libraries offer similar features. This version is immune to injection attacks.
21+
22+
23+
```javascript
24+
const app = require("express")(),
25+
pg = require("pg"),
26+
pool = new pg.Pool(config);
27+
28+
app.get("search", function handler(req, res) {
29+
// BAD: the category might have SQL special characters in it
30+
var query1 =
31+
"SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" +
32+
req.params.category +
33+
"' ORDER BY PRICE";
34+
pool.query(query1, [], function(err, results) {
35+
// process results
36+
});
37+
38+
// GOOD: use parameters
39+
var query2 =
40+
"SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY=$1" + " ORDER BY PRICE";
41+
pool.query(query2, [req.params.category], function(err, results) {
42+
// process results
43+
});
44+
});
45+
46+
```
47+
48+
## References
49+
* Wikipedia: [SQL injection](https://en.wikipedia.org/wiki/SQL_injection).
50+
* MongoDB: [$eq operator](https://docs.mongodb.com/manual/reference/operator/query/eq).
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Uncontrolled data used in path expression (experimental)
2+
This is an experimental query. Experimental queries generate alerts using machine learning. They might include more false positives but they will improve over time.
3+
4+
Accessing files using paths constructed from user-controlled data can allow an attacker to access unexpected resources. This can result in sensitive information being revealed or deleted, or an attacker being able to influence behavior by modifying unexpected files.
5+
6+
7+
## Recommendation
8+
Validate user input before using it to construct a file path, either using an off-the-shelf library like the `sanitize-filename` npm package, or by performing custom validation.
9+
10+
Ideally, follow these rules:
11+
12+
* Do not allow more than a single "." character.
13+
* Do not allow directory separators such as "/" or "\\" (depending on the file system).
14+
* Do not rely on simply replacing problematic sequences such as "../". For example, after applying this filter to ".../...//", the resulting string would still be "../".
15+
* Use a whitelist of known good patterns.
16+
17+
## Example
18+
In the first example, a file name is read from an HTTP request and then used to access a file. However, a malicious user could enter a file name which is an absolute path, such as `"/etc/passwd"`.
19+
20+
In the second example, it appears that the user is restricted to opening a file within the `"user"` home directory. However, a malicious user could enter a file name containing special characters. For example, the string `"../../etc/passwd"` will result in the code reading the file located at `"/home/user/../../etc/passwd"`, which is the system's password file. This file would then be sent back to the user, giving them access to all the system's passwords.
21+
22+
23+
```javascript
24+
var fs = require('fs'),
25+
http = require('http'),
26+
url = require('url');
27+
28+
var server = http.createServer(function(req, res) {
29+
let path = url.parse(req.url, true).query.path;
30+
31+
// BAD: This could read any file on the file system
32+
res.write(fs.readFileSync(path));
33+
34+
// BAD: This could still read any file on the file system
35+
res.write(fs.readFileSync("/home/user/" + path));
36+
});
37+
38+
```
39+
40+
## References
41+
* OWASP: [Path Traversal](https://owasp.org/www-community/attacks/Path_Traversal).
42+
* npm: [sanitize-filename](https://www.npmjs.com/package/sanitize-filename) package.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Client-side cross-site scripting (experimental)
2+
This is an experimental query. Experimental queries generate alerts using machine learning. They might include more false positives but they will improve over time.
3+
4+
Directly writing user input (for example, a URL query parameter) to a webpage without properly sanitizing the input first, allows for a cross-site scripting vulnerability.
5+
6+
This kind of vulnerability is also called *DOM-based* cross-site scripting, to distinguish it from other types of cross-site scripting.
7+
8+
9+
## Recommendation
10+
To guard against cross-site scripting, consider using contextual output encoding/escaping before writing user input to the page, or one of the other solutions that are mentioned in the references.
11+
12+
13+
## Example
14+
The following example shows part of the page URL being written directly to the document, leaving the website vulnerable to cross-site scripting.
15+
16+
17+
```javascript
18+
function setLanguageOptions() {
19+
var href = document.location.href,
20+
deflt = href.substring(href.indexOf("default=")+8);
21+
document.write("<OPTION value=1>"+deflt+"</OPTION>");
22+
document.write("<OPTION value=2>English</OPTION>");
23+
}
24+
25+
```
26+
27+
## References
28+
* OWASP: [DOM based XSS Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html).
29+
* OWASP: [XSS (Cross Site Scripting) Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html).
30+
* OWASP [DOM Based XSS](https://www.owasp.org/index.php/DOM_Based_XSS).
31+
* OWASP [Types of Cross-Site Scripting](https://www.owasp.org/index.php/Types_of_Cross-Site_Scripting).
32+
* Wikipedia: [Cross-site scripting](http://en.wikipedia.org/wiki/Cross-site_scripting).
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
- description: ATM boosted Code Scanning queries for JavaScript
22
- queries: .
3-
- include:
4-
id:
5-
- adaptive-threat-modeling/js/nosql-injection
6-
- adaptive-threat-modeling/js/sql-injection
7-
- adaptive-threat-modeling/js/path-injection
8-
- adaptive-threat-modeling/js/xss

0 commit comments

Comments
 (0)