Skip to content

[apex] ApexSOQLInjection: False positive when passing local var with concatenating strings #2955

Closed

Description

@KevSlashNull

Affects PMD Version:
6.21+
7.0.0

Description:

If the query is not directly passed as a string literal (or multiple concatenated string literals) but instead as a variable, PMD flags the query as a rule violation when the content of the variable is concatenated.

The issue can be resolved by not passing the query as a variable to Database.query, however, with large queries, this makes the code harder to read (equivalent to calling a method with multiple parameters).

Exception Stacktrace:

No stacktrace.

Code Sample demonstrating the issue:

public without sharing class ExampleClass {
    public List<Account> run() {
        String query = 'SELECT Name FROM Account WHERE Active__c = true AND'
                        + ' HasSupportPlan__c = true LIMIT 5';

        return Database.query(query);
    }

    public List<Account> runWithoutRuleViolation() {
        return Database.query('SELECT Name FROM Account WHERE Active__c = true AND'
                        + ' HasSupportPlan__c = true LIMIT 5');
    }
}

This can also be mitigated by replacing Database.query(query) with Database.query(String.escapeSingleQuotes(query)) but that’ll likely create more issues, especially when you’re not using variable binding everywhere.

Steps to reproduce:

Please provide detailed steps for how we can reproduce the bug.

  1. Create the ExampleClass.cls file with the content of the code sample
  2. Run pmd -d ExampleClass.cls -R rulesets/apex/quickstart.xml
  3. See that the output is the following (replace [absolute path] by the path to the ExampleClass.cls). The last point should not be listed because it’s just as secure as the query in runWithoutRuleViolation.
    /[absolute path]/ExampleClass.cls:1:     Missing ApexDoc comment
    /[absolute path]/ExampleClass.cls:2:     Missing ApexDoc comment
    /[absolute path]/ExampleClass.cls:6:     Apex classes should escape variables merged in DML query
    

Running PMD through: CLI or VS Code (Apex PMD extension)

Activity

  1. added
    a:bug
    on Dec 2, 2020
  2. added
    a:false-positive
    and removed
    a:bug
    on Jan 18, 2021
  3. added
    needs:pmd7-revalidation
    on Mar 17, 2024
  4. removed
    needs:pmd7-revalidation
    on Apr 2, 2024
  5. changed the title [-][apex] ApexSOQLInjection false-positive when concatenating strings[/-] [+][apex] ApexSOQLInjection: False positive when concatenating strings[/+] on Jun 12, 2026
  6. changed the title [-][apex] ApexSOQLInjection: False positive when concatenating strings[/-] [+][apex] ApexSOQLInjection: False positive when passing local var with concatenating strings[/+] on Jun 12, 2026
  7. added this to the 7.26.0 milestone on Jun 12, 2026
  8. added a commit that references this issue on Jun 12, 2026
    5e62e72
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions