Skip to content

[apex] AvoidHardcodingId false positives #776

Closed

Description

@parksungrin

Please, prefix the report title with the language it applies to within brackets, such as [java] or [apex]. If not specific to a language, you can use [core]

Rule Set:
6.0.0 Snapshoot
AvoidHardcodingId

Description:
15 digit number is indicated as Salesforce Id.

objAssetDevice.IMEI__c = '359040082913024';

Code Sample demonstrating the issue:
359040082913024 is not Salesforce Id but below code indicated as violation of AvoidHardcodingId
objAssetDevice.IMEI__c = '359040082913024';

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
CLI

Activity

  1. rsoesemann commented on Dec 5, 2017

    @rsoesemann
    Member
  2. jsotuyod commented on Dec 5, 2017

    @jsotuyod
    Member

    @up2go-rsoesemann @JAertgeerts @parksungrin the rule is implemented based on this description which is actually a little more strict on 15 chars ids.

    The rule is flawed for ids longer than 15 (it would allow, 16 and 17 chars "ids", and the 18 char ones are not validated).

    However, for this scenario of a 15 char string, it would still match. I don't think we can safely detect this as a FP and avoid it...

  3. rsoesemann commented on Dec 5, 2017

    @rsoesemann
    Member

    The rule would need to incorporate also a checksum check as described here https://gist.github.com/jeriley/36b29f7c46527af4532aaf092c90dd56

    A simple regex doesn't do the magic.

  4. jsotuyod commented on Dec 5, 2017

    @jsotuyod
    Member

    @up2go-rsoesemann that checksum applies only to 18 digit ids. 15 digit ids would still be flagged as long as the sixth position is a 0.

  5. alan-morey commented on Dec 16, 2017

    @alan-morey

    While not a solution for the false positive scenario where a 15 digit string is considered a Salesforce ID, the current pattern could be made stricter.

    private static final Pattern PATTERN = Pattern.compile("^[a-zA-Z0-9]{5}[0][a-zA-Z0-9]{9,12}$", Pattern.CASE_INSENSITIVE);

    The following pattern:

    Pattern.compile("^[a-zA-Z0-9]{5}0[a-zA-Z0-9]{9}([a-zA-Z0-5]{3})?$");

    Also is Pattern.CASE_INSENSITIVE flag redundant when the character classes are matching both upper and lower alpha?

  6. added this to the 6.1.0 milestone on Dec 20, 2017
  7. jsotuyod commented on Dec 20, 2017

    @jsotuyod
    Member

    @alan-morey thanks, that's exactly what I was going after. The checksum check can be done too.

    I'm flagging this for 6.1.0 release.

  8. modified the milestones: 6.1.0, 6.2.0 on Feb 25, 2018
  9. modified the milestones: 6.2.0, 6.3.0 on Mar 26, 2018
  10. self-assigned this
    on Apr 10, 2018
  11. added a commit that references this issue on Apr 10, 2018
    c77b0b4
  12. changed the title [-][apex] AvoidHardcodingId indicate 15 digit number as Id[/-] [+][apex] AvoidHardcodingId false positives[/+] on Apr 10, 2018
  13. added a commit that references this issue on Apr 10, 2018
  14. added
    a:false-positive
    on Apr 10, 2018
  15. added a commit that references this issue on Apr 24, 2018
  16. adangel commented on Apr 24, 2018

    @adangel
    Member

    Note: only solution for the specific string is right now to suppress the rule, like that:

           @SuppressWarnings('PMD.AvoidHardcodingId')
            String IMEI__c = '359040082913024';
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a:false-positive

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions