CWE-395 Base Brouillon

Use of NullPointerException Catch to Detect NULL Pointer Dereference

Using a try-catch block for NullPointerException as a substitute for proper null checks is an anti-pattern. This approach masks the root cause of null pointer dereferences instead of preventing…

Définition

What is CWE-395?

Using a try-catch block for NullPointerException as a substitute for proper null checks is an anti-pattern. This approach masks the root cause of null pointer dereferences instead of preventing them, leading to unstable and difficult-to-debug code.
Developers sometimes catch NullPointerException for three main reasons, but only one is valid. The first, and most problematic, is using the catch block to handle an existing null dereference bug instead of fixing it with proper validation. The second is explicitly throwing the exception to signal an error, which is misleading as it's designed for runtime detection, not control flow. The only acceptable use is within a testing framework that intentionally generates invalid inputs to verify robustness. Relying on exception handling for normal program logic creates fragile code that obscures the actual source of null values. Instead, you should proactively prevent null dereferences by validating arguments, using optional types, or employing safe navigation operators provided by your language. This makes the code's intent clear, improves performance by avoiding expensive exception overhead, and results in more maintainable and predictable software.
Impact réel

Real-world CVEs caused by CWE-395

Aucune référence CVE publique n'est liée à ce CWE dans le catalogue MITRE pour le moment.

Comment les attaquants l'exploitent

Parcours de l'attaquant étape par étape

  1. 1

    Identifier un chemin de code qui traite des entrées non fiables sans validation.

  2. 2

    Élaborer une charge utile qui exploite le comportement non sécurisé — injection, traversal, débordement ou abus de logique.

  3. 3

    Délivrer la charge utile via une requête normale et observer la réaction de l'application.

  4. 4

    Itérer jusqu'à ce que la réponse divulgue des données, exécute le code de l'attaquant ou élève les privilèges.

Exemple de code vulnérable

Vulnerable Java

The following code mistakenly catches a NullPointerException.

Vulnérable Java
try { 
  	 mysteryMethod(); 
   } catch (NullPointerException npe) {
   }
Exemple de code sécurisé

Secure pseudo

Sécurisé pseudo
// Validate, sanitize, or use a safe API before reaching the sink.
function handleRequest(input) {
  const safe = validateAndEscape(input);
  return executeWithGuards(safe);
}
What changed: the unsafe sink is replaced (or the input is validated/escaped) so the same payload no longer triggers the weakness.
Liste de contrôle de prévention

How to prevent CWE-395

  • Architecture and Design / Implementation Do not extensively rely on catching exceptions (especially for validating user input) to handle errors. Handling exceptions can decrease the performance of an application.
Signaux de détection

How to detect CWE-395

Automated Static Analysis - Binary or Bytecode SOAR Partial

According to SOAR [REF-1479], the following detection techniques may be useful: ``` Cost effective for partial coverage: ``` Bytecode Weakness Analysis - including disassembler + source code weakness analysis Binary Weakness Analysis - including disassembler + source code weakness analysis

Dynamic Analysis with Manual Results Interpretation SOAR Partial

According to SOAR [REF-1479], the following detection techniques may be useful: ``` Cost effective for partial coverage: ``` Framework-based Fuzzer

Manual Static Analysis - Source Code SOAR Partial

According to SOAR [REF-1479], the following detection techniques may be useful: ``` Cost effective for partial coverage: ``` Manual Source Code Review (not inspections)

Automated Static Analysis - Source Code High

According to SOAR [REF-1479], the following detection techniques may be useful: ``` Highly cost effective: ``` Source code Weakness Analyzer Context-configured Source Code Weakness Analyzer

Architecture or Design Review High

According to SOAR [REF-1479], the following detection techniques may be useful: ``` Highly cost effective: ``` Formal Methods / Correct-By-Construction ``` Cost effective for partial coverage: ``` Inspection (IEEE 1028 standard) (can apply to requirements, design, source code, etc.)

Correction automatique Plexicus

Plexicus détecte automatiquement CWE-395 et ouvre une PR de correction en moins de 60 secondes.

Codex Remedium analyse chaque commit, identifie cette faiblesse précise et livre une pull request prête à être relue avec le correctif. Pas de tickets. Pas de transferts.

Questions fréquentes

Frequently asked questions

Qu'est-ce que CWE-395 ?

Using a try-catch block for NullPointerException as a substitute for proper null checks is an anti-pattern. This approach masks the root cause of null pointer dereferences instead of preventing them, leading to unstable and difficult-to-debug code.

Quelle est la gravité de CWE-395 ?

MITRE n'a pas publié de note de probabilité d'exploitation pour cette faiblesse. Traitez-la comme un impact moyen jusqu'à ce que votre modèle de menace prouve le contraire.

Quels langages ou plateformes sont affectés par CWE-395 ?

MITRE lists the following affected platforms: Java.

Comment puis-je prévenir CWE-395 ?

Do not extensively rely on catching exceptions (especially for validating user input) to handle errors. Handling exceptions can decrease the performance of an application.

Comment Plexicus détecte et corrige CWE-395 ?

Le moteur SAST de Plexicus reconnaît la signature de flux de données de CWE-395 à chaque commit. Lorsqu'une correspondance est trouvée, notre agent Codex Remedium ouvre une PR de correction avec le code corrigé, les tests et un résumé d'une ligne pour le relecteur.

Où puis-je en savoir plus sur CWE-395 ?

MITRE publie la définition canonique à https://cwe.mitre.org/data/definitions/395.html. Vous pouvez également consulter la documentation OWASP et NIST pour des conseils adjacents.

Faiblesses associées

Weaknesses related to CWE-395

CWE-705 Parent

Incorrect Control Flow Scoping

This vulnerability occurs when a program fails to return execution to the correct point in the code after finishing a specific operation…

CWE-248 Frère

Uncaught Exception

This vulnerability occurs when a function throws an error or exception, but the calling code does not have a proper handler to catch and…

CWE-382 Frère

J2EE Bad Practices: Use of System.exit()

This vulnerability occurs when a J2EE application directly calls System.exit(), which forcibly terminates the entire application server…

CWE-396 Frère

Declaration of Catch for Generic Exception

This weakness occurs when code catches a generic exception type like 'Exception' or 'Throwable', which can hide specific errors and create…

CWE-397 Frère

Declaration of Throws for Generic Exception

This vulnerability occurs when a method is declared to throw an overly broad exception type, such as a generic 'Exception' or 'Throwable'.…

CWE-455 Frère

Non-exit on Failed Initialization

This vulnerability occurs when software continues to run as normal after encountering a critical security failure during its startup…

CWE-584 Frère

Return Inside Finally Block

This vulnerability occurs when a function places a return statement inside a finally block. This dangerous pattern silently discards any…

CWE-698 Frère

Execution After Redirect (EAR)

Execution After Redirect (EAR) occurs when a web application sends a redirect response to a user's browser but continues to run…

Prêt quand vous l'êtes

Arrêtez de payer par développeur.
Commencez à fermer la boucle.

Plexicus est l'ASPM natif IA qui scanne, filtre, corrige, penteste et explique — de façon autonome. Développeurs illimités, dépôts illimités, actions IA à usage équitable. Vrai niveau gratuit, €269/mo annuel quand vous êtes prêt.