Skip to content

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

Merged
paulbalandan merged 3 commits into
codeigniter4:4.8from
memleakd:feat/encrypted-casts
Jul 7, 2026
Merged

feat: add encrypted casts for model fields and entities#10357
paulbalandan merged 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

@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label Jun 30, 2026
@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
@github-actions github-actions Bot added the stale Pull requests with conflicts label Jul 7, 2026
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

👋 Hi, @memleakd!

We detected conflicts in your PR against the base branch 🙊
You may want to sync 🔄 your branch with upstream!

Ref: Syncing Your Branch

memleakd added 2 commits July 7, 2026 12:20
- 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>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@memleakd memleakd force-pushed the feat/encrypted-casts branch from a42b779 to 63ed7a7 Compare July 7, 2026 09:20
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@memleakd memleakd force-pushed the feat/encrypted-casts branch from 63ed7a7 to 39d71d2 Compare July 7, 2026 09:30
@michalsn michalsn removed the stale Pull requests with conflicts label Jul 7, 2026
@paulbalandan paulbalandan merged commit 18ee79c into codeigniter4:4.8 Jul 7, 2026
57 checks passed
@paulbalandan

Copy link
Copy Markdown
Member

Thank you, @memleakd

@memleakd memleakd deleted the feat/encrypted-casts branch July 7, 2026 13:49
@memleakd

memleakd commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Thank you @michalsn, @paulbalandan, @patel-vansh, @datamweb

@memleakd memleakd mentioned this pull request Jul 9, 2026
5 tasks
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