apex-security-hardening

Harden Apex controllers with USER_MODE CRUD/FLS enforcement and least-privilege permission sets.

5|3|Updated Sep 9, 2026
One-click install
npx skills add https://github.com/SalesforceLabs/revenue-cloud-foundations --skill apex-security-hardening-salesforcelabs
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: apex-security-hardening
Source: https://github.com/SalesforceLabs/revenue-cloud-foundations/tree/main/.cursor/skills/apex-security-hardening
Command: npx skills add https://github.com/SalesforceLabs/revenue-cloud-foundations --skill apex-security-hardening-salesforcelabs

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Salesforce Apex controllers often run SOQL and DML in system mode, bypassing the caller's CRUD and field-level security, while permission sets are built by guesswork and end up over- or under-granted. This Skill provides a canonical workflow to enforce USER_MODE access and derive self-sufficient, least-privilege permission sets that survive deployment and audit review. ## Core Features & Use Cases - USER_MODE Conversion: Detects every system-mode static SOQL query reachable from @AuraEnabled, @InvocableMethod, VF, or webservice entry points and converts queries to WITH USER_MODE and DML to "as user". - Permission-Set Derivation: Enumerates the user-mode object and field surface from the code, cross-checks field permissionability via sf sobject describe, and builds exact objectPermissions and fieldPermissions. - Deploy Verification: Reads ObjectPermissions and FieldPermissions back from the org to catch silent drops from master-detail children and compound address fields, then proves sufficiency with System.runAs or persona walks. - Use Case: Before a security audit, run the hardening pass on a quoting controller: convert all queries to USER_MODE, generate a permission set granting Quote read/edit plus only the permissionable fields the code touches, deploy it, and confirm the org read-back matches the file. ## Quick Start Review my Apex controller for system-mode SOQL and DML, convert everything to USER_MODE, and build a least-privilege permission set that grants exactly what the code touches.

Frequently Asked Questions about apex-security-hardening

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I enforce CRUD and FLS in Apex controllers?

Add WITH USER_MODE to every static and dynamic SOQL query and use "as user" or Database methods with AccessLevel.USER_MODE for DML. Place WITH USER_MODE after the WHERE clause, or after FROM when there is no WHERE, and before GROUP BY, ORDER BY, or LIMIT.

How do I build a least-privilege Salesforce permission set from Apex code?

Enumerate every object and field touched by user-mode queries and DML, then check which fields are permissionable using sf sobject describe. Grant object CRUD only at the level the code needs and fieldPermissions only for permissionable fields, with editable=true solely where the code writes.

Why does my permission set deploy succeed but object permissions are missing?

Master-detail child objects have their CRUD controlled by the master, and the platform silently drops child object permissions on deploy. Grant the master object instead, for example Quote read/edit covers QuoteLineItem and QuoteLineGroup.

Why do I get "Invalid field permission field name" for standard address fields?

Standard compound-address components like BillingStreet and ShippingCountry report permissionable=true but reject permission-set fieldPermissions. Drop those FLS rows because object-level read access already covers them.

Do passing Apex tests prove my permission set is sufficient?

No. Tests run as admin and bypass FLS, so a green suite does not exercise the permission set. Verify with a System.runAs test or a persona walk using a non-admin user assigned only the permission set.