Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Use of Externally-Controlled Input to Select Classes or Code ('Unsafe Reflection')
This vulnerability occurs when an application uses unvalidated external input, like a URL parameter or form field, to dynamically decide which class to load or which method to execute via…
What is CWE-470?
Real-world CVEs caused by CWE-470
-
Cryptography API uses unsafe reflection when deserializing a private key
-
Database system allows attackers to bypass sandbox restrictions by using the Reflection API.
Parcours de l'attaquant étape par étape
- 1
A common reason that programmers use the reflection API is to implement their own command dispatcher. The following example shows a command dispatcher that does not use reflection:
- 2
A programmer might refactor this code to use reflection as follows:
- 3
The refactoring initially appears to offer a number of advantages. There are fewer lines of code, the if/else blocks have been entirely eliminated, and it is now possible to add new command types without modifying the command dispatcher. However, the refactoring allows an attacker to instantiate any object that implements the Worker interface. If the command dispatcher is still responsible for access control, then whenever programmers create a new class that implements the Worker interface, they must remember to modify the dispatcher's access control code. If they do not modify the access control code, then some Worker classes will not have any access control.
- 4
One way to address this access control problem is to make the Worker object responsible for performing the access control check. An example of the re-refactored code follows:
- 5
Although this is an improvement, it encourages a decentralized approach to access control, which makes it easier for programmers to make access control mistakes. This code also highlights another security problem with using reflection to build a command dispatcher. An attacker can invoke the default constructor for any kind of object. In fact, the attacker is not even constrained to objects that implement the Worker interface; the default constructor for any object in the system can be invoked. If the object does not implement the Worker interface, a ClassCastException will be thrown before the assignment to ao, but if the constructor performs operations that work in the attacker's favor, the damage will already have been done. Although this scenario is relatively benign in simple products, in larger products where complexity grows exponentially it is not unreasonable that an attacker could find a constructor to leverage as part of an attack.
Vulnerable Java
A programmer might refactor this code to use reflection as follows:
String ctl = request.getParameter("ctl");
Class cmdClass = Class.forName(ctl + "Command");
Worker ao = (Worker) cmdClass.newInstance();
ao.doAction(request); Secure Java
A common reason that programmers use the reflection API is to implement their own command dispatcher. The following example shows a command dispatcher that does not use reflection:
String ctl = request.getParameter("ctl");
Worker ao = null;
if (ctl.equals("Add")) {
ao = new AddCommand();
}
else if (ctl.equals("Modify")) {
ao = new ModifyCommand();
}
else {
throw new UnknownActionError();
}
ao.doAction(request); How to prevent CWE-470
- Architecture and Design Refactor your code to avoid using reflection.
- Architecture and Design Do not use user-controlled inputs to select and load classes or code.
- Implementation Apply strict input validation by using allowlists or indirect selection to ensure that the user is only selecting allowable classes or code.
How to detect CWE-470
Plexicus détecte automatiquement CWE-470 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.
Frequently asked questions
Qu'est-ce que CWE-470 ?
This vulnerability occurs when an application uses unvalidated external input, like a URL parameter or form field, to dynamically decide which class to load or which method to execute via reflection. An attacker can manipulate this input to force the application to load unexpected, potentially malicious code.
Quelle est la gravité de CWE-470 ?
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-470 ?
MITRE lists the following affected platforms: Java, PHP, Interpreted.
Comment puis-je prévenir CWE-470 ?
Refactor your code to avoid using reflection. Do not use user-controlled inputs to select and load classes or code.
Comment Plexicus détecte et corrige CWE-470 ?
Le moteur SAST de Plexicus reconnaît la signature de flux de données de CWE-470 à 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-470 ?
MITRE publie la définition canonique à https://cwe.mitre.org/data/definitions/470.html. Vous pouvez également consulter la documentation OWASP et NIST pour des conseils adjacents.
Weaknesses related to CWE-470
Improper Control of Dynamically-Managed Code Resources
This vulnerability occurs when an application fails to properly secure access to code resources that can be created or altered at runtime,…
Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution')
Prototype pollution occurs when an application takes user-supplied input and uses it to improperly modify the properties of a JavaScript…
Deserialization of Untrusted Data
This vulnerability occurs when an application accepts and processes serialized data from an untrusted source without proper validation,…
Improper Control of Dynamically-Identified Variables
This vulnerability occurs when an application fails to properly secure access to variables whose names are determined at runtime, allowing…
Improperly Controlled Modification of Dynamically-Determined Object Attributes
This vulnerability occurs when an application accepts user input that specifies which object attributes or fields to create or update, but…
Improper Control of Generation of Code ('Code Injection')
This vulnerability occurs when an application builds executable code using unvalidated external input, such as user data. Because the…
Further reading
- MITRE — CWE-470 officiel https://cwe.mitre.org/data/definitions/470.html
- Seven Pernicious Kingdoms: A Taxonomy of Software Security Errors https://samate.nist.gov/SSATTM_Content/papers/Seven%20Pernicious%20Kingdoms%20-%20Taxonomy%20of%20Sw%20Security%20Errors%20-%20Tsipenyuk%20-%20Chess%20-%20McGraw.pdf
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.