ApexCRUDViolation - lpohl-Reply/pmd-github-action GitHub Wiki
Validate CRUD permission before SOQL/DML operation or enforce user mode
The rule validates you are checking for access permissions before a SOQL/SOSL/DML operation. Since Apex runs by default in system mode not having proper permissions checks results in escalation of privilege and may produce runtime errors. This check forces you to handle such scenarios.
Since Winter '23 (API Version 56) you can enforce user mode for database operations by using
WITH USER_MODE in SOQL. This makes Apex to respect Field-level security (FLS) and object
permissions of the running user. When using user mode, no violation is reported by this rule.
By default, the rule allows access checks can be performed using system Apex provisions such as
DescribeSObjectResult.isAccessible/Createable/etc., the SOQL WITH SECURITY_ENFORCED clause,
or using the open source Force.com ESAPI
class library. Because it is common to use authorization facades to assist with this task, the
rule also allows configuration of regular expression-based patterns for the methods used to
authorize each type of CRUD operation. These pattern are configured via the following properties:
-
createAuthMethodPattern/createAuthMethodTypeParamIndex- a pattern for the method used for create authorization and an optional 0-based index of the parameter passed to that method that denotes theSObjectTypebeing authorized for create. -
readAuthMethodPattern/readAuthMethodTypeParamIndex- a pattern for the method used for read authorization and an optional 0-based index of the parameter passed to that method that denotes theSObjectTypebeing authorized for read. -
updateAuthMethodPattern/updateAuthMethodTypeParamIndex- a pattern for the method used for update authorization and an optional 0-based index of the parameter passed to that method that denotes theSObjectTypebeing authorized for update. -
deleteAuthMethodPattern/deleteAuthMethodTypeParamIndex- a pattern for the method used for delete authorization and an optional 0-based index of the parameter passed to that method that denotes theSObjectTypebeing authorized for delete. -
undeleteAuthMethodPattern/undeleteAuthMethodTypeParamIndex- a pattern for the method used for undelete authorization and an optional 0-based index of the parameter passed to that method that denotes theSObjectTypebeing authorized for undelete. -
mergeAuthMethodPattern/mergeAuthMethodTypeParamIndex- a pattern for the method used for merge authorization and an optional 0-based index of the parameter passed to that method that denotes theSObjectTypebeing authorized for merge.
The following example shows how the rule can be configured for the
sirono-common
AuthorizationUtil class:
<rule ref="category/apex/security.xml/ApexCRUDViolation" message="Validate CRUD permission before SOQL/DML operation">
<priority>3</priority>
<properties>
<property name="createAuthMethodPattern" value="AuthorizationUtil\.(is|assert)(Createable|Upsertable)"/>
<property name="readAuthMethodPattern" value="AuthorizationUtil\.(is|assert)Accessible"/>
<property name="updateAuthMethodPattern" value="AuthorizationUtil\.(is|assert)(Updateable|Upsertable)"/>
<property name="deleteAuthMethodPattern" value="AuthorizationUtil\.(is|assert)Deletable"/>
<property name="undeleteAuthMethodPattern" value="AuthorizationUtil\.(is|assert)Undeletable"/>
<property name="mergeAuthMethodPattern" value="AuthorizationUtil\.(is|assert)Mergeable"/>
</properties>
</rule>Note: This rule will produce false positives for VF getter methods. In VF getters the access permission check happens automatically and is not needed explicitly. However, the rule can't reliably determine whether a getter is a VF getter or not and reports a violation in any case. In such cases, the violation should be suppressed.
3
public class Foo {
public Contact foo(String status, String ID) {
// validate you can actually query what you intend to retrieve
Contact c = [SELECT Status__c FROM Contact WHERE Id=:ID WITH SECURITY_ENFORCED];
// Make sure we can update the database before even trying
if (!Schema.sObjectType.Contact.fields.Status__c.isUpdateable()) {
return null;
}
c.Status__c = status;
update c;
return c;
}
}