Skip to content

forcedotcom/sf-skills / plugins/builder/salesforce-code-quality/skills/dx-code-analyzer-custom-rule-create

dx-code-analyzer-custom-rule-create

Verified

Create custom Code Analyzer rules for Regex (pattern matching), PMD (XPath/AST for Apex and metadata XML), and ESLint (LWC/JavaScript/TypeScript). Use when users want to enforce coding standards, ban patterns, detect hardcoded values, govern metadata, or add rules not in the built-in set. TRIGGER when: user says 'create a rule', 'ban System.debug', 'enforce naming convention', 'detect hardcoded IDs', 'custom rule', 'xpath rule', 'regex rule', 'add a PMD rule', 'enforce a policy', 'create a check for', 'flag this pattern', 'make a rule that catches', 'metadata rule', 'check permissions', 'enforce API version', 'eslint rule', 'lwc rule', 'override rule threshold', 'customize complexity', or describes a pattern to enforce. DO NOT TRIGGER when: user wants to run a scan (use dx-code-analyzer-run), configure engines (use dx-code-analyzer-configure), or explain existing rules (use dx-code-analyzer-run).

examples/metadata-xml-example-permissions.md

View on GitHub
# Metadata XML Examples: Permissions

[← Back to Metadata XML Examples Index](metadata-xml-examples.md)

## Example 1: Flag ModifyAllData / ViewAllData Permissions

**Problem:** Permission sets granting ModifyAllData or ViewAllData are a security risk.

**Target file:** `*.permissionset-meta.xml`

**Sample violating metadata:**
```xml
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
    <label>Power User</label>
    <userPermissions>
        <enabled>true</enabled>
        <name>ModifyAllData</name>
    </userPermissions>
</PermissionSet>
```

**XPath (PMD 7):**
```xpath
//*[@Text='ModifyAllData' or @Text='ViewAllData']/../..
  [local-name()='userPermissions']
  [.//*[@Text='true']]
```

**How it works:**
1. `//*[@Text='ModifyAllData' or @Text='ViewAllData']` — find text node with dangerous permission name
2. `/../..` — navigate up: text node → `<name>` element → `<userPermissions>` parent
3. `[local-name()='userPermissions']` — confirm we're at the right element
4. `[.//*[@Text='true']]` — check that a descendant text node contains "true" (the `<enabled>` value)

**PMD ruleset:**
```xml
<rule name="NoDangerousPermissions"
      language="xml"
      message="Permission set grants ModifyAllData or ViewAllData — use specific object permissions instead"
      class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
    <description>Flags permission sets that grant ModifyAllData or ViewAllData</description>
    <priority>1</priority>
    <properties>
        <property name="xpath">
            <value><![CDATA[
//*[@Text='ModifyAllData' or @Text='ViewAllData']/../..
  [local-name()='userPermissions']
  [.//*[@Text='true']]
]]></value>
        </property>
    </properties>
</rule>
```

---

## Example 4: No Field Permissions in Profiles

**Problem:** Field permissions should be managed via Permission Sets, not Profiles.

**Target file:** `*.profile-meta.xml`

**Sample violating metadata:**
```xml
<Profile xmlns="http://soap.sforce.com/2006/04/metadata">
    <fieldPermissions>
        <editable>true</editable>
        <field>Account.Revenue__c</field>
        <readable>true</readable>
    </fieldPermissions>
</Profile>
```

**XPath:**
```xpath
//*[local-name()='Profile']/*[local-name()='fieldPermissions']
```

**PMD ruleset:**
```xml
<rule name="NoFieldPermissionsInProfile"
      language="xml"
      message="Field permissions should be in Permission Sets, not Profiles"
      class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
    <description>Profiles should not contain field-level security — use Permission Sets</description>
    <priority>2</priority>
    <properties>
        <property name="xpath">
            <value><![CDATA[
//*[local-name()='Profile']/*[local-name()='fieldPermissions']
]]></value>
        </property>
    </properties>
</rule>
```
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 197kUpdated Claude CodeCodexVerifiedInstall