{"@ID": "470", "@Name": "Use of Externally-Controlled Input to Select Classes or Code ('Unsafe Reflection')", "@Abstraction": "Base", "@Structure": "Simple", "@Status": "Draft", "Description": "The product uses external input with reflection to select which classes or code to use, but it does not sufficiently prevent the input from selecting improper classes or code.", "Extended_Description": "If the product uses external inputs to determine which class to instantiate or which method to invoke, then an attacker could supply values to select unexpected classes or methods. If this occurs, then the attacker could create control flow paths that were not intended by the developer. These paths could bypass authentication or access control checks, or otherwise cause the product to behave in an unexpected manner. This situation becomes a doomsday scenario if the attacker can upload files into a location that appears on the product's classpath (CWE-427) or add new entries to the product's classpath (CWE-426). Under either of these conditions, the attacker can use reflection to introduce new, malicious behavior into the product.", "Related_Weaknesses": {"Related_Weakness": [{"@Nature": "ChildOf", "@CWE_ID": "913", "@View_ID": "1000", "@Ordinal": "Primary"}, {"@Nature": "ChildOf", "@CWE_ID": "913", "@View_ID": "1003", "@Ordinal": "Primary"}, {"@Nature": "ChildOf", "@CWE_ID": "610", "@View_ID": "1000"}, {"@Nature": "ChildOf", "@CWE_ID": "20", "@View_ID": "700", "@Ordinal": "Primary"}]}, "Weakness_Ordinalities": {"Weakness_Ordinality": {"Ordinality": "Primary"}}, "Applicable_Platforms": {"Language": [{"@Name": "Java", "@Prevalence": "Undetermined"}, {"@Name": "PHP", "@Prevalence": "Undetermined"}, {"@Class": "Interpreted", "@Prevalence": "Sometimes"}]}, "Alternate_Terms": {"Alternate_Term": {"Term": "Reflection Injection"}}, "Modes_Of_Introduction": {"Introduction": [{"Phase": "Architecture and Design"}, {"Phase": "Implementation"}]}, "Common_Consequences": {"Consequence": [{"Scope": ["Integrity", "Confidentiality", "Availability", "Other"], "Impact": ["Execute Unauthorized Code or Commands", "Alter Execution Logic"], "Note": "The attacker might be able to execute code that is not directly accessible to the attacker. Alternately, the attacker could call unexpected code in the wrong place or the wrong time, possibly modifying critical system state."}, {"Scope": ["Availability", "Other"], "Impact": ["DoS: Crash, Exit, or Restart", "Other"], "Note": "The attacker might be able to use reflection to call the wrong code, possibly with unexpected arguments that violate the API (CWE-227). This could cause the product to exit or hang."}, {"Scope": "Confidentiality", "Impact": "Read Application Data", "Note": "By causing the wrong code to be invoked, the attacker might be able to trigger a runtime error that leaks sensitive information in the error message, such as CWE-536."}]}, "Detection_Methods": {"Detection_Method": {"@Detection_Method_ID": "DM-14", "Method": "Automated Static Analysis", "Description": "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.)", "Effectiveness": "High"}}, "Potential_Mitigations": {"Mitigation": [{"Phase": "Architecture and Design", "Description": "Refactor your code to avoid using reflection."}, {"Phase": "Architecture and Design", "Description": "Do not use user-controlled inputs to select and load classes or code."}, {"Phase": "Implementation", "Description": "Apply strict input validation by using allowlists or indirect selection to ensure that the user is only selecting allowable classes or code."}]}, "Demonstrative_Examples": {"Demonstrative_Example": {"@Demonstrative_Example_ID": "DX-232", "Intro_Text": "A common reason that programmers use the\n               reflection API is to implement their own command\n               dispatcher. The following example shows a command\n               dispatcher that does not use reflection:", "Example_Code": [{"@Nature": "Good", "@Language": "Java", "xhtml:div": {"xhtml:br": [null, null, null, null, null], "xhtml:div": [{"@style": "margin-left:1em;", "#text": "ao = new AddCommand();"}, {"@style": "margin-left:1em;", "#text": "ao = new ModifyCommand();"}, {"@style": "margin-left:1em;", "#text": "throw new UnknownActionError();"}], "#text": "String ctl = request.getParameter(\"ctl\");\n\t\t\t\t  Worker ao = null;\n\t\t\t\t  if (ctl.equals(\"Add\")) {\n\t\t\t\t  \n\t\t\t\t  }\n\t\t\t\t  else if (ctl.equals(\"Modify\")) {\n\t\t\t\t  \n\t\t\t\t  }\n\t\t\t\t  else {\n\t\t\t\t  }\n\t\t\t\t  \n\t\t\t\t  ao.doAction(request);"}}, {"@Nature": "Bad", "@Language": "Java", "xhtml:div": {"xhtml:br": [null, null, null], "#text": "String ctl = request.getParameter(\"ctl\");\n\t\t\t\t\tClass cmdClass = Class.forName(ctl + \"Command\");\n\t\t\t\t\tWorker ao = (Worker) cmdClass.newInstance();\n\t\t\t\t\tao.doAction(request);"}}, {"@Nature": "Bad", "@Language": "Java", "xhtml:div": {"xhtml:br": [null, null, null, null], "#text": "String ctl = request.getParameter(\"ctl\");\n\t\t\t\t   Class cmdClass = Class.forName(ctl + \"Command\");\n\t\t\t\t   Worker ao = (Worker) cmdClass.newInstance();\n\t\t\t\t   ao.checkAccessControl(request);\n\t\t\t\t   ao.doAction(request);"}}], "Body_Text": ["A programmer might refactor this code to use reflection as follows:", {"xhtml:p": ["The refactoring initially appears to offer a\n\t\t\t\t number of advantages. There are fewer lines of code,\n\t\t\t\t the if/else blocks have been entirely eliminated, and\n\t\t\t\t it is now possible to add new command types without\n\t\t\t\t modifying the command dispatcher.", "However, the refactoring allows an attacker to\n\t\t\t\t instantiate any object that implements the Worker\n\t\t\t\t interface. If the command dispatcher is still\n\t\t\t\t responsible for access control, then whenever\n\t\t\t\t programmers create a new class that implements the\n\t\t\t\t Worker interface, they must remember to modify the\n\t\t\t\t dispatcher's access control code. If they do not modify\n\t\t\t\t the access control code, then some Worker classes will\n\t\t\t\t not have any access control."]}, "One way to address this access control\n               problem is to make the Worker object responsible for\n               performing the access control check. An example of the\n               re-refactored code follows:", "Although this is an improvement, it\n               encourages a decentralized approach to access control,\n               which makes it easier for programmers to make access\n               control mistakes.", "This code also highlights another security\n\t\t\t   problem with using reflection to build a command\n\t\t\t   dispatcher. An attacker can invoke the default\n\t\t\t   constructor for any kind of object. In fact, the\n\t\t\t   attacker is not even constrained to objects that\n\t\t\t   implement the Worker interface; the default constructor\n\t\t\t   for any object in the system can be invoked. If the\n\t\t\t   object does not implement the Worker interface, a\n\t\t\t   ClassCastException will be thrown before the assignment\n\t\t\t   to ao, but if the constructor performs operations that\n\t\t\t   work in the attacker's favor, the damage will already\n\t\t\t   have been done. Although this scenario is relatively\n\t\t\t   benign in simple products, in larger products where\n\t\t\t   complexity grows exponentially, it is not unreasonable\n\t\t\t   that an attacker could find a constructor to leverage\n\t\t\t   as part of an attack."]}}, "Observed_Examples": {"Observed_Example": [{"Reference": "CVE-2018-1000613", "Description": "Cryptography API uses unsafe reflection when deserializing a private key", "Link": "https://www.cve.org/CVERecord?id=CVE-2018-1000613"}, {"Reference": "CVE-2004-2331", "Description": "Database system allows attackers to bypass sandbox restrictions by using the Reflection API.", "Link": "https://www.cve.org/CVERecord?id=CVE-2004-2331"}]}, "Taxonomy_Mappings": {"Taxonomy_Mapping": [{"@Taxonomy_Name": "7 Pernicious Kingdoms", "Entry_Name": "Unsafe Reflection"}, {"@Taxonomy_Name": "The CERT Oracle Secure Coding Standard for Java (2011)", "Entry_ID": "SEC06-J", "Entry_Name": "Do not use reflection to increase accessibility of classes, methods, or fields"}]}, "Related_Attack_Patterns": {"Related_Attack_Pattern": {"@CAPEC_ID": "138"}}, "References": {"Reference": {"@External_Reference_ID": "REF-6"}}, "Mapping_Notes": {"Usage": "Allowed", "Rationale": "This CWE entry is at the Base level of abstraction, which is a preferred level of abstraction for mapping to the root causes of vulnerabilities.", "Comments": "Carefully read both the name and description to ensure that this mapping is an appropriate fit. Do not try to 'force' a mapping to a lower-level Base/Variant simply to comply with this preferred level of abstraction.", "Reasons": {"Reason": {"@Type": "Acceptable-Use"}}}, "Content_History": {"Submission": {"Submission_Name": "7 Pernicious Kingdoms", "Submission_Date": "2006-07-19", "Submission_Version": "Draft 3", "Submission_ReleaseDate": "2006-07-19"}, "Modification": [{"Modification_Name": "Eric Dalci", "Modification_Organization": "Cigital", "Modification_Date": "2008-07-01", "Modification_Version": "1.0", "Modification_ReleaseDate": "2008-09-09", "Modification_Comment": "updated Potential_Mitigations, Time_of_Introduction"}, {"Modification_Organization": "KDM Analytics", "Modification_Date": "2008-08-01", "Modification_Version": "1.0", "Modification_ReleaseDate": "2008-09-09", "Modification_Comment": "added/updated white box definitions"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2008-09-08", "Modification_Version": "1.0", "Modification_ReleaseDate": "2008-09-09", "Modification_Comment": "updated Description, Relationships, Other_Notes, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2008-10-14", "Modification_Version": "1.0.1", "Modification_ReleaseDate": "2008-10-14", "Modification_Comment": "updated Applicable_Platforms, Demonstrative_Examples, Description, Other_Notes"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2009-01-12", "Modification_Version": "1.2", "Modification_ReleaseDate": "2009-01-12", "Modification_Comment": "updated Applicable_Platforms, Common_Consequences, Demonstrative_Examples, Observed_Examples, Potential_Mitigations"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2009-05-27", "Modification_Version": "1.4", "Modification_ReleaseDate": "2009-05-27", "Modification_Comment": "updated Demonstrative_Examples, Name"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2009-10-29", "Modification_Version": "1.6", "Modification_ReleaseDate": "2009-10-29", "Modification_Comment": "updated Alternate_Terms, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2011-03-29", "Modification_Version": "1.12", "Modification_ReleaseDate": "2011-03-30", "Modification_Comment": "updated Demonstrative_Examples"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2011-06-01", "Modification_Version": "1.13", "Modification_ReleaseDate": "2011-06-01", "Modification_Comment": "updated Common_Consequences, Relationships, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2012-05-11", "Modification_Version": "2.2", "Modification_ReleaseDate": "2012-05-15", "Modification_Comment": "updated Relationships, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2013-02-21", "Modification_Version": "2.4", "Modification_ReleaseDate": "2013-02-21", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2014-07-30", "Modification_Version": "2.8", "Modification_ReleaseDate": "2014-07-31", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2017-11-08", "Modification_Version": "3.0", "Modification_ReleaseDate": "2017-11-08", "Modification_Comment": "updated White_Box_Definitions"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2019-01-03", "Modification_Version": "3.2", "Modification_ReleaseDate": "2019-01-03", "Modification_Comment": "updated Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2019-06-20", "Modification_Version": "3.3", "Modification_ReleaseDate": "2019-06-20", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2020-02-24", "Modification_Version": "4.0", "Modification_ReleaseDate": "2020-02-24", "Modification_Comment": "updated References, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2020-06-25", "Modification_Version": "4.1", "Modification_ReleaseDate": "2020-06-25", "Modification_Comment": "updated Potential_Mitigations"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2021-10-28", "Modification_Version": "4.6", "Modification_ReleaseDate": "2021-10-28", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-01-31", "Modification_Version": "4.10", "Modification_ReleaseDate": "2023-01-31", "Modification_Comment": "updated Common_Consequences, Demonstrative_Examples, Description, Related_Attack_Patterns, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-04-27", "Modification_Version": "4.11", "Modification_ReleaseDate": "2023-04-27", "Modification_Comment": "updated Detection_Factors, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-06-29", "Modification_Version": "4.12", "Modification_ReleaseDate": "2023-06-29", "Modification_Comment": "updated Mapping_Notes"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-10-26", "Modification_Version": "4.13", "Modification_ReleaseDate": "2023-10-26", "Modification_Comment": "updated Observed_Examples"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2025-12-11", "Modification_Version": "4.19", "Modification_ReleaseDate": "2025-12-11", "Modification_Comment": "updated Demonstrative_Examples, Relationships, Weakness_Ordinalities"}], "Previous_Entry_Name": [{"@Date": "2008-04-11", "#text": "Unsafe Reflection"}, {"@Date": "2009-05-27", "#text": "Use of Externally-Controlled Input to Select Classes or Code (aka 'Unsafe Reflection')"}]}}
