Skip to content

Commit 9280893

Browse files
committed
Rust: Add qhelp for XSS query
1 parent 87fb764 commit 9280893

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>Directly writing user input (for example, an HTTP request parameter) to a web
8+
page, without properly sanitizing the input first, allows for a cross-site
9+
scripting vulnerability.</p>
10+
</overview>
11+
12+
<recommendation>
13+
<p>To guard against cross-site scripting, consider encoding/escaping the unstrusted
14+
input before including it in the HTML.</p>
15+
</recommendation>
16+
17+
<example>
18+
19+
<p>The following example shows a simple web handler that writes a path of the
20+
URL parameter directly to an HTML response, leaving the website vulnerable to
21+
cross-site scripting:</p>
22+
23+
<sample src="XSSBad.rs" />
24+
25+
<p>To fix this vulnerability, the user input should be HTML-encoded before being
26+
included in the response:</p>
27+
28+
<sample src="XSSGood.rs" />
29+
30+
</example>
31+
32+
<references>
33+
<li>
34+
OWASP:
35+
<a href="https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html">XSS
36+
(Cross Site Scripting) Prevention Cheat Sheet</a>.
37+
</li>
38+
<li>
39+
WiMISSING: Alert[rust/xss]kipedia: <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross-site scripting</a>.
40+
</li>
41+
<li>
42+
OWASP:
43+
<a href="https://owasp.org/www-community/attacks/xss/">Cross-site Scripting (XSS)</a>.
44+
</li>
45+
</references>
46+
</qhelp>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use actix_web::{web, HttpResponse, Result};
2+
3+
// BAD: User input is directly included in HTML response without sanitization
4+
async fn vulnerable_handler(path: web::Path<String>) -> impl Responder {
5+
let user_input = path.into_inner();
6+
7+
let html = format!(
8+
r#"
9+
<!DOCTYPE html>
10+
<html>
11+
<head><title>Welcome</title></head>
12+
<body>
13+
<h1>Hello, {}!</h1>
14+
</body>
15+
</html>
16+
"#,
17+
user_input
18+
);
19+
20+
Html::new(html) // Unsafe: User input included directly in the response
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use actix_web::{web, HttpResponse, Result};
2+
use askama::Template;
3+
4+
// GOOD: Manual HTML encoding using an `html_escape` function
5+
async fn safe_handler_with_encoding(path: web::Path<String>) -> impl Responder {
6+
let user_input = path.into_inner();
7+
let escaped_input = html_escape(&user_input);
8+
9+
let html = format!(
10+
r#"
11+
<!DOCTYPE html>
12+
<html>
13+
<head><title>Welcome</title></head>
14+
<body>
15+
<h1>Hello, {}!</h1>
16+
</body>
17+
</html>
18+
"#,
19+
escaped_input
20+
);
21+
22+
Html::new(html) // Safe: user input is HTML-encoded
23+
}

0 commit comments

Comments
 (0)