-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: raw html using user input rule
- Loading branch information
Showing
4 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
imports: | ||
- python_shared_common_html_user_input | ||
patterns: | ||
- pattern: $<STRING> | ||
filters: | ||
- variable: STRING | ||
string_regex: <\w+(\s[^>]*)?> | ||
- variable: STRING | ||
detection: python_shared_common_html_user_input | ||
scope: result | ||
severity: high | ||
metadata: | ||
description: "Unsanitized user input in raw HTML strings (XSS)" | ||
remediation_message: |- | ||
## Description | ||
Including unsanitized user input in HTML exposes your application to cross-site scripting (XSS) attacks. This vulnerability allows attackers to inject malicious scripts into web pages viewed by other users. | ||
## Remediations | ||
- **Do not** include user input directly in HTML strings. This practice can lead to XSS vulnerabilities. | ||
```python | ||
html = f"<h1>{user_input}</h1>" # unsafe | ||
``` | ||
- **Do** use a framework or templating language that automatically handles the encoding and sanitization of user input when constructing HTML. This approach minimizes the risk of XSS attacks. | ||
- **Do** sanitize user input if you must use HTML strings directly. Utilize libraries designed for input sanitization to ensure that user input does not contain malicious content. | ||
```python | ||
from html_sanitizer import Sanitizer | ||
sanitizer = Sanitizer() | ||
sanitized_value = sanitizer.sanitize(user_input) | ||
html = f"<h1>{sanitized_value}</h1>" | ||
``` | ||
## References | ||
- [OWASP Cross-Site Scripting (XSS) Cheatsheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html) | ||
cwe_id: | ||
- 79 | ||
id: python_lang_raw_html_using_user_input | ||
documentation_url: https://docs.bearer.com/reference/rules/python_lang_raw_html_using_user_input |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const { | ||
createNewInvoker, | ||
getEnvironment, | ||
} = require("../../../helper.js") | ||
const { ruleId, ruleFile, testBase } = getEnvironment(__dirname) | ||
|
||
describe(ruleId, () => { | ||
const invoke = createNewInvoker(ruleId, ruleFile, testBase) | ||
|
||
test("raw_html_using_user_input", () => { | ||
const testCase = "main.py" | ||
|
||
const results = invoke(testCase) | ||
|
||
expect(results).toEqual({ | ||
Missing: [], | ||
Extra: [] | ||
}) | ||
}) | ||
}) |
13 changes: 13 additions & 0 deletions
13
tests/python/lang/raw_html_using_user_input/testdata/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from html_sanitizer import Sanitizer | ||
|
||
user_input = input() | ||
|
||
def bad(): | ||
# bearer:expected python_lang_raw_html_using_user_input | ||
html = f"<strong>{user_input}</strong>" | ||
|
||
def ok(): | ||
sanitizer = Sanitizer() | ||
sanitized_value = sanitizer.sanitize(user_input) | ||
|
||
html = f"<strong>{sanitized_value}</strong>" |