What does Active Record Encryption protect a Rails hospital app's national ID column against, and what does it leave exposed?
answer
- ciphertext at rest, plaintext in memory
- dumps, backups, read-only SQL access
- auto filter_parameters and filter_attributes
- anyone with the app's credentials decrypts
- copies outside the column stay plain
basics
~20 sIt keeps the column unreadable to anyone who sees only the database, such as dumps, backups or read-only SQL, and filters it from logs and inspect. Code running with the app's keys, and copies outside the column, still see plain text.
solid answer
~50 s`encrypts` turns the column into AES-256-GCM ciphertext, so a leaked dump, a backup, a replica or someone with read-only SQL access sees envelopes, not national IDs, and tampering is detected by the auth tag. Rails also adds encrypted attributes to `filter_parameters` and the model's `filter_attributes`, so request logs and `inspect` show `[FILTERED]`. It does **not** protect against anything that runs as the application: a compromised server, a console session or a script with `RAILS_MASTER_KEY` decrypts transparently. Deterministic columns reveal which rows share a value, NULLs stay NULL, and ciphertext length tracks plaintext length roughly. Copies you make yourself, such as job arguments, cache entries, exports or a value interpolated into a log line, are plain text. Treat it as protection for data at rest in the database, layered with access control around the app and its keys.
code
ruby · 9 linesclass Patient < ApplicationRecord
encrypts :national_id, deterministic: true
end
# Leaks the plain value into the job backend's storage
RecordsExportJob.perform_later(patient.national_id)
# Passes only the record; the job loads and decrypts it when it runs
RecordsExportJob.perform_later(patient)go deeper
Recall that encryption protects the stored column and that the app itself still reads plain values.
Explain the database-versus-application boundary, the built-in filtering, and leaks through job arguments, caches and hand-written logs.
Show how you would harden a real app around it: key access, console controls, passing ids to jobs, and treating deterministic leakage per field.
Weigh application-level encryption against database or disk encryption and access controls when deciding a data-protection strategy.
## The boundary it draws Active Record Encryption encrypts **in the application process**, just before Active Record writes, and decrypts just after it reads. The keys live in the app's credentials and are loaded into the process. That one fact decides the threat model: anything that only sees the **database** sees ciphertext; anything that runs **as the application** sees plain text. ## What it protects against | Threat | Protected? | Why | |---|---|---| | A database dump or backup leaks | yes | the column holds AES-256-GCM envelopes | | Someone gets read-only SQL access or a replica | yes | the same ciphertext | | A DBA or analyst browsing tables | yes | values are unreadable without app keys | | Someone edits a ciphertext in the database | detected | the GCM authentication tag fails on read | | Request logs printing submitted params | yes, by default | encrypted attributes join `config.filter_parameters` | | `inspect` or console output of a record | masked | encrypted attributes join the model's `filter_attributes` | The last two are Rails conveniences controlled by `config.active_record.encryption.add_to_filter_parameters` (default true) and `excluded_from_filter_parameters`. They hide the value in output; they are not encryption. ## What it does not protect against - **Anyone who can run code with the keys.** A compromised web server, a Rails console in production or a one-off script holding `RAILS_MASTER_KEY` decrypts every record transparently. `inspect` filtering does not stop `patient.national_id`. - **Equality in deterministic columns.** Deterministic encryption shows which rows share a value and how often. - **Metadata.** `NULL` is stored as `NULL`, so readers learn which patients have no ID on file; ciphertext length grows with plaintext length, so very different lengths are distinguishable. - **Copies outside the column.** Rails encrypts the attribute where it is stored, nothing else: 1. passing `patient.national_id` to `perform_later` puts the plain value into the job backend's storage; 2. caching a rendered fragment or a `Rails.cache` entry containing it stores plain text in the cache; 3. CSV exports, analytics events and emails carry plain text; 4. a log line you build yourself, such as `logger.info("Looking up #{id}")`, bypasses parameter filtering. - **Application bugs.** An authorization flaw that shows another patient's record returns decrypted data, because the app decrypted it legitimately. ## Related settings that change the picture - `config.active_record.encryption.encrypt_fixtures` (default false) encrypts fixture values when tests load them, so test data matches production's shape. - `ActiveRecord::Encryption.protecting_encrypted_data { ... }` makes encrypted attributes read as ciphertext inside the block, which tools can use to limit what a console session shows. - Action Text content has its own `encrypted: true` option, separate from `encrypts` on plain columns. ## How to use it well 1. Encrypt fields whose exposure in a dump would be harmful: national IDs, diagnoses, contact details. 2. Keep keys only where the app runs, per environment, and rotate them with a plan. 3. Pass record ids, not sensitive values, to jobs and caches, and let the job load the record. 4. Restrict production console and script access, because that is where the keys work. 5. Keep authorization checks strong; encryption does not replace them. ## What interviewers check - That you can say precisely which boundary the feature protects: the database, not the application. - That you know the built-in filtering is masking, not protection. - That you can list the leaks outside the column, especially job arguments and caches.
- Does [FILTERED] in the Rails console mean a developer cannot read encrypted values there?No. The attribute is added to the model's `filter_attributes`, so `inspect` masks it, but calling `patient.national_id` decrypts it normally because the console has the keys. Limiting what a console can read needs process controls, or code run inside `ActiveRecord::Encryption.protecting_encrypted_data`, which makes encrypted attributes read as ciphertext.
- An attacker exploits SQL injection in a search action; does Active Record Encryption help?Partly. Whatever the injected SQL returns from an encrypted column is ciphertext, because decryption happens in Active Record, not in the database. But if the injection can make the app load records and render them, the app decrypts them as usual. Fixing the injection is still required.
It works like a safe-deposit vault whose clerk holds the key: a burglar who steals the boxes gets only locked boxes, but anyone who can walk up to the counter as the clerk gets every box opened, and papers a customer copied and carried out of the vault were never locked at all.
- Encrypted attributes cannot be read in a production console.
- [FILTERED] in logs means the value is encrypted in the log.
- Encrypting the column also encrypts it in job arguments and caches.
- A compromised app server only sees ciphertext.
- NULL values are encrypted, so readers cannot tell which rows are empty.