Uncaught Exceptions and Improper Recovery
CWE-248, CWE-252, CWE-390, CWE-391, CWE-394, CWE-396, CWE-397, CWE-460, CWE-703, CWE-754, CWE-755
Error handling failures occur when applications don't properly catch, handle, or recover from exceptions and error conditions. This includes:
Uncaught Exception
Unchecked Return Value
Detection of Error Condition Without Action
Unchecked Error Condition
Unexpected Status Code or Return Value
Declaration of Catch for Generic Exception
Declaration of Throws for Generic Exception
Improper Cleanup on Thrown Exception
Improper Check or Handling of Exceptional Conditions
Improper Check for Unusual or Exceptional Conditions
Improper Handling of Exceptional Conditions
When exceptions aren't handled, applications crash, leak information, enter inconsistent states, or allow attacks to proceed unchecked.
Real-World Attack Scenarios
Scenario 1: Uncaught Exception Leading to Crash (DoS)
An application processes user file uploads without exception handling:
The vulnerability:
No exception handling. Any error crashes the application:
Invalid JSON →
json.JSONDecodeErrorMissing key →
KeyErrorDatabase error →
DatabaseException
The attack:
Attacker sends malformed JSON:
Application crashes:
Result:
Application crashes
Service unavailable (DoS)
User requests fail
Revenue impact
The fix:
Catch and handle exceptions:
Finding it: Send malformed input. Try to crash the application. Monitor for exceptions in logs.
Scenario 2: Unchecked Return Value (Silent Failure)
An application doesn't check if critical operations succeeded:
The vulnerability:
Return values not checked. Operations might fail silently:
Database insert fails → User not created, but response says success
Email fails → User not notified, but response says email sent
Cache fails → Cache inconsistent with database
The attack:
Attacker creates 1000 accounts in rapid succession. Database transaction pool exhausted, insert fails:
Response: "User created successfully"
Reality: User not actually created
Attacker receives success notification but account doesn't exist
Next login attempt fails (unexpected)
Or worse, if database fails randomly due to load:
Some users created, some not
Application thinks all created
Database state corrupted
Result:
Inconsistent state
Data corruption
User confusion
Security checks bypassed (user created according to app, but not in DB)
The fix:
Check all return values:
Finding it: Check code for function calls where return value is ignored. Look for operations that might fail but assume success. Test with resource exhaustion.
Scenario 3: Generic Exception Catching (Hiding Real Errors)
Application catches all exceptions generically:
The vulnerability:
Catching Exception broadly hides all errors:
Payment fails → Returns success anyway
Invalid card → Returns success
Network error → Returns success
Timeout → Returns success
The attack:
Attacker:
Sends payment request with invalid card
Payment fails with exception
Exception caught generically
Application returns "success"
Attacker charged? Not charged? Unknown!
Customer charged multiple times or not at all
Result:
Financial inconsistency
Revenue loss
Customer charge disputes
Payment system confused
The fix:
Catch specific exceptions:
Finding it: Search for bare except: or except Exception:. Look for code that catches broad exceptions and doesn't log/handle them.
Scenario 4: Missing Cleanup on Exception (Resource Leak)
File not closed if exception occurs:
The vulnerability:
If exception occurs before file.close():
File descriptor remains open
Resource leaked
Repeated calls leak more file descriptors
Eventually: "Too many open files" error
Application crashes (DoS)
The attack:
Attacker triggers the exception repeatedly:
Result:
Resource exhaustion
Denial of service
Application crash
The fix:
Use try/finally or context manager:
Finding it: Look for files/resources opened without try/finally or context managers. Check for multiple cleanup statements (not guaranteed if exception occurs).
Scenario 5: Unchecked Error, Code Continues (Privilege Escalation)
Critical security check fails but code continues:
The vulnerability:
If check_if_admin() raises exception:
Exception propagates
Code continues execution
Admin action performed despite failed check
Security control bypassed
The attack:
Attacker triggers exception in admin check:
Result:
Privilege escalation
Unauthorized admin actions
Data deletion
Complete security bypass
The fix:
Catch exceptions and fail securely:
Finding it: Look for security checks that aren't wrapped in try/except. Check if authorization can fail silently. Test with invalid input to authorization functions.
Scenario 6: Unexpected Status Code Not Checked
API call doesn't check status code:
The vulnerability:
Status code not checked:
404 → User not found, but continues
401 → Authentication failed, but continues
500 → Server error, but continues
403 → Forbidden, but continues
The attack:
Attacker requests data for user they shouldn't access:
External API returns 403 Forbidden:
Application code continues anyway:
Result:
Crash or undefined behavior
Data access validation bypassed
Authorization checks ignored
The fix:
Check status codes:
Finding it: Search for response.json() without status code check. Look for API calls not validating response. Test with error responses.
Mitigation Strategies
Catch specific exceptions
Check all return values
Use finally for cleanup
Or use context managers
Fail securely
Log all errors
Test error paths
Unit tests for error cases
Integration tests for API failures
Fuzz testing with invalid input
Load testing to find resource leak.
Last updated