Skip to content

Commit 2e36b80

Browse files
committed
Encrypt the email_address field on TeacherInvitation
As a general rule we'd try to avoid having personally identifiable information in the editor-api database. At the moment however, the feature to generate email invitations has to live in this codebase. To improve the security of the email_address field, this commit ensures that is encrypted against the keys introduced in the previous commit. I thought I might have to use the `deterministic: true` option[1] here to allow the `find_by_token_for` method to work, but I've convinced myself using the existing tests and by experimenting in the console that this isn't the case. The generated token can be used to retrieve the TeacherInvitation record even when `email_address` is encrypted. I've added a test to demonstrate that this non-deterministic encryption (that is, where a different value is generated each time encrypt is called even for the same input) is intended. The rails guide suggests that this is the best practice unless we need to query by email address (which we currently don't need to do). [1] https://guides.rubyonrails.org/active_record_encryption.html#deterministic-and-non-deterministic-encryption
1 parent 6148ed7 commit 2e36b80

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

app/models/teacher_invitation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class TeacherInvitation < ApplicationRecord
88
format: { with: EmailValidator.regexp, message: I18n.t('validations.invitation.email_address') }
99
validate :school_is_verified
1010
after_create_commit :send_invitation_email
11+
encrypts :email_address
1112

1213
generates_token_for :teacher_invitation, expires_in: 30.days do
1314
email_address

spec/models/teacher_invitation_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,11 @@
6464

6565
expect(invitation.school_name).to eq('school-name')
6666
end
67+
68+
it 'non-deterministically encrypts the email_address' do
69+
school = create(:verified_school)
70+
described_class.create!(email_address: '[email protected]', school:)
71+
72+
expect(described_class.find_by(email_address: '[email protected]')).to be_nil
73+
end
6774
end

0 commit comments

Comments
 (0)