FAQ-001621 - SQL Injection and Input Validation / Security Review and Vulnerability Assessment

Current Status:VALID_RESPONSEErrorUnable to AnswerSuggests Case

Current FAQ

Question
What specific modifications are needed to fix SOQL injection issues?
Answer
To fix SOQL injection issues in Apex code, you should make the following modifications: 1. **Use Bind Variables**: Incorporate bind variables in SOQL queries to safely handle user input and prevent direct concatenation of user input. 2. **Sanitize Inputs**: Use `String.escapeSingleQuotes()` to sanitize all string inputs dynamically added to queries. Validate and sanitize user inputs, especially when they are used to identify objects, fields, or query portions. 3. **Avoid Direct Strings for Field/Object Names**: Do not pass field or object names as strings directly into queries. If dynamic queries are necessary, perform CRUD/FLS checks on fields and objects before including them. 4. **Secure API Context**: For apps using an API account with elevated privileges, ensure the query execution context does not allow unauthorized access to sensitive data. 5. **Validate and Restrict Inputs**: Validate and restrict user inputs to prevent malicious data from being used in query construction. Validate user inputs against a predefined whitelist of acceptable values to ensure only valid data is used. 6. **Avoid String Concatenation**: For dynamic SOQL queries, avoid using strings as building blocks. Instead, use `sObjectType` or `sObjectFields` to construct queries securely. 7. **Sanitize Public Method Parameters**: Ensure all parameters passed to public methods executing database operations are sanitized. These changes ensure secure input handling and query construction, mitigating SOQL injection risks.
Enhancing FAQ...

Enhancing FAQ with AI recommendations...

AI Recommended Enhancement

Related Security Rules (click to view)
ApexSOQLInjectionApexCRUDViolationApexSharingViolations
Question
What specific modifications are needed to fix SOQL injection issues?
Recommended Answer Update
To fix SOQL injection issues in Apex code, you should make the following modifications: 1. **Use Bind Variables**: Incorporate bind variables in SOQL queries to safely handle user input and prevent direct concatenation of user input. 2. **Sanitize Inputs**: Use `String.escapeSingleQuotes()` to sanitize all string inputs dynamically added to queries. Validate and sanitize user inputs, especially when they're used to identify objects, fields, or query portions. 3. **Avoid Direct Strings for Field/Object Names**: Don't pass field or object names as strings directly into queries. If dynamic queries are necessary, perform CRUD/FLS checks on fields and objects before including them. 4. **Secure API Context**: For apps using an API account with elevated privileges, ensure the query execution context doesn't allow unauthorized access to sensitive data. 5. **Validate and Restrict Inputs**: Validate and restrict user inputs to prevent malicious data from being used in query construction. Validate user inputs against a predefined whitelist of acceptable values to ensure only valid data is used. 6. **Avoid String Concatenation**: For dynamic SOQL queries, avoid using strings as building blocks. Instead, use `SObjectType` or `SObjectField` to construct queries securely. 7. **Sanitize Public Method Parameters**: Ensure all parameters passed to public methods executing database operations are sanitized. These changes ensure secure input handling and query construction, mitigating SOQL injection risks.
Reasoning
The FAQ content is accurate and comprehensive for SOQL injection prevention. I made minor improvements to enhance readability and consistency: changed 'Do not pass' to 'Don't pass' for a more conversational tone, changed 'does not allow' to 'doesn't allow' for consistency, and corrected 'sObjectType' and 'sObjectFields' to the proper Apex naming conventions 'SObjectType' and 'SObjectField'. For security rules selection: ApexSOQLInjection directly relates to the FAQ's core topic of fixing SOQL injection issues through bind variables, input sanitization, and avoiding string concatenation - all techniques mentioned in points 1, 2, 5, 6, and 7. ApexCRUDViolation relates to point 3 which specifically mentions performing 'CRUD/FLS checks on fields and objects before including them' in dynamic queries. ApexSharingViolations relates to point 4 which addresses 'query execution context' and preventing 'unauthorized access to sensitive data', which are sharing and access control concerns.
Reasoning References
Recommended Related Articles