From 76085df046aac0edfdf5c0dce00b2000f418af9a Mon Sep 17 00:00:00 2001 From: Jasper Mayone Date: Wed, 18 Nov 2026 11:45:58 -0500 Subject: [PATCH 1/3] fix: encrypt postal addresses with lockbox --- app/models/address.rb | 65 +++++++++------- app/models/user_address.rb | 50 ++++++------- app/serializers/address_serializer.rb | 50 ++++++------- ...000_add_ciphertext_columns_to_addresses.rb | 16 ++++ ...261118120100_encrypt_existing_addresses.rb | 29 +++++++ db/schema.rb | 16 +++- spec/factories/addresses.rb | 50 ++++++------- .../encrypt_existing_addresses_spec.rb | 36 +++++++++ spec/models/address_spec.rb | 75 ++++++++++++------- 9 files changed, 260 insertions(+), 127 deletions(-) create mode 100644 db/migrate/20261118120000_add_ciphertext_columns_to_addresses.rb create mode 100644 db/migrate/20261118120100_encrypt_existing_addresses.rb create mode 100644 spec/migrations/encrypt_existing_addresses_spec.rb diff --git a/app/models/address.rb b/app/models/address.rb index 84252c35..9ef038c2 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -5,31 +5,31 @@ # Table name: addresses # Database name: primary # -# id :bigint not null, primary key -# address_type :enum default("shipping"), not null -# addressable_type :string not null -# city :string -# contact_email :string -# contact_first_name :string -# contact_last_name :string -# contact_name :string -# contact_phone_number :string -# country :string(2) -# latitude :float -# line1 :string -# line2 :string -# line3 :string -# longitude :float -# nickname :string -# postal_code :string -# residential :boolean default(FALSE), not null -# shipping_notes :text -# state :string -# supports_weekend_deliveries :boolean default(FALSE), not null -# type :string default("Address"), not null -# created_at :datetime not null -# updated_at :datetime not null -# addressable_id :bigint not null +# id :bigint not null, primary key +# address_type :enum default("shipping"), not null +# addressable_type :string not null +# city_ciphertext :text +# contact_email_ciphertext :text +# contact_first_name_ciphertext :text +# contact_last_name_ciphertext :text +# contact_name_ciphertext :text +# contact_phone_number_ciphertext :text +# country :string(2) +# latitude_ciphertext :text +# line1_ciphertext :text +# line2_ciphertext :text +# line3_ciphertext :text +# longitude_ciphertext :text +# nickname_ciphertext :text +# postal_code_ciphertext :text +# residential :boolean default(FALSE), not null +# shipping_notes_ciphertext :text +# state :string +# supports_weekend_deliveries :boolean default(FALSE), not null +# type :string default("Address"), not null +# created_at :datetime not null +# updated_at :datetime not null +# addressable_id :bigint not null # # Indexes # @@ -49,6 +49,18 @@ class Address < ApplicationRecord # STI configuration self.inheritance_column = "type" + # Encrypted with Lockbox. state and country stay in plain text for residency + # checks and aggregates. The plain-text columns are ignored until a later + # migration drops them. + ENCRYPTED_FIELDS = %i[nickname contact_name contact_first_name contact_last_name contact_email + contact_phone_number line1 line2 line3 city postal_code shipping_notes].freeze + ENCRYPTED_COORDINATES = %i[latitude longitude].freeze + + self.ignored_columns += (ENCRYPTED_FIELDS + ENCRYPTED_COORDINATES).map(&:to_s) + + has_encrypted(*ENCRYPTED_FIELDS) + has_encrypted(*ENCRYPTED_COORDINATES, type: :float) + # Standard address field validations validates :line1, presence: true, unless: :allow_partial_address validates :city, presence: true @@ -103,7 +115,8 @@ def country_name end def self.ransackable_attributes(_auth_object = nil) - ["id", "nickname", "line1", "line2", "line3"] + # The street lines are encrypted, so SQL cannot search them. + ["id"] end private diff --git a/app/models/user_address.rb b/app/models/user_address.rb index 71948200..bd20ccbc 100644 --- a/app/models/user_address.rb +++ b/app/models/user_address.rb @@ -5,31 +5,31 @@ # Table name: addresses # Database name: primary # -# id :bigint not null, primary key -# address_type :enum default("shipping"), not null -# addressable_type :string not null -# city :string -# contact_email :string -# contact_first_name :string -# contact_last_name :string -# contact_name :string -# contact_phone_number :string -# country :string(2) -# latitude :float -# line1 :string -# line2 :string -# line3 :string -# longitude :float -# nickname :string -# postal_code :string -# residential :boolean default(FALSE), not null -# shipping_notes :text -# state :string -# supports_weekend_deliveries :boolean default(FALSE), not null -# type :string default("Address"), not null -# created_at :datetime not null -# updated_at :datetime not null -# addressable_id :bigint not null +# id :bigint not null, primary key +# address_type :enum default("shipping"), not null +# addressable_type :string not null +# city_ciphertext :text +# contact_email_ciphertext :text +# contact_first_name_ciphertext :text +# contact_last_name_ciphertext :text +# contact_name_ciphertext :text +# contact_phone_number_ciphertext :text +# country :string(2) +# latitude_ciphertext :text +# line1_ciphertext :text +# line2_ciphertext :text +# line3_ciphertext :text +# longitude_ciphertext :text +# nickname_ciphertext :text +# postal_code_ciphertext :text +# residential :boolean default(FALSE), not null +# shipping_notes_ciphertext :text +# state :string +# supports_weekend_deliveries :boolean default(FALSE), not null +# type :string default("Address"), not null +# created_at :datetime not null +# updated_at :datetime not null +# addressable_id :bigint not null # # Indexes # diff --git a/app/serializers/address_serializer.rb b/app/serializers/address_serializer.rb index 4997be72..da6fef25 100644 --- a/app/serializers/address_serializer.rb +++ b/app/serializers/address_serializer.rb @@ -5,31 +5,31 @@ # Table name: addresses # Database name: primary # -# id :bigint not null, primary key -# address_type :enum default("shipping"), not null -# addressable_type :string not null -# city :string -# contact_email :string -# contact_first_name :string -# contact_last_name :string -# contact_name :string -# contact_phone_number :string -# country :string(2) -# latitude :float -# line1 :string -# line2 :string -# line3 :string -# longitude :float -# nickname :string -# postal_code :string -# residential :boolean default(FALSE), not null -# shipping_notes :text -# state :string -# supports_weekend_deliveries :boolean default(FALSE), not null -# type :string default("Address"), not null -# created_at :datetime not null -# updated_at :datetime not null -# addressable_id :bigint not null +# id :bigint not null, primary key +# address_type :enum default("shipping"), not null +# addressable_type :string not null +# city_ciphertext :text +# contact_email_ciphertext :text +# contact_first_name_ciphertext :text +# contact_last_name_ciphertext :text +# contact_name_ciphertext :text +# contact_phone_number_ciphertext :text +# country :string(2) +# latitude_ciphertext :text +# line1_ciphertext :text +# line2_ciphertext :text +# line3_ciphertext :text +# longitude_ciphertext :text +# nickname_ciphertext :text +# postal_code_ciphertext :text +# residential :boolean default(FALSE), not null +# shipping_notes_ciphertext :text +# state :string +# supports_weekend_deliveries :boolean default(FALSE), not null +# type :string default("Address"), not null +# created_at :datetime not null +# updated_at :datetime not null +# addressable_id :bigint not null # # Indexes # diff --git a/db/migrate/20261118120000_add_ciphertext_columns_to_addresses.rb b/db/migrate/20261118120000_add_ciphertext_columns_to_addresses.rb new file mode 100644 index 00000000..faa60435 --- /dev/null +++ b/db/migrate/20261118120000_add_ciphertext_columns_to_addresses.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# Postal addresses are personal data. The street, the postal code, the contact +# details and the coordinates move to Lockbox ciphertext columns. state and +# country stay in plain text, because residency checks and aggregates read them. +# The plain-text columns stay until a later migration drops them; the model +# ignores them from now on. +class AddCiphertextColumnsToAddresses < ActiveRecord::Migration[8.1] + def change + %i[nickname contact_name contact_first_name contact_last_name contact_email contact_phone_number line1 + line2 line3 city postal_code shipping_notes latitude longitude].each do |name| + add_column :addresses, :"#{name}_ciphertext", :text + end + end + +end diff --git a/db/migrate/20261118120100_encrypt_existing_addresses.rb b/db/migrate/20261118120100_encrypt_existing_addresses.rb new file mode 100644 index 00000000..2cfc3235 --- /dev/null +++ b/db/migrate/20261118120100_encrypt_existing_addresses.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +# Copies each plain-text address value into its Lockbox ciphertext column. +# Lockbox derives the key from the table name, so this local model encrypts +# with the same key as Address. The plain-text columns are not changed. +class EncryptExistingAddresses < ActiveRecord::Migration[8.1] + disable_ddl_transaction! + + class MigrationAddress < ActiveRecord::Base + self.table_name = "addresses" + self.inheritance_column = nil + + has_encrypted :nickname, :contact_name, :contact_first_name, :contact_last_name, :contact_email, + :contact_phone_number, :line1, :line2, :line3, :city, :postal_code, :shipping_notes, + migrating: true + has_encrypted :latitude, :longitude, type: :float, migrating: true + + end + + def up + MigrationAddress.reset_column_information + Lockbox.migrate(MigrationAddress, restart: true) + end + + def down + # The plain-text columns still hold the values, so there is nothing to undo. + end + +end diff --git a/db/schema.rb b/db/schema.rb index 82deb4bf..fe00e097 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2026_11_12_130000) do +ActiveRecord::Schema[8.1].define(version: 2026_11_18_120100) do # These are extensions that must be enabled in order to support this database enable_extension "fuzzystrmatch" enable_extension "pg_catalog.plpgsql" @@ -90,6 +90,20 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "shipping_notes" + t.text "nickname_ciphertext" + t.text "contact_name_ciphertext" + t.text "contact_first_name_ciphertext" + t.text "contact_last_name_ciphertext" + t.text "contact_email_ciphertext" + t.text "contact_phone_number_ciphertext" + t.text "line1_ciphertext" + t.text "line2_ciphertext" + t.text "line3_ciphertext" + t.text "city_ciphertext" + t.text "postal_code_ciphertext" + t.text "shipping_notes_ciphertext" + t.text "latitude_ciphertext" + t.text "longitude_ciphertext" t.index ["addressable_id"], name: "index_addresses_on_addressable_id" t.index ["addressable_type", "addressable_id", "type", "address_type"], name: "unique_address_per_addressable_type_and_address_type", unique: true t.index ["addressable_type", "addressable_id"], name: "index_addresses_on_addressable" diff --git a/spec/factories/addresses.rb b/spec/factories/addresses.rb index 90d3d769..acc5cdb4 100644 --- a/spec/factories/addresses.rb +++ b/spec/factories/addresses.rb @@ -5,31 +5,31 @@ # Table name: addresses # Database name: primary # -# id :bigint not null, primary key -# address_type :enum default("shipping"), not null -# addressable_type :string not null -# city :string -# contact_email :string -# contact_first_name :string -# contact_last_name :string -# contact_name :string -# contact_phone_number :string -# country :string(2) -# latitude :float -# line1 :string -# line2 :string -# line3 :string -# longitude :float -# nickname :string -# postal_code :string -# residential :boolean default(FALSE), not null -# shipping_notes :text -# state :string -# supports_weekend_deliveries :boolean default(FALSE), not null -# type :string default("Address"), not null -# created_at :datetime not null -# updated_at :datetime not null -# addressable_id :bigint not null +# id :bigint not null, primary key +# address_type :enum default("shipping"), not null +# addressable_type :string not null +# city_ciphertext :text +# contact_email_ciphertext :text +# contact_first_name_ciphertext :text +# contact_last_name_ciphertext :text +# contact_name_ciphertext :text +# contact_phone_number_ciphertext :text +# country :string(2) +# latitude_ciphertext :text +# line1_ciphertext :text +# line2_ciphertext :text +# line3_ciphertext :text +# longitude_ciphertext :text +# nickname_ciphertext :text +# postal_code_ciphertext :text +# residential :boolean default(FALSE), not null +# shipping_notes_ciphertext :text +# state :string +# supports_weekend_deliveries :boolean default(FALSE), not null +# type :string default("Address"), not null +# created_at :datetime not null +# updated_at :datetime not null +# addressable_id :bigint not null # # Indexes # diff --git a/spec/migrations/encrypt_existing_addresses_spec.rb b/spec/migrations/encrypt_existing_addresses_spec.rb new file mode 100644 index 00000000..293d31d9 --- /dev/null +++ b/spec/migrations/encrypt_existing_addresses_spec.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require "rails_helper" +require Rails.root.join("db/migrate/20261118120100_encrypt_existing_addresses") + +# The data migration: each plain-text address value is copied into its Lockbox +# ciphertext column, and Address decrypts it with its own key. +RSpec.describe EncryptExistingAddresses do + delegate :connection, to: :'ActiveRecord::Base' + + def migrate = ActiveRecord::Migration.suppress_messages { described_class.new.migrate(:up) } + + # Address ignores the plain-text columns, so set them with SQL, and clear the + # ciphertext the factory wrote, as rows from before the migration have none. + def set_plaintext(address, **columns) + assignments = columns.map { |column, value| "#{column} = #{connection.quote(value)}" } + clears = Address::ENCRYPTED_FIELDS.chain(Address::ENCRYPTED_COORDINATES).map { |column| "#{column}_ciphertext = NULL" } + connection.execute("UPDATE addresses SET #{(assignments + clears).join(', ')} WHERE id = #{address.id}") + end + + it "encrypts the plain-text values and keeps the plain-text columns" do + address = create(:shipping_address) + set_plaintext(address, line1: "1 Main St", line2: nil, city: "Burlington", postal_code: "05401", + contact_name: "Ada", contact_email: "ada@patchworklabs.org", shipping_notes: "Back door", + latitude: 44.47, longitude: -73.21) + + migrate + + expect(Address.find(address.id)).to have_attributes( + line1: "1 Main St", line2: nil, city: "Burlington", postal_code: "05401", contact_name: "Ada", + contact_email: "ada@patchworklabs.org", shipping_notes: "Back door", latitude: 44.47, longitude: -73.21, + state: "IL" + ) + expect(connection.select_value("SELECT line1 FROM addresses WHERE id = #{address.id}")).to eq("1 Main St") + end +end diff --git a/spec/models/address_spec.rb b/spec/models/address_spec.rb index 82b9a432..349da02c 100644 --- a/spec/models/address_spec.rb +++ b/spec/models/address_spec.rb @@ -5,31 +5,31 @@ # Table name: addresses # Database name: primary # -# id :bigint not null, primary key -# address_type :enum default("shipping"), not null -# addressable_type :string not null -# city :string -# contact_email :string -# contact_first_name :string -# contact_last_name :string -# contact_name :string -# contact_phone_number :string -# country :string(2) -# latitude :float -# line1 :string -# line2 :string -# line3 :string -# longitude :float -# nickname :string -# postal_code :string -# residential :boolean default(FALSE), not null -# shipping_notes :text -# state :string -# supports_weekend_deliveries :boolean default(FALSE), not null -# type :string default("Address"), not null -# created_at :datetime not null -# updated_at :datetime not null -# addressable_id :bigint not null +# id :bigint not null, primary key +# address_type :enum default("shipping"), not null +# addressable_type :string not null +# city_ciphertext :text +# contact_email_ciphertext :text +# contact_first_name_ciphertext :text +# contact_last_name_ciphertext :text +# contact_name_ciphertext :text +# contact_phone_number_ciphertext :text +# country :string(2) +# latitude_ciphertext :text +# line1_ciphertext :text +# line2_ciphertext :text +# line3_ciphertext :text +# longitude_ciphertext :text +# nickname_ciphertext :text +# postal_code_ciphertext :text +# residential :boolean default(FALSE), not null +# shipping_notes_ciphertext :text +# state :string +# supports_weekend_deliveries :boolean default(FALSE), not null +# type :string default("Address"), not null +# created_at :datetime not null +# updated_at :datetime not null +# addressable_id :bigint not null # # Indexes # @@ -54,4 +54,29 @@ it "defines the address_type enum values" do expect(described_class.address_types.keys).to include("shipping", "billing") end + + describe "encryption at rest" do + let(:address) do + create(:shipping_address, line1: "1 Main St", postal_code: "05401", contact_name: "Ada", + contact_phone_number: "+18025550100", latitude: 44.47, longitude: -73.21) + end + + def row = ActiveRecord::Base.connection.select_one("SELECT * FROM addresses WHERE id = #{address.id}") + + it "writes the street, contact and coordinates only as ciphertext" do + expect(row).to include("line1" => nil, "postal_code" => nil, "contact_name" => nil, "contact_phone_number" => nil, + "latitude" => nil, "longitude" => nil) + expect(row["line1_ciphertext"]).to be_present + expect(row["line1_ciphertext"]).not_to include("Main") + end + + it "keeps state and country in plain text" do + expect(row).to include("state" => "IL", "country" => "US") + end + + it "reads the values back" do + expect(described_class.find(address.id)).to have_attributes(line1: "1 Main St", postal_code: "05401", + latitude: 44.47, longitude: -73.21) + end + end end From a3aea78fd61b4ad81a6bcd1b175b5ea9faaa4519 Mon Sep 17 00:00:00 2001 From: Jasper Mayone Date: Wed, 18 Nov 2026 12:02:09 -0500 Subject: [PATCH 2/3] fix: make address backfill safe to rerun and filter address params --- config/initializers/filter_parameter_logging.rb | 5 ++++- ...20000_add_ciphertext_columns_to_addresses.rb | 16 +++++++++++----- ...20261118120100_encrypt_existing_addresses.rb | 5 ++++- .../encrypt_existing_addresses_spec.rb | 17 +++++++++++++++++ 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb index fc408304..78883b97 100644 --- a/config/initializers/filter_parameter_logging.rb +++ b/config/initializers/filter_parameter_logging.rb @@ -19,5 +19,8 @@ :reason, :evidence_url, :voucher, :outcome, # Key custody: a key holder's share of the master key, the wrapped copies # and the WebAuthn answers must never reach the logs (see KeyCustody). - :share, :ciphertext, :prf, :credential, :wrapped + :share, :ciphertext, :prf, :credential, :wrapped, + # Postal addresses (shipping_address_attributes, billing_address_attributes) + # are encrypted at rest, so they must not reach the logs either. + :address_attributes ] diff --git a/db/migrate/20261118120000_add_ciphertext_columns_to_addresses.rb b/db/migrate/20261118120000_add_ciphertext_columns_to_addresses.rb index faa60435..5d190cbd 100644 --- a/db/migrate/20261118120000_add_ciphertext_columns_to_addresses.rb +++ b/db/migrate/20261118120000_add_ciphertext_columns_to_addresses.rb @@ -6,11 +6,17 @@ # The plain-text columns stay until a later migration drops them; the model # ignores them from now on. class AddCiphertextColumnsToAddresses < ActiveRecord::Migration[8.1] - def change - %i[nickname contact_name contact_first_name contact_last_name contact_email contact_phone_number line1 - line2 line3 city postal_code shipping_notes latitude longitude].each do |name| - add_column :addresses, :"#{name}_ciphertext", :text - end + COLUMNS = %i[nickname contact_name contact_first_name contact_last_name contact_email contact_phone_number line1 + line2 line3 city postal_code shipping_notes latitude longitude].freeze + + def up + COLUMNS.each { |name| add_column :addresses, :"#{name}_ciphertext", :text } + end + + # After the new code runs, the ciphertext is the only copy of a new or edited + # address. Dropping these columns would lose it. + def down + raise ActiveRecord::IrreversibleMigration end end diff --git a/db/migrate/20261118120100_encrypt_existing_addresses.rb b/db/migrate/20261118120100_encrypt_existing_addresses.rb index 2cfc3235..b3a94d74 100644 --- a/db/migrate/20261118120100_encrypt_existing_addresses.rb +++ b/db/migrate/20261118120100_encrypt_existing_addresses.rb @@ -19,7 +19,10 @@ class MigrationAddress < ActiveRecord::Base def up MigrationAddress.reset_column_information - Lockbox.migrate(MigrationAddress, restart: true) + # restart: false fills only the rows with no ciphertext yet. A row that the + # new code has written already has ciphertext and NULL plain text, and a + # restart would wipe it. + Lockbox.migrate(MigrationAddress, restart: false) end def down diff --git a/spec/migrations/encrypt_existing_addresses_spec.rb b/spec/migrations/encrypt_existing_addresses_spec.rb index 293d31d9..330f01a0 100644 --- a/spec/migrations/encrypt_existing_addresses_spec.rb +++ b/spec/migrations/encrypt_existing_addresses_spec.rb @@ -33,4 +33,21 @@ def set_plaintext(address, **columns) ) expect(connection.select_value("SELECT line1 FROM addresses WHERE id = #{address.id}")).to eq("1 Main St") end + + it "leaves a row that the new code wrote alone on a second run" do + address = create(:shipping_address, line1: "2 Elm St") + + migrate + + expect(Address.find(address.id).line1).to eq("2 Elm St") + end + + it "keeps the address fields out of the log" do + filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters) + + expect(filter.filter("user" => { "shipping_address_attributes" => { "line1" => "1 Main St" }, + "billing_address_attributes" => { "postal_code" => "05401" } +})) + .to eq("user" => { "shipping_address_attributes" => "[FILTERED]", "billing_address_attributes" => "[FILTERED]" }) + end end From ddad05becba33f00ad5f4fe30edb6a10f4758814 Mon Sep 17 00:00:00 2001 From: Jasper Mayone Date: Wed, 18 Nov 2026 12:02:22 -0500 Subject: [PATCH 3/3] test: tidy address log filter spec --- spec/migrations/encrypt_existing_addresses_spec.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/spec/migrations/encrypt_existing_addresses_spec.rb b/spec/migrations/encrypt_existing_addresses_spec.rb index 330f01a0..7d80c402 100644 --- a/spec/migrations/encrypt_existing_addresses_spec.rb +++ b/spec/migrations/encrypt_existing_addresses_spec.rb @@ -45,9 +45,12 @@ def set_plaintext(address, **columns) it "keeps the address fields out of the log" do filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters) - expect(filter.filter("user" => { "shipping_address_attributes" => { "line1" => "1 Main St" }, - "billing_address_attributes" => { "postal_code" => "05401" } -})) + params = { + "shipping_address_attributes" => { "line1" => "1 Main St" }, + "billing_address_attributes" => { "postal_code" => "05401" } + } + + expect(filter.filter("user" => params)) .to eq("user" => { "shipping_address_attributes" => "[FILTERED]", "billing_address_attributes" => "[FILTERED]" }) end end