Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparent Column Encryption does not work (permission/ownership issue) #12952

Closed
jstdk opened this issue Mar 11, 2023 · 12 comments
Closed

Transparent Column Encryption does not work (permission/ownership issue) #12952

jstdk opened this issue Mar 11, 2023 · 12 comments
Labels
bug Something isn't working database

Comments

@jstdk
Copy link

jstdk commented Mar 11, 2023

Bug report

Describe the bug

I followed the Supabase tutorial: https://supabase.com/blog/transparent-column-encryption-with-postgres to encrypt the password field in the email table. I tried this:

CREATE TABLE emails (
id bigserial primary key,
email text,
password text,
key_id uuid not null DEFAULT 'e348034b-3f07-4878-aad6-000511d12826'::uuid,
nonce bytea default pgsodium.crypto_aead_det_noncegen()
);

This succeeds, then I move on to:

SECURITY LABEL FOR pgsodium
ON COLUMN emails.password
IS 'ENCRYPT WITH KEY COLUMN key_id NONCE nonce';

Error: Failed to run sql query: must be owner of event trigger pgsodium_trg_mask_update

Then I tried to Google and change the owner as suggested by other users and tutorials. For example https://www.postgresql.org/docs/current/sql-altereventtrigger.html

Problem is that I cannot find this trigger in Triggers. I tried:

ALTER EVENT TRIGGER pgsodium_trg_mask_update OWNER to postgres

Error: Failed to run sql query: must be owner of event trigger pgsodium_trg_mask_update

I am out of options, How did the writer of the tutorial make this work?

To Reproduce

Follow https://supabase.com/blog/transparent-column-encryption-with-postgres

Expected behavior

Password column encrypted

Screenshots

See code examples above

System information

Linux (Fedora 37) + Supabase Dashboard in Chromium

@jstdk jstdk added the bug Something isn't working label Mar 11, 2023
@james2doyle
Copy link

Having the same issue. Also tried to use an RPC call and that also failed

@james2doyle
Copy link

You need to grant the user the permissions to use the function:

GRANT EXECUTE ON FUNCTION pgsodium.crypto_aead_det_decrypt (bytea, bytea, uuid, bytea) TO authenticated;

@jstdk
Copy link
Author

jstdk commented Mar 15, 2023

Thanks. Unfortunately, I keep getting this error: Failed to run sql query: must be owner of event trigger pgsodium_trg_mask_update

@james2doyle
Copy link

james2doyle commented Mar 15, 2023 via email

@jstdk
Copy link
Author

jstdk commented Mar 15, 2023

I ran it in the SQL editor indeed. Will try as client. Thanks for the extra info

@radulescuandrew
Copy link

I have the same issue while trying to alter a table on a recently restored project.

must be owner of event trigger pgsodium_trg_mask_update

I tried the above but it is not working. Any other guidance?

@gregnr
Copy link
Member

gregnr commented Jun 28, 2023

cc @michelp in case you have any insight.

@michelp
Copy link
Contributor

michelp commented Jun 28, 2023

Hi @radulescuandrew , the issue is that in an older version of our software stack, there was a dump/restore bug that introduced this ownership issue. If you start through the same tutorial with a fresh project, it will work properly.

Fixing it for an existing database depends on if you have encrypted data you want to preserve. If you do, you'll have to temporarily copy or dump that data to the side so you can reinser it. then disabling and then renabling the extensions will initialize them with the correct privledges:

drop view vault.decrypted_secrets;
drop extension supabase_vault;
drop extension pgsodium;
create extension pgsodium;
create extension supabase_vault;

@jstdk
Copy link
Author

jstdk commented Jun 30, 2023

That explains why it did not even work with the new toggle in the edit column page in my existing project

@SamMakesThings
Copy link

SamMakesThings commented Jul 25, 2023

Note to future people: Double check the blocked function name in your logs. It turned out that I had to allow access to crypto_aead_det_encrypt, not just crypto_aead_det_decrypt.

Full command:
GRANT EXECUTE ON FUNCTION pgsodium.crypto_aead_det_encrypt (bytea, bytea, uuid, bytea) TO authenticated;

@jacktho
Copy link

jacktho commented Aug 18, 2023

I was able to fix the lack of permissions for crypto_aead_det_encrypt and crypto_aead_det_decrypt with GRANT pgsodium_keyiduser TO authenticated;

As mentioned here:
https://supabase.com/docs/guides/database/column-encryption#granting-api-access-to-encrypted-columns

@jstdk
Copy link
Author

jstdk commented Aug 23, 2023

Finally implemented it today for all databases and nuked out various crypto npm packages. What a relieve. It works great without any SQL code needed (only the one-line by @SamMakesThings ), except for querying a view instead of the tables directly.

Thanks everyone, this solution is what I was hoping for. Will close the ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working database
Projects
None yet
Development

No branches or pull requests

7 participants