# Improper Handling of Exceptional Conditions (CWE-755) The product does not handle or incorrectly handles an exceptional condition. **Stack:** Python - Prevalence: Media 3 lenguajes cubiertos - Impact: Alto 1 reglas de severidad alta - Prevention: Documentada 4 ejemplos de corrección **OWASP:** Insecure Design (A04:2021-Insecure Design) - #4 ## Description When exceptional conditions are not properly handled, the product may enter an undefined state, crash, or expose sensitive information. This can lead to denial of service, information disclosure, or unexpected behavior. ## Prevention Estrategias de prevención para Improper Handling of Exceptional Conditions basadas en 2 reglas de detección de Shoulder. ### Python Return error responses when security checks fail instead of continuing execution Wrap database, file, network, and API operations in try/except with proper logging ## Warning Signs - [HIGH] security checks (authentication, authorization, validation) inside try/except blocks that return suc - [MEDIUM] critical operations (database, file I/O, network calls, external APIs) that lack proper exception ha ## Consequences - DoS - Leer datos de la aplicación - Ejecutar código no autorizado ## Mitigations - Anticipa todas las condiciones excepcionales potenciales y manéjalas adecuadamente - Usa bloques try-catch y mecanismos adecuados de manejo de errores - Falla de forma segura cuando ocurra una excepción ## Detection - Total rules: 4 - Languages: go, javascript, typescript, python ## Rules by Language ### Python (2 rules) - **Security Check Failing Open** [HIGH]: Detects security checks (authentication, authorization, validation) inside try/except blocks that return success on exception. This causes the system to "fail open" - granting access when security checks fail. - Remediation: Return an error response when security checks fail instead of continuing execution. ```python from flask import request, abort @app.route('/api/admin') def admin_endpoint(): try: user = authenticate(request.headers.get('Authorization')) check_admin_permission(user) except (AuthenticationError, PermissionError): abort(403) # Fail closed - deny access return {'data': get_admin_data()} ``` Learn more: https://shoulder.dev/learn/python/cwe-755/failing-open - **Missing Exception Handling in Critical Operations** [MEDIUM]: Detects critical operations (database, file I/O, network calls, external APIs) that lack proper exception handling. Uncaught exceptions can crash the application, leak sensitive information, or leave the system in an inconsistent state. - Remediation: Wrap database, file, network, and API operations in try/except blocks. ```python import logging import requests logger = logging.getLogger(__name__) def fetch_data(url): try: response = requests.get(url, timeout=5) response.raise_for_status() return response.json() except requests.RequestException as e: logger.error(f"Request failed: {e}") return None ``` Learn more: https://shoulder.dev/learn/python/cwe-755/uncaught-exception