Skip to content
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

Encrypts can’t decrypt binary column? #102

Open
emilkarl opened this issue Oct 23, 2023 · 4 comments
Open

Encrypts can’t decrypt binary column? #102

emilkarl opened this issue Oct 23, 2023 · 4 comments

Comments

@emilkarl
Copy link

emilkarl commented Oct 23, 2023

Tried to use encryption on Rails 7.0.2 for my solid cache. Seems like it doesn’t work properly, it encrypts and stores the rows but it can’t be decrypted. It raises exception ActiveRecord::Encryption::Errors::Decrypt.

I changed my value column to be a text and that solves the issue but not sure that is the correct approach? Should it work with the binary column that comes with the default migration? In that case, am I missing some configuration that is not specified in the readme? 🤷‍♂️

Using postgresql.

All the best,
Emil

Working migration

class CreateSolidCacheEntries < ActiveRecord::Migration[7.0]
  def change
    create_table :solid_cache_entries do |t|
      t.binary :key, null: false, limit: 1024
      t.text :value, null: false, limit: 512.megabytes
      t.datetime :created_at, null: false

      t.index :key, unique: true
    end
  end
end
@formigarafa
Copy link

That's probably right because the encryption will create a json in which one of the keys is rhe encrypted value.
Of course binary can also hold that string but maybe, if that is supposed to work, there is some type conversion trickery missing or extra.

@emilkarl
Copy link
Author

emilkarl commented Oct 31, 2023

Sounds fine. Maybe this should be added to the docs. That if you want to use encrypts your value column have to be a text 🤷‍♂️

@skatkov
Copy link
Contributor

skatkov commented Nov 1, 2023

Haven't taken a deep dive into encryption/decryption code. But it might be, that value needs to unescaped before decrypting.

PG::Connection.unescape_bytea(value)

@djmb
Copy link
Collaborator

djmb commented Jan 30, 2024

The issue here is that Rails doesn't really support encrypting binary columns. I didn't realise this before because it just happens to work for Mysql (and SQLite). Not for PostgreSQL though.

I've raised rails/rails#50920 to add proper support. Hopefully that will resolve it. In the meantime I'll update the docs to highlight this.

Another note is that there's a potential space saving here. ActiveRecord Encryption serializes messages to JSON with a Base64 encrypted payload which means they use about 30% more space than a binary encoding. Once binary data is properly supported, we could use a MessagePack based serializer for binary columns instead.

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

No branches or pull requests

4 participants