{"id":77,"date":"2013-11-01T13:04:47","date_gmt":"2013-11-01T13:04:47","guid":{"rendered":"http:\/\/www.greytrix.com\/blogs\/salesforce\/?p=77"},"modified":"2024-08-05T05:49:02","modified_gmt":"2024-08-05T05:49:02","slug":"soql-injections","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2013\/11\/01\/soql-injections\/","title":{"rendered":"SOQL Injections in Salesforce"},"content":{"rendered":"<p>SOQL Injection is the breach of our application security which is dangerous for our valuable data. This happens because preventive measures are not taken into consideration when we write our SOQL queries for any DML operation.<\/p>\n<p>When the queries are formed dynamically with front end input, user can use backend queries differently to get the information which he should not be having the access of.<\/p>\n<ul>\n<li>SELECT Id FROM employeeDetail__c WHERE (UserLogged__c = true and Name like \\&#8217;%&#8217; + name + &#8216;%\\&#8217;)&#8217;;<\/li>\n<\/ul>\n<p>When the user inputs the name <b>GreyMan<\/b> then the query will be as shown below &#8211;<\/p>\n<ul>\n<li>SELECT Id FROM employeeDetail__c WHERE (UserLogged__c = true and Name like\u00a0 \u2018% GreyMan%\u2019 );<\/li>\n<\/ul>\n<p>But if in case user provides unexpected input like \u00a0\u00a0<b>greytest%&#8217;) OR (Name LIKE &#8216;<\/b><\/p>\n<ul>\n<li>SELECT Id FROM employeeDetail__c WHERE (UserLogged__c = true and Name LIKE &#8216;%test%&#8217;) OR (Name LIKE &#8216;%&#8217;) ;<\/li>\n<\/ul>\n<p>This query will perform injection against your logic and will show all the Ids.<\/p>\n<p><span style=\"color: #800000;\"><em><strong>The New Stuff<\/strong><\/em>\u00a0:<\/span>\u00a0<a title=\"Mapping custom lead fields to standard contact fields\" href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/2013\/11\/01\/mapping-custom-lead-fields-to-standard-contact-fields\/\" target=\"_blank\" rel=\"noopener\">Mapping custom lead fields to standard contact fields<\/a><a title=\"Overriding standard visual page styling in Salesforce\" href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/overriding-standard-visual-page-styling-in-salesforce\/\" target=\"_blank\" rel=\"noopener\"><br \/>\n<\/a><\/p>\n<p>A SOQL Injection flaw can be used to modify the intended logic of any vulnerable query like the one shown above. To prevent a SOQL injection attack, avoid using dynamic SOQL queries.<\/p>\n<ul>\n<li>String name = &#8216;%&#8217; + Qname + &#8216;%&#8217;;<\/li>\n<li>List&lt; employeeDetail __c&gt; = [SELECT Id, Name FROM Contact WHERE (UserLogged__c = true and Name like: name)];<\/li>\n<\/ul>\n<p>Here some conditions are described which we need to take care of when we write SOQL Queries.<\/p>\n<ul>\n<li>Stored Procedures having static logic is ok but procedures having dynamic logic like exec or other dynamic constructs used internally can be injected.<span style=\"font-size: 14px; line-height: 1.6em;\">\u00a0<\/span><\/li>\n<\/ul>\n<ul>\n<li>We can\u2019t use bind variable fields in the query string like the below example.<\/li>\n<\/ul>\n<p>Object_Variable__c employeeDetail = new Object_Variable__c(Employee_Name__c =&#8217;GreyMan&#8217;);<\/p>\n<p>Select id, Employee_Id__c, Employee_Name__c from Employee__c where Employee_Name__c = employeeDetail.Employee_Name__c;<\/p>\n<ul>\n<li>We can instead resolve the variable field into a string and use the string in your dynamic SOQL query.<\/li>\n<\/ul>\n<p>String Empname = \u2019GreyMan\u2019;<\/p>\n<p>Select id, Employee_Id__c, Employee_Name__c from Employee__c where \u00a0\u00a0\u00a0\u00a0Employee_Name__c =: Empname;<\/p>\n<p><b>OR<\/b><\/p>\n<p>Object_Variable__c employeeDetail = new Object_Variable__c(Employee_Name__c =&#8217;GreyMan&#8217;);<\/p>\n<p>String employeeName= employeeDetail. Employee_Name__c;<\/p>\n<p>Select id, Employee_Id__c, Employee_Name__c from Employee__c where Employee_Name__c = employeeName;<\/p>\n<ul>\n<li>Sanitizing user data before passing it to a query is a standard best practice, but proper construction of queries is the most important and reliable defense.<\/li>\n<li>In case of dynamic SOQL use <b>escapeSingleQuotes<\/b> method to sanitize the user input. This method ensures that all single quotation marks are treated as enclosing strings, instead of database commands.<\/li>\n<\/ul>\n<p>sQuery += &#8216;Select id, Employee_Id__c, Employee_Name__c&#8217;;<\/p>\n<p>sQuery += &#8216; from &#8216;+ Employee__c;<\/p>\n<p>SQuery += &#8216; where Employee_Name__c= \\&#8217; &#8216; +String.escapeSingleQuotes(Empname)+&#8217;\\&#8217; &#8216;;<\/p>\n<p><span style=\"font-size: Medium;\"><strong>About Us<\/strong><\/span><br \/>\n<a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> as a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners across the globe. We offers Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that requires apex code incorporation into the Salesforce platform.<\/p>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> has some unique solutions for Cloud CRM such as Salesforce integration with <a href=\"https:\/\/www.greytrix.com\/product\/sage-x3\/gumu-x3-salesforce-integration\">Sage Enterprise Management (Sage X3<\/a>), Sage Intacct, <a href=\"https:\/\/www.greytrix.com\/product\/sage-100-erp\/gumu-100-salesforce-integration\">Sage 100 <\/a>and <a href=\"https:\/\/www.greytrix.com\/product\/sage-300-erp\/gumu-300-salesforce-integration\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/product\/crm-development\/salesforce-com\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/product\/professional-services\/data-migration-services\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/product\/professional-services\/integrated-application-development\">Integrated App development<\/a>, <a href=\"https:\/\/www.greytrix.com\/product\/professional-services\/custom-development\">Custom App development<\/a> and <a href=\"https:\/\/www.greytrix.com\/product\/professional-services\/maintenance-support\">Technical Support<\/a> to business partners and end users.<\/p>\n<p>Greytrix GUMU&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star app listed on <a href=\"https:\/\/appexchange.salesforce.com\/listingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<\/p>\n<p>For more information, please contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n<p><strong><span style=\"font-size: Medium;\">Related Posts<\/span><\/strong><\/p>\n<ul>\n<li><a title=\"Overriding standard visual page styling in Salesforce\" href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/2013\/10\/23\/overriding-standard-visual-page-styling-in-salesforce\/\" target=\"_blank\" rel=\"noopener\">Overriding standard visual page styling in Salesforce<\/a><\/li>\n<li><a title=\"Assign custom object permissions to Standard User in Salesforce\" href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/2013\/06\/11\/assign-custom-object-permissions-to-standard-user-in-salesforce\/\" target=\"_blank\" rel=\"noopener\">Assign custom object permissions to Standard User in Salesforce<\/a><\/li>\n<li><a title=\"Mapping Custom Lead Fields in Salesforce\" href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/2013\/08\/23\/mapping-custom-lead-fields-in-salesforce\/\" target=\"_blank\" rel=\"noopener\">Mapping Custom Lead Fields in Salesforce<\/a><\/li>\n<li><a title=\"Calling SOQL Query from JavaScript for validation of Account Name in Salesforce\" href=\"http:\/\/www.greytrix.com\/blogs\/salesforce\/2013\/06\/05\/calling-soql-query-from-javascript-for-validation-of-account-name-in-salesforce\/\" target=\"_blank\" rel=\"noopener\">Calling SOQL Query from JavaScript for validation of Account Name in Salesforce<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>SOQL Injection is the breach of our application security which is dangerous for our valuable data. This happens because preventive measures are not taken into consideration when we write our SOQL queries for any DML operation. When the queries are formed dynamically with front end input, user can use backend queries differently to get the\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2013\/11\/01\/soql-injections\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[471,472],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-salesforce-srv","tag-soql","tag-soql-injections"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":2,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":3969,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/77\/revisions\/3969"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}