SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • ShellShell
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
Apex

Apex static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your APEX code

  • All rules 56
  • Vulnerability1
  • Bug12
  • Security Hotspot3
  • Code Smell40
 
Tags
    Impact
      Clean code attribute
        1. Sharing level should be specified in Apex Classes with SOQL/SOSL Queries or DML Statements

           Vulnerability

        Sharing level should be specified in Apex Classes with SOQL/SOSL Queries or DML Statements

        intentionality - complete
        security
        Vulnerability

          Why is this an issue?

          More Info

          By default Apex code executes without checking permissions. Hence the code will not enforce field level security, sharing rules and user permissions during execution of Apex code in Triggers, Classes and Controllers. This creates the risk that unauthorized users may get access to sensitive data records or fields.

          To prevent this, developers should use with sharing keyword when declaring their classes if the class has SOQL or SOSL queries or DML Statements. This will ensure that current user’s permissions, field level security and sharing rules are enforced during code execution. Thus users will only see or modify records and fields which they have access to.

          Use without sharing when a specific class should have full access to records without taking into account current user’s permissions. This should be used very carefully.

          Use inherited sharing when the code should inherit the level of access from the calling class. This is more secure than not specifying a sharing level as the default will be equivalent to "with sharing".

          This rule raises an issue when a class containing DML, SOSL or SOQL queries has no sharing level specified (with sharing, without sharing, inherited sharing).

          Noncompliant code example

          public class MyClass { // Noncompliant, no sharing specified
            List<Case> lstCases = new List<Case>();
            for(Case c:[SELECT Id FROM Case WHERE Status = 'In Progress']){ // SOQL query
                // ...
            }
          }
          
          public class MyClass { // Noncompliant
            List<List<SObject>> sList = [FIND 'TEST' IN ALL FIELDS
                                                RETURNING Case(Name), Contact(FirstName,LastName)]; // SOSL query
          
          }
          
          public class MyClass { // Noncompliant
            List<Case> lstCases = new List<Case>();
            for(Case c:[SELECT Id, Status FROM Case WHERE Status = 'In Progress']){
                c.Status = 'Closed';
                lstCasesToBeUpdated.add(c);
            }
            Update lstCasesToBeUpdated;  // DML query
          }
          

          Compliant solution

          public with sharing class MyClass {
            List<Case> lstCases = new List<Case>();
            for(Case c:[SELECT Id FROM Case WHERE Status = 'In Progress']){
                // ...
            }
          }
          
          public without sharing class MyClass {
            List<List<SObject>> sList = [FIND 'TEST' IN ALL FIELDS
                                                RETURNING Case(Name), Contact(FirstName,LastName)];
          }
          
          public inherited sharing class MyClass {
            List<Case> lstCases = new List<Case>();
            for(Case c:[SELECT Id, Status FROM Case WHERE Status = 'In Progress']){
                c.Status = 'Closed';
                lstCasesToBeUpdated.add(c);
            }
            Update lstCasesToBeUpdated;
          }
          

          Exceptions

          No issue will be raised for test classes, i.e. classes annotated with @isTest

            Available In:
          • SonarQube IdeCatch issues on the fly,
            in your IDE
          • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories
          • SonarQube ServerAnalyze code in your
            on-premise CI
            Enterprise
            Edition
            Available Since
            9.1

          © 2008-2025 SonarSource SA. All rights reserved.

          Privacy Policy | Cookie Policy | Terms of Use