Skip to content

Fatal casting issues #2703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stubbo opened this issue Jan 9, 2024 · 1 comment
Closed

Fatal casting issues #2703

stubbo opened this issue Jan 9, 2024 · 1 comment

Comments

@stubbo
Copy link
Contributor

stubbo commented Jan 9, 2024

  • Laravel-mongodb Version: >= 4.1.0
  • PHP Version: Any (I'm using 8.2.14)
  • Database Driver & Version: Any (I'm using MongoDB, server: 6.0.6, PHP driver: 1.16.2)

Description:

Models sometimes fail to set attributes because the MongoDB\Laravel\Eloquent\Model::setAttribute calls castAttribute, which is intended to be called when going from database to PHP and not PHP to database.

The main cast which has this issue is any of the encryption ones, the model will attempt to decrypt something which isn't encrypted.

I have started work on a PR to fix this, hopefully I can get it finished today/tomorrow.

Steps to reproduce

  1. Create a model with an encrypted cast
class EncryptedModel extends \MongoDB\Laravel\Eloquent\Model {
    public $fillable = ['fieldName'];
    public $casts = ['fieldName' => 'encrypted'];
}
  1. Try to set the encrypted value, this is when it should error with: Illuminate\Contracts\Encryption\DecryptException: The payload is invalid.
EncryptedModel::make(['fieldName' => 'willError']);
  1. 😭

Expected behaviour

The value should be set without error.
$model->fieldName should return a decrypted string.
$model->getRawOriginal('fieldName') should return an encrypted string.

Actual behaviour

Anything which calls setAttribute will error which includes calls to: make, create, update
Here is a working test which shows this issue: https://github.com/stubbo/laravel-mongodb/actions/runs/7462021749

@richardfila
Copy link
Contributor

Hit this issue today when switching a Laravel project from jenssegers/mongodb to mongodb/laravel-mongodb.
Showstopping error; had to switch back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants