Skip to content

Improper use of assert statement

Description

The application uses `assert` in non-test code, which can lead to undefined behavior or application crashes when compiling Python code to optimized byte code.

Examples

Insecure Code

python
assert user.is_authenticated(), "user must be authenticated"

Secure Code

python
try:
    if not user.is_authenticated():
        raise AuthError("user must be authenticated")
except AuthError as e:
    # Handle error
    #...
    # Return, do not continue processing
    return

Remediation

Remove `assert` calls and replace them with `if` conditions or `try/except` blocks if necessary.

Rule Details

FieldValue
IDCODE-0112
CategoryErrorHandling
SeverityLOW
CWECWE-754
ConfidenceHIGH
ImpactLOW
LikelihoodLOW
ExploitabilityCOMPLEX
Tagsassert, python
OWASPA6:2017-Security Misconfiguration, A05:2021-Security Misconfiguration