DEV Community

hailports
hailports

Posted on

The Apex Class That Bypasses Your Sharing Model

The Apex Class That Bypasses Your Sharing Model

As a senior Salesforce administrator, I've seen and managed some of the largest organizations in the world, where data security is paramount. One common challenge we face is ensuring that our sharing models are robust enough to protect sensitive information while still allowing necessary access for employees. However, there’s always a risk that custom Apex code might bypass these sharing settings, leading to potential data leaks or breaches.

In this article, I’ll walk you through an example of how a malicious Apex class could bypass your organization's sharing model and provide guidance on how to prevent such occurrences. By the end of this post, you'll have a better understanding of why it’s crucial to regularly audit your custom Apex code for security vulnerabilities.

Introduction

Salesforce offers robust sharing mechanisms that control who can access what data based on roles, profiles, groups, and more. However, these controls are only as strong as the Apex code that interacts with them. A poorly written or intentionally malicious Apex class can circumvent these mechanisms, leading to unauthorized access to sensitive information.

The Problem: Bypassing Sharing Models

Imagine you have a custom object called ConfidentialDocument__c, which contains highly sensitive data such as financial reports or confidential contracts. Your sharing rules are set up so that only certain users and groups within specific roles can view these documents. However, one of your developers creates an Apex class with the following code:

public class ConfidentialDocumentAccess {
    public static List<ConfidentialDocument__c> getDocuments() {
        return [SELECT Id, Name FROM ConfidentialDocument__c];
    }
}
Enter fullscreen mode Exit fullscreen mode

At first glance, this seems harmless. However, if another developer uses this class in a way that doesn’t respect the sharing settings (e.g., by querying all records without any conditions), it can potentially expose sensitive information.

Real SOQL Queries

Let’s consider an example where an attacker or malicious user could exploit this code:

public class ConfidentialDocumentAccess {
    public static List<ConfidentialDocument__c> getDocuments() {
        // Potential vulnerability: querying all records without conditions
        return [SELECT Id, Name FROM ConfidentialDocument__c];
    }
}
Enter fullscreen mode Exit fullscreen mode

In a real-world scenario, such a query could be used by an attacker to retrieve sensitive data. For instance:

List<ConfidentialDocument__c> documents = ConfidentialDocumentAccess.getDocuments();
for (ConfidentialDocument__c doc : documents) {
    // Process each document
    System.debug('Document: ' + doc.Name);
}
Enter fullscreen mode Exit fullscreen mode

Specific Config Steps

To mitigate this risk, follow these best practices:

  1. Audit Custom Apex Code: Regularly review your custom Apex classes to ensure they respect the sharing model.
  2. Use Proper SOQL Queries: Always use SOQL queries that adhere to the sharing settings defined in your organization.
  3. Implement Access Controls: Use Apex triggers or metadata to enforce access controls, ensuring that only authorized users can perform certain actions.

Here’s an example of a more secure version of the above code:

public class ConfidentialDocumentAccess {
    public static List<ConfidentialDocument__c> getAccessibleDocuments(UserInfo currentUser) {
        // Query accessible documents based on current user's role and sharing model
        String userId = currentUser.getUserId();
        Id ownerId = [SELECT OwnerId FROM User WHERE Id = :userId].OwnerId;

        return [
            SELECT Id, Name 
            FROM ConfidentialDocument__c 
            WHERE OwnerId IN (SELECT ParentId FROM GroupMember WHERE UserId = :userId)
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

This version of the code ensures that only documents owned by groups or users with whom the current user is associated are returned.

Enterprise Experience

In one of my previous roles at a $5B+ enterprise, we encountered a similar issue. A custom Apex class was used to fetch all Opportunity records without any sharing checks, leading to data exposure. We addressed this by implementing rigorous code reviews and continuous integration tests that flagged any potential security issues.

Preventing Security Vulnerabilities

To prevent such vulnerabilities in your organization:

  1. Code Reviews: Conduct regular code reviews with a focus on security best practices.
  2. Security Training: Train developers on secure coding techniques and the importance of respecting sharing models.
  3. Automated Testing: Use tools like Apex tests to ensure that custom Apex classes adhere to the organization’s security policies.

Conclusion

While Salesforce provides powerful sharing mechanisms, it's crucial to remain vigilant against potential vulnerabilities in custom Apex code. By following best practices and regularly auditing your codebase, you can significantly reduce the risk of data breaches or unauthorized access.

Call to Action

Don’t let your Apex code become a liability. Try the free scanner at https://orgscanner.dev/?utm_source=devto&utm_medium=content&utm_campaign=content_poster. This tool can help you identify potential security issues in your custom Apex classes and ensure that they adhere to your organization’s sharing model.

Stay secure, stay vigilant!

Top comments (0)