Skip to content

Commit fec7ea6

Browse files
committed
ATM: add missing query help files
1 parent b08fa43 commit fec7ea6

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
# Shell command built from environment values (experimental)
3+
4+
Dynamically constructing a shell command with values from the
5+
local environment, such as file paths, may inadvertently
6+
change the meaning of the shell command.
7+
8+
Such changes can occur when an environment value contains
9+
characters that the shell interprets in a special way, for instance
10+
quotes and spaces.
11+
12+
This can result in the shell command misbehaving, or even
13+
allowing a malicious user to execute arbitrary commands on the system.
14+
15+
Note: This CodeQL query is an experimental query. Experimental queries generate alerts using machine learning. They might include more false positives but they will improve over time.
16+
17+
## Recommendation
18+
19+
If possible, use hard-coded string literals to specify the
20+
shell command to run, and provide the dynamic arguments to the shell
21+
command separately to avoid interpretation by the shell.
22+
23+
Alternatively, if the shell command must be constructed
24+
dynamically, then add code to ensure that special characters in
25+
environment values do not alter the shell command unexpectedly.
26+
27+
## Example
28+
29+
The following example shows a dynamically constructed shell
30+
command that recursively removes a temporary directory that is located
31+
next to the currently executing JavaScript file. Such utilities are
32+
often found in custom build scripts.
33+
34+
```javascript
35+
var cp = require("child_process"),
36+
path = require("path");
37+
function cleanupTemp() {
38+
let cmd = "rm -rf " + path.join(__dirname, "temp");
39+
cp.execSync(cmd); // BAD
40+
}
41+
```
42+
43+
The shell command will, however, fail to work as intended if the
44+
absolute path of the script's directory contains spaces. In that
45+
case, the shell command will interpret the absolute path as multiple
46+
paths, instead of a single path.
47+
48+
For instance, if the absolute path of
49+
the temporary directory is "`/home/username/important project/temp`", then the shell command will recursively delete
50+
`"/home/username/important"` and `"project/temp"`,
51+
where the latter path gets resolved relative to the working directory
52+
of the JavaScript process.
53+
54+
Even worse, although less likely, a malicious user could
55+
provide the path `"/home/username/; cat /etc/passwd #/important
56+
project/temp"` in order to execute the command `"cat
57+
/etc/passwd"`.
58+
59+
To avoid such potentially catastrophic behaviors, provide the
60+
directory as an argument that does not get interpreted by a
61+
shell:
62+
63+
```javascript
64+
var cp = require("child_process"),
65+
path = require("path");
66+
function cleanupTemp() {
67+
let cmd = "rm",
68+
args = ["-rf", path.join(__dirname, "temp")];
69+
cp.execFileSync(cmd, args); // GOOD
70+
}
71+
```
72+
73+
74+
## References
75+
* OWASP: [Command Injection](https://www.owasp.org/index.php/Command_Injection)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# DOM text reinterpreted as HTML (experimental)
2+
3+
Extracting text from a DOM node and interpreting it as HTML can lead to a cross-site scripting vulnerability.
4+
5+
A webpage with this vulnerability reads text from the DOM, and afterwards adds the text as HTML to the DOM. Using text from the DOM as HTML effectively unescapes the text, and thereby invalidates any escaping done on the text. If an attacker is able to control the safe sanitized text, then this vulnerability can be exploited to perform a cross-site scripting attack.
6+
7+
Note: This CodeQL query is an experimental query. Experimental queries generate alerts using machine learning. They might include more false positives but they will improve over time.
8+
9+
## Recommendation
10+
11+
To guard against cross-site scripting, consider using contextual output encoding/escaping before writing text to the page, or one of the other solutions that are mentioned in the References section below.
12+
13+
## Example
14+
15+
The following example shows a webpage using a `data-target` attribute
16+
to select and manipulate a DOM element using the JQuery library. In the example, the
17+
`data-target` attribute is read into the `target` variable, and the
18+
`$` function is then supposed to use the `target` variable as a CSS
19+
selector to determine which element should be manipulated.
20+
21+
```javascript
22+
$("button").click(function () {
23+
var target = $(this).attr("data-target");
24+
$(target).hide();
25+
});
26+
```
27+
28+
However, if an attacker can control the `data-target` attribute,
29+
then the value of `target` can be used to cause the `$` function
30+
to execute arbitrary JavaScript.
31+
32+
The above vulnerability can be fixed by using `$.find` instead of `$`.
33+
The `$.find` function will only interpret `target` as a CSS selector
34+
and never as HTML, thereby preventing an XSS attack.
35+
36+
```javascript
37+
$("button").click(function () {
38+
var target = $(this).attr("data-target");
39+
$.find(target).hide();
40+
});
41+
```
42+
43+
## References
44+
* OWASP: [DOM based XSS Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html)
45+
* OWASP: [(Cross Site Scripting) Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html)
46+
* OWASP [DOM Based XSS](https://owasp.org/www-community/attacks/DOM_Based_XSS)
47+
* OWASP [Types of Cross-Site Scripting](https://owasp.org/www-community/Types_of_Cross-Site_Scripting)
48+
* Wikipedia: [Cross-site scripting](http://en.wikipedia.org/wiki/Cross-site_scripting)

0 commit comments

Comments
 (0)