You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-**Silent failure** – using `pass` suppresses all evidence that something went wrong
16
16
@@ -21,10 +21,16 @@ This security concern also applies when using `continue` inside an exception blo
21
21
-`pass` statements in exception clauses
22
22
-`continue` statements in exception clauses
23
23
24
+
:::{important}
25
+
Treat the detection of `pass` and `continue` in exception handlers as a **signal** rather than proof of a security issue. Always examine whether the exception handler catches exceptions that are too broad (for example, `except:` or `except Exception:`).
26
+
27
+
No static analysis tool can determine with 100% confidence whether a particular exception handler represents a security risk. Due to Python's dynamic nature. The intent of the code, the surrounding context, and the runtime behavior all influence whether suppressing an exception is appropriate.
28
+
:::
29
+
24
30
25
31
## Background
26
32
27
-
Checking exception statements in Python code for possible security issues should be done.
33
+
Checking exception statements in Python code for possible security issues should always be done.
28
34
Reasons are e.g.:
29
35
30
36
1.**Masking of Critical Errors and Vulnerabilities:**
@@ -66,8 +72,11 @@ Reasons are e.g.:
66
72
# Optionally, re-raise the exception if the program cannot continue meaningfully
67
73
# raise
68
74
```
75
+
69
76
***Avoid `pass`:** Only use `pass`if the exception is truly expected and handling it means doing nothing, and you have documented why that's the case. Such scenarios are rare.
77
+
70
78
***Use `finally`for Cleanup:** If resources need to be released regardless of whether an exception occurs, use a `finally` block or context managers (`with` statements).
79
+
71
80
```python
72
81
try:
73
82
f = open("my_file.txt", "r")
@@ -92,3 +101,4 @@ Reasons are e.g.:
92
101
## More info
93
102
94
103
* [CWE-703: Improper Check or Handling of Exceptional Conditions](https://cwe.mitre.org/data/definitions/703.html)
0 commit comments