feat(security): encrypt Contact.email at rest + blind index (G3) - #596
Merged
Conversation
First slice of the PII encrypt-at-rest epic (2.0.0). Contact.email is now encrypted (encrypted cast, column widened to text). The plaintext unique + lookup indexes can't survive encryption (random IV), so a deterministic email_hash blind index (HMAC-SHA256 of the normalised email, unique) carries uniqueness + equality lookups. Contact::hashEmail + a saving hook maintain it. Rewrote every email equality site (LiveChat, Personalization, portal DocumentResource, ContactListController search/autocomplete, model scopeSearch) to the blind index; table email search is now exact. An idempotent PiiEncryptionBackfill encrypts + hashes existing rows in the migration. Money columns stay plaintext (forecasting/sort/range). MySQL-verified (the text change needed both email indexes dropped first).
Not up to standards ⛔🔴 Issues
|
| Category | Results |
|---|---|
| Security | 1 high |
🟢 Metrics 16 complexity · 0 duplication
Metric Results Complexity 16 Duplication 0
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
This was referenced Jul 12, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First slice of the PII encrypt-at-rest epic — the final feature epic toward 2.0.0 (design:
docs/superpowers/specs/2026-07-10-pii-encrypt-at-rest-design.md).What
Contact.emailis now encrypted at rest (encryptedcast; column widened totext). Because encryption uses a random IV, the plaintextunique+ lookup indexes can't survive it — a deterministic blind indexemail_hash(HMAC-SHA256 of the normalised email,unique) carries both uniqueness and equality lookups.Contact::hashEmail()+ asavinghook keepemail_hashin sync; it's guarded from mass-assignment.LiveChatService,PersonalizationService, the portalDocumentResource(customer→contact match),ContactListController(search + autocomplete), and the modelscopeSearch. Table email search is now exact (partialLIKEis gone — documented tradeoff; masking already gated it off forfree).App\Support\PiiEncryptionBackfill(idempotent) encrypts + hashes existing rows in the migration.Money columns stay plaintext (they need DB sort/range/SUM for forecasting; masking already hides them from
free).Notable
MySQL-verified caught a real bug: the
textchange needed both email indexes (contacts_email_uniqueandcontacts_email_index) dropped first — MySQL 1170, which sqlite masked.Tests
New
ContactEmailEncryptionTest(encrypted-at-rest, blind index deterministic + case-insensitive, lookup, uniqueness, idempotent backfill). Reconciled the query/assert regressions ( → ; email search → exact). Full suite 1138 passed / 1 skipped; PHPStan 0-new; Pint clean; MySQL-verified.Next
Slice 2 — encrypt
Contact.phone_number+Company.phone_number(no blind index; drop theirLIKEsearch).VERSION →
2.0.0-rc.1.