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).

references/xpath-patterns-security.md

View on GitHub
# XPath Patterns for Security

[← Back to XPath Patterns Index](xpath-patterns.md)

## Security Patterns

### Class without sharing declaration

```xpath
//UserClass[
  @Nested = false()
  and ModifierNode[@WithSharing = false() and @WithoutSharing = false() and @InheritedSharing = false()]
]
```

**AST evidence:** `ModifierNode` has `@WithSharing`, `@WithoutSharing`, `@InheritedSharing` — all `false()` means no sharing keyword declared (implicit without sharing in API versions 66.0 and below. In API versions 67.0 and higher, the default changes to implicit with sharing).

Added `@Nested = false()` to exclude inner classes (which inherit from parent).

> ⚠️ **PMD 7 boolean attributes:** `@WithSharing`, `@WithoutSharing`, `@InheritedSharing`, and `@Nested` are boolean-typed in PMD 7 — string comparison (`='false'`) errors with "Cannot compare xs:boolean to xs:string". Always use `= false()` / `= true()`. See [xpath-patterns.md](xpath-patterns.md) for the full list.

### SOQL without WITH USER_MODE or SECURITY_ENFORCED

```xpath
//SoqlExpression[
  not(contains(@CanonicalQuery, 'WITH USER_MODE'))
  and not(contains(@CanonicalQuery, 'WITH SECURITY_ENFORCED'))
]
[not(ancestor::UserClass[ModifierNode[@Test = true()]])]
```

**AST evidence:** `SoqlExpression[@CanonicalQuery]` contains the full normalized query text.

### Hardcoded Salesforce IDs (structural — string literals 15-18 chars)

```xpath
//LiteralExpression[
  @LiteralType='STRING'
  and string-length(@Image) >= 15
  and string-length(@Image) <= 18
]
[not(ancestor::UserClass[ModifierNode[@Test = true()]])]
```

**Note:** `matches()` function may not be available in all PMD XPath versions. The string-length check catches most cases. For precision, use Regex engine instead.
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 321kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 321kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 321kUpdated 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