GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [apex] ApexSOQLInjection: Add support count query by gwilymatgearset · Pull Request #4056 · pmd/pmd

Conversation

@gwilymatgearset

@gwilymatgearset gwilymatgearset commented Jul 20, 2022 •
edited by adangel
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

Rule: ApexSOQLInjection

Describe the PR

PMD currently has a check for calls to Database.query to check that it correctly protects against soql injection. However, the very similar method Database.countQuery doesn't have these checks.

This PR adds support for detecting soql injections in the countQuery method in the same way we support the query method.

This PR fixes a potential false-negative.

Related issues

I didn't open an issue for this because fixing it was super fast. Let me know if you want one though for a more centralised discussion!

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rsoesemann reacted with heart emoji rsoesemann reacted with rocket emoji
All reactions
  • ❤️ 1 reaction
  • 🚀 1 reaction
gwilymatgearset added 3 commits July 20, 2022 16:40
@gwilymatgearset
Add tests for Database.countQuery by copying the Database.query tests
c211759
@gwilymatgearset
Now detect Database.countQuery as well as Database.query
b3aab7c
@gwilymatgearset
Use correct queries rather than the same as the Database.query ones
94b3723
@ghost

ghost commented Jul 20, 2022

Copy link
Copy Markdown
1 Message
📖 Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 37 errors and 6 configuration errors.
Full report

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel adangel changed the title Add support for soql injection detection in count query [apex] Add support for soql injection detection in count query Jul 25, 2022
@adangel adangel changed the title [apex] Add support for soql injection detection in count query [apex] ApexSOQLInjection: Add support count query Jul 25, 2022
@adangel adangel added this to the 6.48.0 milestone Jul 25, 2022
@adangel adangel added the a:false-negative PMD doesn't flag a problematic piece of code label Jul 25, 2022
adangel
adangel approved these changes Jul 25, 2022
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I'm going to merge this now. Please have a look at the one potential problem I've identified with List<String>...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...pex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/security/xml/ApexSOQLInjection.xml
<code><![CDATA[
public class Foo {
public void test1(List<String> t1) {
Database.countQuery('SELECT COUNT() FROM Account ' + string.join(t1, ','));

@adangel adangel Jul 25, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this is the same test case as for Database.query, but: Wouldn't that allow injection here? (in both cases)

The list t1 is coming from somewhere unsafe. If the first (and only) entry in the list is e.g. "; DELETE FROM Account" - wouldn't that just be executed? Since in this case the list has only 1 element, it is exactly like a simple concatenation...

Would you mind following up on this? If you agree with my assessment, please create a new issue - so that somebody can fix it for both methods (query, countQuery).

I'd merge this PR as is for now, since it is consistent with the current behavior of the rule. This potential bug should be fixed in a separate PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gwilymatgearset gwilymatgearset Jul 29, 2022

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I've managed to inject in that case:

List<string> stuff = new List<string>();

stuff.add('WHERE Name = \'hi\' OR Name = \'');
stuff.add('\'');

Database.countQuery(
    'SELECT COUNT() FROM Account ' +
    string.join(stuff, ',')
);

I'll open an issue and if I have some time in the near future I'll try fixing that one :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread ...pex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/security/xml/ApexSOQLInjection.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

adangel added a commit that referenced this pull request Jul 25, 2022
@adangel
[doc] Update release notes (#4056)
f121ad1
adangel added a commit that referenced this pull request Jul 25, 2022
@adangel
Merge pull request #4056 from gwilymatgearset:add-support-for-soql-in…
73a7ed9
…jection-detection-in-count-query

[apex] ApexSOQLInjection: Add support count query #4056
@adangel
adangel merged commit 94b3723 into pmd:master Jul 25, 2022
@gwilymatgearset
gwilymatgearset deleted the add-support-for-soql-injection-detection-in-count-query branch September 30, 2022 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

a:false-negative PMD doesn't flag a problematic piece of code

Projects

None yet

Milestone

6.48.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@gwilymatgearset @adangel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.