Skip to content

Commit 6d6ec89

Browse files
committed
JS: add qhelp
1 parent 708fd3d commit 6d6ec89

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>
8+
9+
Sanitizing untrusted input for HTML meta-characters is an important
10+
technique for preventing cross-site scripting attacks. Usually, this
11+
is done by escaping <code>&lt;</code>, <code>&gt;</code>,
12+
<code>&amp;</code> and <code>&quot;</code>. But the context in which
13+
the sanitized value is used decides which characters that actually
14+
need to be sanitized.
15+
16+
</p>
17+
18+
<p>
19+
20+
As a consequence, some programs only sanitize
21+
<code>&lt;</code> and <code>&gt;</code> since those are the most
22+
common dangerous characters. The lack of sanitization for
23+
<code>&quot;</code> is problematic when an incompletely sanitized
24+
value is used as an HTML attribute in a string that
25+
<strong>later</strong> is parsed as HTML.
26+
27+
</p>
28+
29+
</overview>
30+
31+
<recommendation>
32+
33+
Sanitize all relevant HTML meta-characters when constructing
34+
HTML dynamically, pay special attention to where the sanitized value is used.
35+
36+
</recommendation>
37+
38+
<example>
39+
40+
<p>
41+
42+
The following example code writes part of an HTTP request (which is
43+
controlled by the user) to an HTML attribute of the server response.
44+
45+
The user-controlled value is, however, not sanitized for
46+
<code>&quot;</code>. This leaves the website vulnerable to cross-site
47+
scripting since an attacker can use a string like <code>"
48+
onclick="alert(42)</code> to inject JavaScript code into the response.
49+
50+
</p>
51+
<sample src="examples/IncompleteHtmlAttributeSanitization.js" />
52+
53+
54+
<p>
55+
56+
Sanitizing the user-controlled data for
57+
<code>&quot;</code> prevents the vulnerability:
58+
59+
</p>
60+
61+
<sample src="examples/IncompleteHtmlAttributeSanitizationGood.js" />
62+
63+
</example>
64+
65+
<references>
66+
<li>
67+
OWASP:
68+
<a href="https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html">DOM based
69+
XSS Prevention Cheat Sheet</a>.
70+
</li>
71+
<li>
72+
OWASP:
73+
<a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html">XSS
74+
(Cross Site Scripting) Prevention Cheat Sheet</a>.
75+
</li>
76+
<li>
77+
OWASP
78+
<a href="https://www.owasp.org/index.php/Types_of_Cross-Site_Scripting">Types of Cross-Site
79+
Scripting</a>.
80+
</li>
81+
<li>
82+
Wikipedia: <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross-site scripting</a>.
83+
</li>
84+
</references>
85+
86+
</qhelp>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var app = require('express')();
2+
3+
app.get('/user/:id', function(req, res) {
4+
let id = req.params.id;
5+
id = id.replace(/<|>/g, ""); // BAD
6+
let userHtml = `<div data-id="${id}">${getUserName(id)} || Unknown name</div>`;
7+
// ...
8+
res.send(prefix + userHtml + suffix);
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var app = require('express')();
2+
3+
app.get('/user/:id', function(req, res) {
4+
let id = req.params.id;
5+
id = id.replace(/<|>|&|"/g, ""); // GOOD
6+
let userHtml = `<div data-id="${id}">${getUserName(id)} || Unknown name</div>`;
7+
// ...
8+
res.send(prefix + userHtml + suffix);
9+
});

0 commit comments

Comments
 (0)