Sub topic 12
Keeping Sensitive Data Out of Non-Production Orgs
This chapter assumes you can run sf data export tree and sf data import tree, and that you know the difference between a scratch org and a sandbox. Non-production orgs are easier to access than production. Developers, contractors, and CI jobs all use them, so real customer data there is a real risk.
Why the risk is higher outside production
Production has tight profiles, audit processes, and a small set of users. A scratch org or sandbox usually has none of that. Debug logs, shared logins, and committed data files can all expose records.
Seed files deserve special attention. A JSON file created by sf data export tree is plain text. If you commit it to Git, the data lives in history even after you delete the file.
What counts as sensitive
- Personal data: names, emails, phone numbers, addresses.
- Financial data: card numbers, bank details, contract values.
- Health or government identifiers.
- Credentials: API keys, tokens, and passwords stored in fields.
Your legal and security teams decide the exact list. Ask them before you copy anything from production.
Choose a strategy
| Strategy | How it works | Best for |
|---|---|---|
| Synthetic data | You create fake records from scratch | Scratch orgs, CI, shared repositories |
| Export from a non-production source | You export from a sandbox that has already been cleaned | Realistic shapes without production values |
| Masking | Sensitive field values are replaced after copy | Sandboxes created from production |
| Exclude the data | A sandbox template leaves the objects out | Sandboxes that do not need the records |
The safest rule is to never export from production into files. Use a source org that holds only fake or masked data.
Mask data in a sandbox
Salesforce offers Data Mask as a separate product. It replaces field values in a sandbox after creation. Availability and setup depend on your license, so check your org's documentation before relying on it.
You can also scrub values in your own post-copy class. This example overwrites contact emails so no real address can receive a message:
global class MaskAfterRefresh implements SandboxPostCopy {
global void runApexClass(SandboxContext context) {
List<Contact> contacts = [SELECT Id, Email FROM Contact WHERE Email != null LIMIT 10000];
for (Contact c : contacts) {
c.Email = c.Id + '@example.invalid';
}
update contacts;
}
}The .invalid top-level domain is reserved, so mail sent to it cannot be delivered. This example handles at most 10,000 rows and covers one field. A real masking job must cover every sensitive field on every object, and larger volumes need Batch Apex.
Keep files out of version control
Add the export folder to .gitignore unless the files contain only synthetic data:
echo "data/exports/" >> .gitignoreCommit only the files you generated yourself, for example the Apex seed script or a plan file that points at fake records.
Common mistakes
- Exporting real Contacts "just for a quick test" and leaving the JSON on a laptop.
- Masking one field but leaving the same value in a related text field, such as Description.
- Leaving email deliverability on in a refreshed sandbox. Set it to "System email only" so test records cannot send real mail.
Exercise
A teammate exports 50 Contacts from production with sf data export tree so they can test a scratch org. Give two problems with this, and name a safer option.
Show answer
The JSON file holds real personal data in plain text, and it can end up in Git history or on a laptop. Real emails in the scratch org could also receive test messages. A safer option is to generate synthetic records with an Apex seed script, or to export from a sandbox that has already been masked.