-
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(python): extend and add rules following benchmarking (#452)
- Loading branch information
Showing
40 changed files
with
806 additions
and
76 deletions.
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
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,26 @@ | ||
patterns: | ||
- | | ||
def __html__($<...>): | ||
languages: | ||
- python | ||
severity: high | ||
metadata: | ||
description: Usage of __html__ magic method | ||
remediation_message: | | ||
## Description | ||
The Django template engine considers values returned by the `__html__` method as "safe" for rendering and therefore no HTML escaping is applied (escaping special characters like ampersands or quotes). Using this method exposes your application to Cross-Site Scripting (XSS) vulnerabilities. | ||
## Remediations | ||
- **Do not** use the `__html__` magic method | ||
- **Do** use `format_html` instead to build up HTML fragments. This is more appropriate because it applies escaping to its arguments by default. | ||
```python | ||
from django.utils.html import format_html | ||
format_html("{} <b>{}</b> {}", mark_safe(some_html), some text) | ||
``` | ||
cwe_id: | ||
- 79 | ||
id: python_django_html_magic_method | ||
documentation_url: https://docs.bearer.com/reference/rules/python_django_html_magic_method |
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,58 @@ | ||
imports: | ||
- python_shared_lang_import3 | ||
sanitizer: python_django_mark_safe_sanitizer | ||
patterns: | ||
- pattern: $<MARK_SAFE>($<...>) | ||
filters: | ||
- variable: MARK_SAFE | ||
detection: python_shared_lang_import3 | ||
scope: cursor | ||
filters: | ||
- variable: MODULE1 | ||
values: [django] | ||
- variable: MODULE2 | ||
values: [utils] | ||
- variable: MODULE3 | ||
values: [safestring] | ||
- variable: NAME | ||
values: [mark_safe] | ||
auxiliary: | ||
- id: python_django_mark_safe_sanitizer | ||
patterns: | ||
- pattern: $<FORMAT_HTML>($<...>) | ||
filters: | ||
- variable: FORMAT_HTML | ||
detection: python_shared_lang_import3 | ||
scope: cursor | ||
filters: | ||
- variable: MODULE1 | ||
values: [django] | ||
- variable: MODULE2 | ||
values: [utils] | ||
- variable: MODULE3 | ||
values: [html] | ||
- variable: NAME | ||
values: [format_html] | ||
languages: | ||
- python | ||
severity: high | ||
metadata: | ||
description: Usage of mark_safe | ||
remediation_message: | | ||
## Description | ||
The Django utils method `mark_safe` is used to mark a string as "safe" for output as HTML, but it does not escape special characters like ampersands or quotes, and therefore could expose your application to XSS attacks if an external string is passed to it. | ||
## Remediations | ||
- **Do not** use `mark_safe` wherever possible | ||
- **Do** use `format_html` instead to build up HTML fragments. This is more appropriate because it applies escaping to its arguments by default. | ||
```python | ||
from django.utils.html import format_html | ||
format_html("{} <b>{}</b> {}", mark_safe(some_html), some text) | ||
``` | ||
cwe_id: | ||
- 79 | ||
id: python_django_mark_safe | ||
documentation_url: https://docs.bearer.com/reference/rules/python_django_mark_safe |
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,74 @@ | ||
imports: | ||
- python_shared_lang_import1 | ||
patterns: | ||
- pattern: $<PICKLE>($<...>) | ||
filters: | ||
- variable: PICKLE | ||
detection: python_shared_lang_import1 | ||
scope: cursor | ||
filters: | ||
- variable: MODULE1 | ||
values: | ||
- pickle | ||
- _pickle | ||
- cPickle | ||
- variable: NAME | ||
values: | ||
- load | ||
- loads | ||
- dump | ||
- dumps | ||
- pattern: $<UNPICKLER>.$<METHOD>() | ||
filters: | ||
- variable: UNPICKLER | ||
detection: python_lang_avoid_pickle_unpickler | ||
scope: cursor | ||
- variable: METHOD | ||
values: | ||
- load | ||
- persistent_load | ||
auxiliary: | ||
- id: python_lang_avoid_pickle_unpickler | ||
patterns: | ||
- pattern: $<UNPICKLER>($<...>) | ||
filters: | ||
- variable: UNPICKLER | ||
detection: python_shared_lang_import1 | ||
scope: cursor | ||
filters: | ||
- variable: MODULE1 | ||
values: | ||
- pickle | ||
- _pickle | ||
- cPickle | ||
- variable: NAME | ||
values: [Unpickler] | ||
languages: | ||
- python | ||
severity: critical | ||
metadata: | ||
description: Usage of unsafe Pickle libraries | ||
remediation_message: | | ||
## Description | ||
Using pickle, _pickle and cPickle can make your application vulnerable to unsafe code execution. This is because the deserialization logic of these libraries allows for arbitrary code execution. It is best practices to avoid these libraries and to use a safer serialization formats like JSON. | ||
## Remediations | ||
- **Do not** use pickle or its derivatives for deserialization wherever possible. These libraries are open to security vulnerabilities. | ||
- **Do** use recommended safer formats like JSON, Protocol Buffers (protobuf) and MessagePack. | ||
```python | ||
import msgpack #MessagePack | ||
data = {'key': 'value'} | ||
packed_data = msgpack.packb(data) | ||
``` | ||
## References | ||
- [OWASP Deserialization cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/Deserialization_Cheat_Sheet.html) | ||
cwe_id: | ||
- 502 | ||
id: python_lang_avoid_pickle | ||
documentation_url: https://docs.bearer.com/reference/rules/python_lang_avoid_pickle |
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
Oops, something went wrong.