<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Operand ABAP_SOURCE_CODE in statement GENERATE is an ABAP command injection risk in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/operand-abap-source-code-in-statement-generate-is-an-abap-command-injection/m-p/12803894#M2025117</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
  &lt;P&gt;This is a question which I have solved (I'm posting an answer), but I would be interested by any complement how you would possibly best solve it, or any complement to my answer.&lt;/P&gt;
  &lt;P&gt;ATC/Code Inspector returns the following &lt;STRONG&gt;normal&lt;/STRONG&gt; security error (details posted below) concerning this line of code:&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;        " BEFORE ARE CUSTOM SECURITY CHECKS
        " (authorization check based on the MD5 hash key of the ABAP source code)

        GENERATE SUBROUTINE POOL abap_source_code
          ...&lt;/CODE&gt;&lt;/PRE&gt; My program gets the ABAP source code from an external source, but makes sure it's valid by doing an authorization check based on the MD5 hash key of this code.The security error mentions to use CL_ABAP_DYN_PRG to validate the contents of "abap_source_code", but here it's an internal table, usually CL_ABAP_DYN_PRG works on scalar variables (check against a white list, check that the text is correctly quoted, etc.)How to do it when the concerned variable is an internal table?I'm using ABAP 7.56.Thanks a lot!SandraError details: 
  &lt;H3&gt;Appl. Comp. Check / Check Class / Message Code&lt;/H3&gt;
  &lt;P&gt;BC-ABA-LA-EPC / CL_CI_TEST_EXTENDED_CHECK_SEC / 1108&lt;/P&gt;
  &lt;H3&gt;Details of Analysis&lt;/H3&gt;
  &lt;UL&gt; 
   &lt;LI&gt;Operand ABAP_SOURCE_CODE in statement GENERATE is an ABAP command injection risk.&lt;/LI&gt; 
   &lt;LI&gt;Data flow:&lt;/LI&gt; 
   &lt;LI&gt;Return code of method call: GET_ABAP_SOURCE_CODE (METH INITIALIZE [20])&lt;/LI&gt; 
   &lt;LI&gt;ABAP_SOURCE_CODE -&amp;gt; ABAP_SOURCE_CODE (Method: INITIALIZE Line: 52)&lt;/LI&gt; 
   &lt;LI&gt;Cannot be suppressed using a pragma or pseudo-comment&lt;/LI&gt; 
   &lt;LI&gt;Additional Info: Data Source: CALL (Procedure)&lt;/LI&gt; 
  &lt;/UL&gt;
  &lt;H3&gt;What is checked?&lt;/H3&gt;
  &lt;H3&gt;Potential injection of harmful code in the statements &lt;CODE&gt;INSERT REPORT&lt;/CODE&gt; and &lt;CODE&gt;GENERATE SUBROUTINE POOL&lt;/CODE&gt;&lt;/H3&gt;
  &lt;P&gt;Message number 1108&lt;/P&gt;
  &lt;P&gt;Security problems can occur wherever external data (such as user input) is processed further without being checked.&lt;/P&gt;
  &lt;P&gt;The statements INSERT REPORT and GENERATE SUBROUTINE POOL are used to generate ABAP programs dynamically, which can then be executed. If user input is entered directly in the source code of these generated programs, an attacker could potentially execute any of the operations in the system. These are known as &lt;STRONG&gt;ABAP command injections&lt;/STRONG&gt;.&lt;/P&gt;
  &lt;H3&gt;Procedure&lt;/H3&gt;
  &lt;P&gt;Dynamic generation of ABAP code always carries a high level of risk to security. First, always check whether other dynamic programming methods can be used instead. If dynamic generations are absolutely necessary, all input data must be checked separately and appropriately.&lt;/P&gt;
  &lt;P&gt;The class &lt;CODE&gt;CL_ABAP_DYN_PRG&lt;/CODE&gt; can be used to implement input checks as described in Validation by Methods of CL_ABAP_DYN_PRG. (The individual methods in the class &lt;CODE&gt;CL_ABAP_DYN_PRG&lt;/CODE&gt; became available in different Support Packages or SAP Notes. SAP Note &lt;A rel="noopener noreferrer" href="http://service.sap.com/sap/support/notes/1852318" target="_blank"&gt;1852318&lt;/A&gt; provides an overview of these methods.) In the case in question, the following methods of this class are viewed as sufficient by the automated check (if the &lt;CODE&gt;RETURNING&lt;/CODE&gt; parameter of the method in question is used in further processing instead of the original input value):&lt;/P&gt;
  &lt;OL&gt; 
   &lt;LI&gt;&lt;CODE&gt;ESCAPE_QUOTES&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;ESCAPE_QUOTES_STR&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;QUOTE&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;QUOTE_STR&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_CHAR_LITERAL&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_STRING_LITERAL&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_INT_VALUE&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_VARIABLE_NAME&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_COLUMN_NAME&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_TABLE_OR_VIEW_NAME_STR&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_TABLE_OR_VIEW_NAME_TAB&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_TABLE_NAME_STR&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_TABLE_NAME_TAB&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_WHITELIST_STR&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_WHITELIST_TAB&lt;/CODE&gt;&lt;/LI&gt; 
  &lt;/OL&gt;
  &lt;P&gt;Checks on the merged ABAP code passed to the statements INSERT REPORT or GENERATE SUBROUTINE POOL are not feasible.&lt;/P&gt;
  &lt;P&gt;Secure data sources can also be displayed using the report (report RSLIN_SEC_DISPLAY_BADIS) RSLIN_SEC_DISPLAY_BADIS.&lt;/P&gt;
  &lt;P&gt;If the source code position in question does not have any security problems and there is no point in modifying the source code, an exemption should be requested in ATC.&lt;/P&gt;
  &lt;H3&gt;How is the check done?&lt;/H3&gt;
  &lt;P&gt;A local data flow analysis is performed.&lt;/P&gt;</description>
    <pubDate>Mon, 06 Nov 2023 10:00:42 GMT</pubDate>
    <dc:creator>Sandra_Rossi</dc:creator>
    <dc:date>2023-11-06T10:00:42Z</dc:date>
    <item>
      <title>Operand ABAP_SOURCE_CODE in statement GENERATE is an ABAP command injection risk</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/operand-abap-source-code-in-statement-generate-is-an-abap-command-injection/m-p/12803894#M2025117</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
  &lt;P&gt;This is a question which I have solved (I'm posting an answer), but I would be interested by any complement how you would possibly best solve it, or any complement to my answer.&lt;/P&gt;
  &lt;P&gt;ATC/Code Inspector returns the following &lt;STRONG&gt;normal&lt;/STRONG&gt; security error (details posted below) concerning this line of code:&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;        " BEFORE ARE CUSTOM SECURITY CHECKS
        " (authorization check based on the MD5 hash key of the ABAP source code)

        GENERATE SUBROUTINE POOL abap_source_code
          ...&lt;/CODE&gt;&lt;/PRE&gt; My program gets the ABAP source code from an external source, but makes sure it's valid by doing an authorization check based on the MD5 hash key of this code.The security error mentions to use CL_ABAP_DYN_PRG to validate the contents of "abap_source_code", but here it's an internal table, usually CL_ABAP_DYN_PRG works on scalar variables (check against a white list, check that the text is correctly quoted, etc.)How to do it when the concerned variable is an internal table?I'm using ABAP 7.56.Thanks a lot!SandraError details: 
  &lt;H3&gt;Appl. Comp. Check / Check Class / Message Code&lt;/H3&gt;
  &lt;P&gt;BC-ABA-LA-EPC / CL_CI_TEST_EXTENDED_CHECK_SEC / 1108&lt;/P&gt;
  &lt;H3&gt;Details of Analysis&lt;/H3&gt;
  &lt;UL&gt; 
   &lt;LI&gt;Operand ABAP_SOURCE_CODE in statement GENERATE is an ABAP command injection risk.&lt;/LI&gt; 
   &lt;LI&gt;Data flow:&lt;/LI&gt; 
   &lt;LI&gt;Return code of method call: GET_ABAP_SOURCE_CODE (METH INITIALIZE [20])&lt;/LI&gt; 
   &lt;LI&gt;ABAP_SOURCE_CODE -&amp;gt; ABAP_SOURCE_CODE (Method: INITIALIZE Line: 52)&lt;/LI&gt; 
   &lt;LI&gt;Cannot be suppressed using a pragma or pseudo-comment&lt;/LI&gt; 
   &lt;LI&gt;Additional Info: Data Source: CALL (Procedure)&lt;/LI&gt; 
  &lt;/UL&gt;
  &lt;H3&gt;What is checked?&lt;/H3&gt;
  &lt;H3&gt;Potential injection of harmful code in the statements &lt;CODE&gt;INSERT REPORT&lt;/CODE&gt; and &lt;CODE&gt;GENERATE SUBROUTINE POOL&lt;/CODE&gt;&lt;/H3&gt;
  &lt;P&gt;Message number 1108&lt;/P&gt;
  &lt;P&gt;Security problems can occur wherever external data (such as user input) is processed further without being checked.&lt;/P&gt;
  &lt;P&gt;The statements INSERT REPORT and GENERATE SUBROUTINE POOL are used to generate ABAP programs dynamically, which can then be executed. If user input is entered directly in the source code of these generated programs, an attacker could potentially execute any of the operations in the system. These are known as &lt;STRONG&gt;ABAP command injections&lt;/STRONG&gt;.&lt;/P&gt;
  &lt;H3&gt;Procedure&lt;/H3&gt;
  &lt;P&gt;Dynamic generation of ABAP code always carries a high level of risk to security. First, always check whether other dynamic programming methods can be used instead. If dynamic generations are absolutely necessary, all input data must be checked separately and appropriately.&lt;/P&gt;
  &lt;P&gt;The class &lt;CODE&gt;CL_ABAP_DYN_PRG&lt;/CODE&gt; can be used to implement input checks as described in Validation by Methods of CL_ABAP_DYN_PRG. (The individual methods in the class &lt;CODE&gt;CL_ABAP_DYN_PRG&lt;/CODE&gt; became available in different Support Packages or SAP Notes. SAP Note &lt;A rel="noopener noreferrer" href="http://service.sap.com/sap/support/notes/1852318" target="_blank"&gt;1852318&lt;/A&gt; provides an overview of these methods.) In the case in question, the following methods of this class are viewed as sufficient by the automated check (if the &lt;CODE&gt;RETURNING&lt;/CODE&gt; parameter of the method in question is used in further processing instead of the original input value):&lt;/P&gt;
  &lt;OL&gt; 
   &lt;LI&gt;&lt;CODE&gt;ESCAPE_QUOTES&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;ESCAPE_QUOTES_STR&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;QUOTE&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;QUOTE_STR&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_CHAR_LITERAL&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_STRING_LITERAL&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_INT_VALUE&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_VARIABLE_NAME&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_COLUMN_NAME&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_TABLE_OR_VIEW_NAME_STR&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_TABLE_OR_VIEW_NAME_TAB&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_TABLE_NAME_STR&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_TABLE_NAME_TAB&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_WHITELIST_STR&lt;/CODE&gt;&lt;/LI&gt; 
   &lt;LI&gt;&lt;CODE&gt;CHECK_WHITELIST_TAB&lt;/CODE&gt;&lt;/LI&gt; 
  &lt;/OL&gt;
  &lt;P&gt;Checks on the merged ABAP code passed to the statements INSERT REPORT or GENERATE SUBROUTINE POOL are not feasible.&lt;/P&gt;
  &lt;P&gt;Secure data sources can also be displayed using the report (report RSLIN_SEC_DISPLAY_BADIS) RSLIN_SEC_DISPLAY_BADIS.&lt;/P&gt;
  &lt;P&gt;If the source code position in question does not have any security problems and there is no point in modifying the source code, an exemption should be requested in ATC.&lt;/P&gt;
  &lt;H3&gt;How is the check done?&lt;/H3&gt;
  &lt;P&gt;A local data flow analysis is performed.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 10:00:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/operand-abap-source-code-in-statement-generate-is-an-abap-command-injection/m-p/12803894#M2025117</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2023-11-06T10:00:42Z</dc:date>
    </item>
    <item>
      <title>Re: Operand ABAP_SOURCE_CODE in statement GENERATE is an ABAP command injection risk</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/operand-abap-source-code-in-statement-generate-is-an-abap-command-injection/m-p/12803895#M2025118</link>
      <description>&lt;P&gt;&lt;STRONG&gt;The below solution is to be taken with a lot of precaution &lt;/STRONG&gt;because, as I said, my program has some very specific security checks (authorization check based on the MD5 hash key of the code) which make CL_ABAP_DYN_PRG not relevant.&lt;/P&gt;&lt;P&gt;The following code does a &lt;STRONG&gt;DUMMY&lt;/STRONG&gt; check, and the ATC/Code Inspector security error doesn't appear anymore:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;        " BEFORE ARE CUSTOM SECURITY CHECKS
        " (authorization check based on the MD5 hash key of the ABAP source code)

        " DUMMY CHECK to bypass ATC/Code Inspector security error
        DATA(verified_abap_source_code) = VALUE string_table( ).
        LOOP AT abap_source_code REFERENCE INTO DATA(abap_line).
          TRY.
              DATA(verified_abap_line) = cl_abap_dyn_prg=&amp;gt;check_whitelist_tab(
                              val       = abap_line-&amp;gt;*
                              whitelist = VALUE #( ( condense( abap_line-&amp;gt;* ) ) ) ).
            CATCH cx_abap_not_in_whitelist INTO DATA(error).
              RAISE EXCEPTION error. " do what you want
          ENDTRY.
          INSERT verified_abap_line INTO TABLE verified_abap_source_code.
        ENDLOOP.

        GENERATE SUBROUTINE POOL verified_abap_source_code
          ...&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Nov 2023 10:11:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/operand-abap-source-code-in-statement-generate-is-an-abap-command-injection/m-p/12803895#M2025118</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2023-11-06T10:11:02Z</dc:date>
    </item>
  </channel>
</rss>

