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] False positive in rule ApexCSRF · Issue #2064 · pmd/pmd

[apex] False positive in rule ApexCSRF #2064

New issue
Copy link
New issue
Copy link
Closed
Closed
[apex] False positive in rule ApexCSRF#2064
Copy link
Labels
good first issueA great starting point for new contributors

Description

@nicolas-harraudeau-sonarsource
nicolas-harraudeau-sonarsource
opened on Oct 11, 2019
Issue body actions

Affects PMD Version: 6.18.0

Rule: ApexCSRF

Description:
VisualForce Page fails when a controller's constructor executes DML queries. Salesforce seems to already protect against CSRF attacks.

Maybe I am missing something but there seems to be no security vulnerability, so this would be a False Positive.

However the page fails so reporting an issue still makes sense. What about reporting DML in constructors as "Error Prone" instead of "Security" issues?

Code Sample demonstrating the issue:

1/ Create the two following files in a Salesforce developer console

// File ApexCSRF_FP.cls
public class ApexCSRF_FP {
    public ApexCSRF_FP() {  // issue raised here
        List<Task> tasks = [Select Id, Subject FROM Task];
        for (Task t : tasks) {
            t.subject = 'test';
        }
        update tasks;
    }
    public List<Task> getValue() {
        return [Select Id, Subject FROM Task];
    }
}
// File ApexCSRF_FP.vfp
<apex:page controller="MyVisualForcePageController">
    <apex:pageBlock>
    <apex:pageBlockTable value="{! value }" var="val">
        <apex:column value="{! val.Id }"/>
        <apex:column value="{! val.Subject }"/>
    </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

2/ On the ApexCSRF_FP.vfp page click on the "preview" button.

The following message appears instead of the new page:

DML currently not allowed
An unexpected error has occurred. Your development organization has been notified.

3/ Comment update tasks; and the page will load properly.

This was tested with Salesforce API version 42 and 46.

Running PMD through: CLI

Activity

  1. rsoesemann commented on Oct 12, 2019

    @rsoesemann
    rsoesemann
    on Oct 12, 2019
    Member
    More actions

    Hi Nicolas, thank for taking the time to check a competitor „product“ and help to improve it.

    Am I getting you Right. Pmd should not mark this as security issue as this is already checked on save by the platform?

    The rule is 3 years old and might be outdated. Should we not simply deprecate it?

  2. nicolas-harraudeau-sonarsource commented on Oct 14, 2019

    @nicolas-harraudeau-sonarsource
    nicolas-harraudeau-sonarsource
    on Oct 14, 2019
    Author
    More actions

    Hi Robert,

    Hi Nicolas, thank for taking the time to check a competitor „product“ and help to improve it.

    No problem ;) In addition to our own analyzer we allow the import of PMD issues in SonarQube/SonarCloud. We try to give back to the community when we can.

    Am I getting you Right. Pmd should not mark this as security issue as this is already checked on save by the platform?

    The specific case of a class constructor does not seem to be vulnerable. I tried with a DML query directly in the constructor and also indirectly by calling a function which was executing a DML statement. In both cases an error was returned saying that "DML currently not allowed". I also checked that the records targeted by the DML operations are not updated when this error occurs.

    Note that this isn't detected on "code save". The Developer Console will save code with DML queries in a Controller's constructor. It will just fail at runtime. Thus having DML in a Controller's constructor is a problem, it just isn't a security vulnerability.

    Class constructors can execute DML queries as long as they are not used as a controller. I can instantiate such a class from a function triggered by a button.

    The rule is 3 years old and might be outdated. Should we not simply deprecate it?

    I can't really say if you should remove completely this rule or not. Salesforce documentation shows a clear scenario of CSRF attack, it just doesn't match what the rule detects.

    Side Note regarding the init method
    Class constructors are not the only thing listed in PMD rule ApexCSRF. There is also the "init" method. I have to admit that I am confused by the example in the rule description:

    public class Foo {
        public init() {
            insert data;
        }
    
        public Foo() {
            insert data;
        }
    }
    

    This doesn't look like valid Apex code. The developer console raises an error: "Invalid constructor name: init". I don't know where this init method comes from.

  3. adangel commented on Oct 16, 2019

    @adangel
    adangel
    on Oct 16, 2019
    Member
    More actions

    So, if I understand this right, the goal is to move this rule from "Security" to "Error Prone", since the Apex runtime will prevent executing DML in constructors. But the code still compiles, so we should still flag it.

    That's an interesting case in terms of keeping the rulesets/categories compatible. Assuming somebody is using the rule like this:

    <rule ref="category/apex/security.xml/ApexCSRF" />
    

    that would then immediately break with a PMD update, when we simply move the rule.
    Not sure, whether we should do this in a minor release...

    We can however explain these findings in the rule description and also fix the example code
    Maybe instead of init just a initializer was meant? (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm)

    Then the example would look like this:

    public class Foo {
        {
            insert data;
        }
    
        public Foo() {
            insert data;
        }
    }
    
  4. rsoesemann commented on Oct 17, 2019

    @rsoesemann
    rsoesemann
    on Oct 17, 2019
    Member
    More actions

    @adangel I fully agree that the risk of destroying somebodies rule pipeline by moving the rule into a new category is worse that having a security rule which is actually checked by Salesforce.

    I'd vote to leave it as it is, but change the sample code (with a minor adjustment):

    public class Foo {
    	static {
    		insert record;
    	}
    
    	public Foo() {
    		insert record;
    	}
    }
    
  5. nicolas-harraudeau-sonarsource commented on Oct 17, 2019

    @nicolas-harraudeau-sonarsource
    nicolas-harraudeau-sonarsource
    on Oct 17, 2019
    Author
    More actions

    I tested with an initialization block and a static initialization block just in case the behavior was different. The same error is sent back by Salesforce.

  6. adangel commented on Oct 17, 2019

    @adangel
    adangel
    on Oct 17, 2019
    Member
    More actions

    I thought about moving the rule. So, what we could do is: move the rule from Security to ErrorProne and leave a deprecated rule reference in Security in place. That way, if somebody uses the rule from the Security ruleset, the ruleset would still work, it would just issue a deprecation warning.

    In Summary, what needs to be done here (volunteers are welcome):

    1. Move the rule (and the tests) from security to errorprone
    2. Extend the description to explain, that Salesforce detects this potential security problem at runtime and that modifying the database in the initializer will crash the application upon accessing at runtime
    3. Fix the sample code
    4. Add a deprecated rule-ref in security for compatibility
  7. adangel
    added
    good first issueA great starting point for new contributors
    on Oct 17, 2019
  8. rsoesemann commented on Oct 29, 2019

    @rsoesemann
    rsoesemann
    on Oct 29, 2019
    Member
    More actions

    I’d say we just close this.

  9. rsoesemann
    closed this as completedon Oct 29, 2019
  10. adangel
    added a commit that references this issue on Nov 15, 2019

    [apex] Move ApexCSRF rule from security to errorprone

    64f4436
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

    good first issueA great starting point for new contributors

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.