Skip to content
This repository was archived by the owner on Apr 7, 2021. It is now read-only.

Type error: substr() expects parameter 2 to be integer, boolean given #19

Closed
WouterFlorijn opened this issue May 9, 2018 · 3 comments
Closed

Comments

@WouterFlorijn
Copy link

When using the decryptedAttribute function on an encrypted value, I get the following error:

Symfony\Component\Debug\Exception\FatalThrowableError : Type error: substr() expects parameter 2 to be integer, boolean given

at C:\wamp\www\lifestream\vendor\austinheap\laravel-database-encryption\src\Traits\HasEncryptedAttributes.php: 171

I am encrypting/decrypting data from a migration to ensure that existing data will still work.

My migration looks like this:

class EncryptData extends Migration
{
    public function up()
    {
        $this->encryptModels(SomeModel::all());
    }

    public function down()
    {
        $this->decryptModels(SomeModel::all());
    }

    private function encryptModels($models)
    {
        foreach ($models as $model)
        {
            if (!property_exists($model, 'encrypted'))
                return;
            foreach ($model->encrypted as $key)
            {
                $value = $model->getAttributeRaw($key);
                $value = $model->encryptedAttribute($value);
                $model->setAttributeRaw($key, $value);
                $model->save();
            }
        }
    }

    private function decryptModels($models)
    {
        foreach ($models as $model)
        {
            if (!property_exists($model, 'encrypted'))
                return;
            foreach ($model->encrypted as $key)
            {
                $value = $model->getAttributeRaw($key);
                $value = $model->decryptedAttribute($value);
                $model->setAttributeRaw($key, $value);
                $model->save();
            }
        }
    }
}

The encryption part works fine, but rolling back the migration throws the exception. Also, the encrypted values look like this:

��__LARAVEL-DATABASE-ENCRYPTED-VERSION-00-01-00__��version�VERSION-00-01-00��type�string[native]��eyJpdiI6IjJEYTJuRkdMeTFTQ1pCNXRCanNvUUE9PSIsInZhbHVlIjoiYlo0d2NIOFwvd1BtNER6OUZIOUpIWGc9PSIsIm1hYyI6IjQ5Mzk2OTNlNTAyZGI1NmJkMmUyZjQyNDZlZWYwYTMzYjM3MzRhOTU2NzAzMDc0OGI1Y2Y4ZjczZTk2ZDdhNjQifQ==

Are the �� characters supposed to look like this?

@austinheap
Copy link
Owner

Hey @WouterFlorijn --

Are the �� characters supposed to look like this?

Yes, they are character codes used to identify the header and it's various key/value pairs. Do the models function normally when you enable encryption? I.e.: do they encrypt/decrypt automatically to the database correctly? Those headers should never be visible to the model, as they're stripped off before actual cryptographic operations.

It's important to note: all calls to decryptedAttribute($value) must have the header present in the $value argument. Without the header it doesn't know what it's looking at. Based on your error (substr() expects parameter 2 to be integer, boolean given), it seems it's not able to find/parse the header.

@WouterFlorijn
Copy link
Author

@austinheap I found out I was decrypting the data twice. I made my own method to get the raw encrypted string, but it didn't work and returned the decrypted string. Then I tried to decrypt that, which obviously failed.

Perhaps a good idea to throw a clear exception like Encryption header missing. instead of something obscure like the error I got. But other than that it's working fine.

austinheap added a commit that referenced this issue May 23, 2018
@austinheap
Copy link
Owner

@WouterFlorijn Just pushed a commit that'll throw DecryptException if the header is missing:

throw_if(! array_key_exists('stop', $characters), DecryptException::class, 'Cannot decrypt model attribute not originally encrypted by this package!');

throw_if($offset === false, DecryptException::class, 'Cannot decrypt model attribute with no package header!');

Re: migration issues: take a look at src/Console/Commands/MigrateEncryptionCommand.php -- it's heavily unit tested so you should be able to lift any migration logic you need from that.

@austinheap austinheap added bug and removed question labels May 23, 2018
austinheap added a commit that referenced this issue Nov 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants