Understanding Rails · Season 3: Active Record in depth
Encrypting attributes
Episode 25 · deep dive · about 4 minutes · Rails 8.1 · by Tommy A. Caruso
How was this episode?
The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.
Not endorsed yet. Are you a Rails developer? What's an endorsement?
Endorse this version To endorse: create an account, confirm your email, then link GitHub.
Active Record encryption for the data you must protect.
Free to watch. If it helped, support the series.
Transcript
0:00 Encrypting attributes
Episode twenty five. Encrypting attributes. Some data you must protect, even from someone who gets a copy of your database. Rails can encrypt one attribute at a time, and the rest of your code doesn't change. Today: how it works, and what it costs.
0:20 Keys
Encryption needs keys. Run bin rails db encryption init. It prints three values: a primary key, a deterministic key, and a key derivation salt. We've hidden them here, because they're secrets. They go in your credentials, the encrypted file from episode sixteen.
0:39 encrypts
Then, one line in the model. Encrypts body. That's all it takes.
0:44 What the database holds
Create a note with the vet's phone number in it. Read the body, and it's plain text. Now ask the database directly. It holds a little JSON: the encrypted text, and a header with what's needed to decrypt it. Someone with a copy of the database sees only that.
1:02 Filtered from logs
Rails keeps it out of your logs, too. Inspect the note, and the body shows as filtered.
1:08 Bigger values
The stored value is bigger than the text. Twenty two characters became one hundred and two. A short string column can overflow, so give encrypted columns room.
1:20 No searching
Here's the first cost. You can't search encrypted text. Where body equals the exact text finds nothing. The about scope, with like, finds nothing either. Each encryption mixes in a random value, so the same text never looks the same twice, and the database has nothing to compare.
1:39 Old plain text
The second cost is old data. Add encrypts to a column that already holds plain text, and reading an old note raises a decryption error.
1:49 support_unencrypted_data
Support unencrypted data, in the app's config, lets Rails read plain text while you move over. Note ten reads normally again, and encrypted attribute says false.
2:01 Encrypting old records
Then call encrypt on each record. Note ten is ciphertext in the database now, and it still reads as buy milk for the office. Do that for every record, in a task, and then turn the setting off.
2:15 Deterministic encryption
Some values you do need to look up, like an email address. Deterministic true makes the same value always encrypt the same way. Two notebooks with the same owner email store exactly the same ciphertext.
2:29 Looking it up
So a where works. Rails encrypts the value you're searching for, and compares ciphertext with ciphertext. Both notebooks come back.
2:39 downcase
But it only finds an exact match. Search with capitals, and you get nil. Add down-case true, and Rails lowercases the value before it's encrypted, so the lookup works either way. The original capitals are gone, though.
2:54 Deterministic is weaker
Deterministic is weaker. Anyone with the database can see which rows share a value, even without reading it. So use it only for what you must look up, and leave everything else random.
3:07 Guard the keys
And guard those keys. Lose the primary key, and every value encrypted with it is gone for good. So back up the master key that opens your credentials, and give production its own.
3:19 Rotating a key
Keys can still be changed. Make the primary key a list: the old key, then a new one. Rails encrypts new values with the last key, and tries each key when it reads. A note written before the change still reads. A note written after it uses the new key.
3:37 Dropping the old key
Remove the old key too soon, and the older note fails with a decryption error, while the newer one reads fine. So first call encrypt on the old records. That writes them again with the new key. Then the old key can go, and both notes read.
3:53 The rules
So: encrypt the attributes you must protect. Keep them random, unless you have to search them. Plan for the data that's already there. And keep the keys safe.
4:03 Next
Next: deprecated associations.
Check your understanding
-
Note has encrypts :body with no options. What does Note.where(body: "Vet: 555-0100").count return, when that note exists?
- 1
- 0
- It raises a decryption error
Show answer
0. By default each encryption mixes in a random value, so the stored text never matches. The query finds nothing. Source: Active Record Encryption: deterministic and non-deterministic encryption (Rails Guides 8.1).
-
Notebook has encrypts :owner_email, deterministic: true, downcase: true. What is the cost?
- You can no longer query by owner_email
- Each save stores two copies of the email
- The original capitals are lost
Show answer
The original capitals are lost. downcase lowercases the value before it's encrypted. Lookups ignore case, but the original case is gone. Source: Active Record Encryption: ignoring case (Rails Guides 8.1).
-
You add encrypts :body to a column that already holds plain text. Which setting lets old notes read while you encrypt them?
- support_unencrypted_data = true
- deterministic: true
- add_to_filter_parameters = false
- downcase: true
Show answer
support_unencrypted_data = true. With support for unencrypted data on, reading plain text succeeds instead of raising. Turn it off once every record is encrypted. Source: Active Record Encryption: support for unencrypted data (Rails Guides 8.1).
-
The primary_key in your credentials becomes a list: the old key, then a new one. Which key encrypts new values?
- The first key in the list
- Each key in turn
- Both keys, one after the other
- The last key in the list
Show answer
The last key in the list. The last key encrypts new content, and every key is tried when reading. Re-encrypt old records before you remove a key. Source: Active Record Encryption: rotating keys (Rails Guides 8.1).
Sources
Every example in this episode was run on Rails 8.1.4.1 and Ruby 4.0.2.