FAQ-000558 - Custom Settings and Configuration Security / Technical Implementation and Code Security

Current Status:VALID_RESPONSEErrorUnable to AnswerSuggests Case

Current FAQ

Question
What is the secure way to manage API keys in Custom Settings that need to be accessed by code running in system mode?
Answer
To securely manage API keys in Custom Settings for code running in system mode: 1. **Use Protected Custom Settings or Metadata**: Store API keys in protected custom settings or protected custom metadata to prevent unauthorized access. 2. **Encrypt the API Keys**: Use the Apex Crypto class to encrypt the keys, and store the encryption key in a protected custom setting or hidden custom metadata field. 3. **Configure Proper Permissions**: Restrict access to these fields to ensure only authorized users or processes can access them. 4. **Use the `transient` Keyword**: In your code, use the `transient` keyword to prevent sensitive data from being stored in the viewstate. These steps help ensure the security of API keys while allowing access in system mode.
Enhancing FAQ...

Enhancing FAQ with AI recommendations...

AI Recommended Enhancement

Related Security Rules (click to view)
ApexSuggestUsingNamedCredAvoidHardcodedCredentialsInFieldDeclsAvoidHardcodedCredentialsInVarAssignAvoidHardcodedCredentialsInVarDeclsApexBadCrypto
Question
What is the secure way to manage API keys in Custom Settings that need to be accessed by code running in system mode?
Recommended Answer Update
To securely manage API keys in Custom Settings for code running in system mode: 1. **Use Protected Custom Settings or Custom Metadata Types**: Store API keys in protected custom settings or protected custom metadata types to prevent unauthorized access through the UI and API. 2. **Consider Named Credentials**: For external API authentication, Named Credentials provide the most secure approach by handling encryption and access control automatically. 3. **If using Custom Settings, encrypt the API Keys**: Use the Apex Crypto class to encrypt the keys, and store the encryption key separately in a protected custom setting or custom metadata type field. 4. **Configure Proper Permissions**: Restrict access to these fields to ensure only authorized users or processes can access them. 5. **Use the `transient` keyword**: In your code, use the `transient` keyword to prevent sensitive data from being stored in the viewstate. These steps help ensure the security of API keys while allowing access in system mode.
Reasoning
The original answer contains solid security practices but can be improved for clarity and completeness. I made the following refinements: 1) Clarified 'Custom Metadata' to 'Custom Metadata Types' for technical precision, 2) Added Named Credentials as the preferred modern approach for external API authentication, which aligns with ApexSuggestUsingNamedCred rule guidance, 3) Made encryption conditional ('If using Custom Settings') since Named Credentials handle encryption automatically, 4) Improved the structure to present the most secure option first. The selected security rules relate directly to the FAQ content: ApexSuggestUsingNamedCred relates to the FAQ's focus on securely managing API credentials and suggests Named Credentials as the preferred approach. AvoidHardcodedCredentialsInFieldDecls, AvoidHardcodedCredentialsInVarAssign, and AvoidHardcodedCredentialsInVarDecls all relate to the core problem this FAQ addresses - avoiding hardcoded credentials by using secure storage mechanisms like Custom Settings. ApexBadCrypto relates to the FAQ's recommendation to use the Apex Crypto class for encryption, ensuring proper cryptographic practices are followed.
Reasoning References