Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 39 additions & 26 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
50 changes: 25 additions & 25 deletions app/models/user_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
50 changes: 25 additions & 25 deletions app/serializers/address_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
5 changes: 4 additions & 1 deletion config/initializers/filter_parameter_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
22 changes: 22 additions & 0 deletions db/migrate/20261118120000_add_ciphertext_columns_to_addresses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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]
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
32 changes: 32 additions & 0 deletions db/migrate/20261118120100_encrypt_existing_addresses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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
# 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
# The plain-text columns still hold the values, so there is nothing to undo.
end

end
16 changes: 15 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 25 additions & 25 deletions spec/factories/addresses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
Loading
Loading