Skip to content

feat: add encrypted casts for model fields and entities#10357

Open
memleakd wants to merge 3 commits into
codeigniter4:4.8from
memleakd:feat/encrypted-casts
Open

feat: add encrypted casts for model fields and entities#10357
memleakd wants to merge 3 commits into
codeigniter4:4.8from
memleakd:feat/encrypted-casts

Conversation

@memleakd

Copy link
Copy Markdown
Contributor

Description

This PR adds an encrypted cast type for Model Field Casting and Entity Property Casting.

It uses the existing Encryption service to encrypt string values before storage and decrypt them when they are read back:

protected array $casts = [
    'secret_note' => 'encrypted',
];

The encrypted value is stored as Base64-encoded ciphertext, so it can be safely saved in string/text database columns even when the encryption handler returns binary data.

The implementation is intentionally limited to string values, with nullable values supported through ?encrypted. The docs cover the important caveats around column size, querying encrypted values, passwords, serialization, and key rotation.

Tests cover the cast itself, nullable values, invalid payloads, failed decryption, previous keys, DataConverter usage, and Entity raw/plain value behavior.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

- Add encrypted DataCaster support for Model fields and Entity properties.
- Store encrypted values as Base64-encoded ciphertext using the Encryption service.
- Document storage, validation, query, serialization, and key rotation caveats.
- Add focused tests for encryption, decryption, nullable values, previous keys, and invalid payloads.

Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label Jun 30, 2026
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@michalsn michalsn added the enhancement PRs that improve existing functionalities label Jun 30, 2026

@patel-vansh patel-vansh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice Addition. Thanks!

Comment thread system/DataCaster/Cast/EncryptedCast.php Outdated
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch. enhancement PRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants