Skip to content

Saleor Core: the high performance, composable, headless commerce API.

semgrep: support all different types of try-except blocks (#16662)

Mikail1159dd7593e5
diff --git a/.semgrep/security/logging/exception-object-in-logger-extra.py b/.semgrep/security/logging/exception-object-in-logger-extra.py
index 85ae4953bb..a217c3ff66 100644
--- a/.semgrep/security/logging/exception-object-in-logger-extra.py
+++ b/.semgrep/security/logging/exception-object-in-logger-extra.py
@@ -6,6 +6,43 @@ def test_log_info_exception():
# ruleid: exception-object-in-logger-extra
logger.info("Failed", extra={"error": exc})
+# Should match when catching multiple exceptions.
+def test_log_catch_multiple_exceptions():
+ try:
+ do_something()
+ except (OSError, FileNotFoundError) as exc:
+ # ruleid: exception-object-in-logger-extra
+ logger.info("Failed", extra={"error": exc})
+
+# TODO: uncomment once 'except*' is supported by Semgrep
+# https://github.com/semgrep/semgrep/issues/10511
+# # Should match when catching an exception group.
+# def test_log_catch_wildcard():
+# try:
+# do_something()
+# except* Exception as exc:
+# <ruleid>: exception-object-in-logger-extra
+# logger.info("Failed", extra={"error": exc})
+
+# Should match when logging an exception in the middle
+# of a 'except' block.
+def test_log_middle_of_statement():
+ try:
+ do_something()
+ except Exception as exc:
+ something()
+ # ruleid: exception-object-in-logger-extra
+ logger.info("Failed", extra={"error": exc})
+ something()
+
+# Should match when catching multiple exceptions.
+def test_log_info_exception():
+ try:
+ do_something()
+ except (OSError, FileNotFoundError) as exc:
+ # ruleid: exception-object-in-logger-extra
+ logger.info("Failed", extra={"error": exc})
+
# Should match when extra contains multiple keys & values.
def test_log_multiple_extra():
try:
@@ -33,6 +70,38 @@ def test_log_exception_trailing_arguments_case2():
# ruleid: exception-object-in-logger-extra
logger.info(extra={"error": exc}, msg="Failed", exc_info=True)
+# Should match when using a 'finally' block
+def test_log_exception_with_finally_block():
+ try:
+ do_something()
+ except Exception as exc:
+ # ruleid: exception-object-in-logger-extra
+ logger.info("Failed", extra={"error": exc})
+ finally:
+ do_something_else()
+
+# Should match when using a 'else' block
+def test_log_exception_with_else_block():
+ try:
+ do_something()
+ except Exception as exc:
+ # ruleid: exception-object-in-logger-extra
+ logger.info("Failed", extra={"error": exc})
+ else:
+ do_something_else()
+
+# Should match when using a 'else' and 'finally' block
+def test_log_exception_with_finally_else_block():
+ try:
+ do_something()
+ except Exception as exc:
+ # ruleid: exception-object-in-logger-extra
+ logger.info("Failed", extra={"error": exc})
+ else:
+ do_something_else()
+ finally:
+ do_something_else()
+
# Should not match when converting an exception object to string.
def test_not_logging_exception():
try:
diff --git a/.semgrep/security/logging/exception-object-in-logger-extra.yaml b/.semgrep/security/logging/exception-object-in-logger-extra.yaml
index 045e621cf2..64f597b018 100644
--- a/.semgrep/security/logging/exception-object-in-logger-extra.yaml
+++ b/.semgrep/security/logging/exception-object-in-logger-extra.yaml
@@ -1,11 +1,46 @@
rules:
- id: exception-object-in-logger-extra
+ # e.g., it can cause Sentry to hold the thread reference which leads to threads to not be freed.
+ # This is especially true with asgiref as it can spin N threads per HTTP request.
+ message: >-
+ Exception objects should not be put inside a logger's `extra` field, it
+ can lead to high memory usage. Instead, use str(exc) to log the message.
patterns:
- - pattern-inside: |
- try:
- ...
- except ... as $EXC:
- ...
+ # - Matches all combinations of the 'try' statement
+ # from https://docs.python.org/3.12/reference/compound_stmts.html#the-try-statement
+ # - Except exception groups ('except*', https://docs.python.org/3.12/reference/compound_stmts.html#except-star),
+ # due to being unsupported by the Semgrep Engine.
+ # Ticket: https://github.com/semgrep/semgrep/issues/10511
+ # - Maybe somehow we can avoid listing the different possible statements.
+ - pattern-either:
+ - pattern-inside: |
+ try:
+ ...
+ except ... as $EXC:
+ ...
+ - pattern-inside: |
+ try:
+ ...
+ except ... as $EXC:
+ ...
+ finally:
+ ...
+ - pattern-inside: |
+ try:
+ ...
+ except ... as $EXC:
+ ...
+ else:
+ ...
+ - pattern-inside: |
+ try:
+ ...
+ except ... as $EXC:
+ ...
+ else:
+ ...
+ finally:
+ ...
- pattern: |
$LOGGER.$LOG_FUNC(..., extra={..., ...: $EXC, ...}, ...)
- metavariable-regex:
@@ -14,10 +49,19 @@ rules:
- metavariable-regex:
metavariable: $LOG_FUNC
regex: (debug|info|warn|warning|error|exception|critical)
- # e.g., it can cause Sentry to hold the thread reference which leads to threads to not be freed. This is especially true with asgiref as it can spin N threads per HTTP request.
- message: >-
- Exception objects should not be put inside a logger's `extra` field, it can lead to high memory usage. Instead, use str(exc) to log the message.
- languages: [python]
- severity: ERROR
focus-metavariable: $EXC
+ languages:
+ - python
+ severity: ERROR
+ metadata:
+ category: security
+ subcategory:
+ - audit
+ cwe:
+ - "CWE 400: Uncontrolled Resource Consumption"
+ confidence: MEDIUM
+ likelihood: MEDIUM
+ impact: MEDIUM
+ technology:
+ - python
diff --git a/saleor/payment/gateways/adyen/webhooks.py b/saleor/payment/gateways/adyen/webhooks.py
index 2fb356988f..f6721351be 100644
--- a/saleor/payment/gateways/adyen/webhooks.py
+++ b/saleor/payment/gateways/adyen/webhooks.py
@@ -857,8 +857,8 @@ def handle_order_closed(notification: dict[str, Any], gateway_config: GatewayCon
kind,
get_plugins_manager(allow_replica=False),
)
- except Exception as e:
- logger.exception("Exception during order creation", extra={"error": e})
+ except Exception:
+ logger.exception("Exception during order creation")
return
finally:
if not order and adyen_partial_payments: