dwyl / auth

🚪 🔐 UX-focussed Turnkey Authentication Solution for Web Apps/APIs (Documented, Tested & Maintained)
https://authdemo.fly.dev
GNU General Public License v2.0
130 stars 9 forks source link

Chore: Update the `people.email` to use `Fields.EmailEncrypted` #285

Closed nelsonic closed 1 year ago

nelsonic commented 1 year ago

As noted in https://github.com/dwyl/auth/issues/284 sadly, by default the mix phx.gen.auth generator does not setup any protection for personal data in the database. 😢 Email addresses are stored as plaintext:

mix-phx-gen-auth-people-table-email-plaintext

Similarly the people_tokens table stores email addresses as plaintext in the sent_to column:

people_token-email-plaintext

This is obviously undesirable. 🙃 This is a privacy/security issue waiting to become a scandal!

Todo

This shouldn't take very long but allocating T2h to allow for documenting the steps

nelsonic commented 1 year ago

Getting the following error:

15:06:13.691 [info] alter table people
** (Postgrex.Error) ERROR 42804 (datatype_mismatch) column "email" cannot be cast automatically to type bytea

    hint: You might need to specify "USING email::bytea".
nelsonic commented 1 year ago

Sadly, having the person.email and person_tokens.sent_to in plaintext is relied upon by the following query:

https://github.com/dwyl/auth/blob/da0af7ee702c278714b47f92045edce4adad542a/lib/auth/accounts/person_token.ex#L110-L129

I don't see us using this function in our auth implementation because this is the flow/journey where people click on a link in an email to verify their email address and we are definitely not doing email verification with links.

see: https://github.com/dwyl/auth/issues/223#issuecomment-1426810316

So I'm going to explore removing this function and seeing what the ramifications are ... 💭

nelsonic commented 1 year ago

Following the changes made in: https://github.com/dwyl/auth/pull/231/commits/2bbba99c7057f0d2943dd8327be52ccf1a1bd58c

If we now run a query to view the data in the people table:

SELECT id, email, inserted_at FROM people;

We see that the email is now a binary blob

image

Registration and Login still works as expected:

image

But now the personal data captured in registration is stored encrypted at rest the way it should be. 🔐

nelsonic commented 1 year ago

captured in: https://dwyl.github.io/book/auth/09-encrypt-data.html