# Improper Handling of Exceptional Conditions (CWE-755) The product does not handle or incorrectly handles an exceptional condition. **Stack:** JavaScript - Prevalence: 中 覆盖 3 种语言 - Impact: 高 1 条严重级别为高的规则 - Prevention: 已记录 4 个修复示例 **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 基于 1 条 Shoulder 检测规则的 Improper Handling of Exceptional Conditions 预防策略。 ### JavaScript Use finally blocks to release resources (connections, file handles) on all code paths ## Warning Signs - [MEDIUM] Resource at ... may not be released when exceptions occur - [MEDIUM] code that allocates resources (files, connections, memory) within try blocks but fails to release th ## Consequences - 拒绝服务 (DoS) - 读取应用程序数据 - 执行未授权代码 ## Mitigations - 预见所有潜在的异常情况并妥善处理 - 使用 try-catch 块和适当的错误处理机制 - 在发生异常时以安全的方式失败 ## Detection - Total rules: 4 - Languages: go, javascript, typescript, python ## Rules by Language ### Javascript (1 rules) - **Resource Exhaustion via Exception Handling** [MEDIUM]: Detects code that allocates resources (files, connections, memory) within try blocks but fails to release them in finally blocks or error paths. When exceptions occur, resources may not be properly cleaned up, leading to resource exhaustion, memory leaks, and denial of service. - Remediation: Use finally blocks or try-with-resources pattern: ```javascript // ✅ SAFE - Cleanup in finally let connection; try { connection = await db.getConnection(); await connection.query(sql); } catch (error) { logger.error('Query failed:', error); throw error; } finally { if (connection) { await connection.release(); } } ``` ### Typescript (1 rules) - **Resource Exhaustion via Exception Handling** [MEDIUM]: Detects code that allocates resources (files, connections, memory) within try blocks but fails to release them in finally blocks or error paths. When exceptions occur, resources may not be properly cleaned up, leading to resource exhaustion, memory leaks, and denial of service. - Remediation: Use finally blocks or try-with-resources pattern: ```javascript // ✅ SAFE - Cleanup in finally let connection; try { connection = await db.getConnection(); await connection.query(sql); } catch (error) { logger.error('Query failed:', error); throw error; } finally { if (connection) { await connection.release(); } } ```