The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
docs(vault): consistent formatting
Greg Richardson107f6325664f
diff --git a/apps/docs/pages/guides/database/column-encryption.mdx b/apps/docs/pages/guides/database/column-encryption.mdxindex 82944a72372..1e3c576b682 100644--- a/apps/docs/pages/guides/database/column-encryption.mdx+++ b/apps/docs/pages/guides/database/column-encryption.mdx@@ -62,9 +62,9 @@ This key can now be used with pgsodium encryption functions by its UUID (`eaa20d```sqlselect * from pgsodium.crypto_aead_det_encrypt (- 'this is the message', -- a message to encrypt- 'this is associated data', -- some authenticated associated data- 'eaa20d8c-c77c-4985-9f73-2a5f5d1f1e6d'::uuid -- key ID+ 'this is the message', -- a message to encrypt+ 'this is associated data', -- some authenticated associated data+ 'eaa20d8c-c77c-4985-9f73-2a5f5d1f1e6d'::uuid -- key ID);```@@ -80,9 +80,9 @@ Now to decrypt the ciphertext, pass it to the decryption function _with the same```sqlselect *from convert_from(pgsodium.crypto_aead_det_decrypt (- '\\x099baa820250d7375ed141f8f1936af384bc229f3de1010a6eff6ffdaf3998baffbae75b5cd83d1c469407ff2d3764a428b742',- 'this is associated data',- 'eaa20d8c-c77c-4985-9f73-2a5f5d1f1e6d'::uuid+ '\\x099baa820250d7375ed141f8f1936af384bc229f3de1010a6eff6ffdaf3998baffbae75b5cd83d1c469407ff2d3764a428b742',+ 'this is associated data',+ 'eaa20d8c-c77c-4985-9f73-2a5f5d1f1e6d'::uuid), 'utf8');```@@ -121,14 +121,14 @@ Let's see a simple example of using `SECURITY LABEL` to encrypt a column using *For the simplest case, a column can be encrypted with one Key ID which must be of the type `aead-det` (as created above):```sql-CREATE TABLE secrets (- id bigserial primary key,- secret_number text+create table secrets (+ id bigserial primary key,+ secret_number text);-SECURITY LABEL FOR pgsodium- ON COLUMN secrets.secret_number- IS 'ENCRYPT WITH KEY ID e348034b-3f07-4878-aad6-000511d12826';+security label for pgsodium+ on column secrets.secret_number+ is 'ENCRYPT WITH KEY ID e348034b-3f07-4878-aad6-000511d12826';```The advantage of this approach is simplicity - the user creates one key and labels a column with it. The cryptographic algorithm for this approach uses a _nonceless_ encryption algorithm called `crypto_aead_det_xchacha20()`. This algorithm is written by the author of libsodium and can be found [here](https://github.com/jedisct1/libsodium-xchacha20-siv).@@ -140,15 +140,15 @@ Using one key for an entire column means that whoever can decrypt one row can deA more fine grained approach would be storing one Key ID per row:```sql-CREATE TABLE secrets (- id bigserial primary key,- secret text,- key_id uuid not null DEFAULT 'e348034b-3f07-4878-aad6-000511d12826'::uuid+create table secrets (+ id bigserial primary key,+ secret text,+ key_id uuid not null default 'e348034b-3f07-4878-aad6-000511d12826'::uuid);-SECURITY LABEL FOR pgsodium- ON COLUMN secrets.secret- IS 'ENCRYPT WITH KEY COLUMN key_id';+security label for pgsodium+ on column secrets.secret+ is 'ENCRYPT WITH KEY COLUMN key_id';```This approach ensures that a key for one user doesn't necessarily decrypt any others. While rows can share key IDs, they don't necessarily have to (unlike the first example above where one key id was used for the entire column). It also acts as a natural partition that can work in conjunction with Row Level Security to share distinct keys between owners.@@ -164,16 +164,16 @@ Nonces are some extra cryptographic context that is used in many cryptographic aDuplicate ciphertexts cannot be used to “attack the key”, it can only reveal the duplication. However, duplication is still information. In our examples so far, an attacker might be able to use this information to determine that two accounts share the same secret. While not technically breaking the encryption, this still leaks information to an attacker.```sql-CREATE TABLE secrets (- id bigserial primary key,- secret text,- key_id uuid not null DEFAULT 'e348034b-3f07-4878-aad6-000511d12826'::uuid,- nonce bytea default pgsodium.crypto_aead_det_noncegen()+create table secrets (+ id bigserial primary key,+ secret text,+ key_id uuid not null default 'e348034b-3f07-4878-aad6-000511d12826'::uuid,+ nonce bytea default pgsodium.crypto_aead_det_noncegen());-SECURITY LABEL FOR pgsodium- ON COLUMN secrets.secret- IS 'ENCRYPT WITH KEY COLUMN key_id NONCE nonce';+security label for pgsodium+ on column secrets.secret+ is 'ENCRYPT WITH KEY COLUMN key_id NONCE nonce';```This is the most secure form of TCE - there is a unique key ID and a unique nonce per row.@@ -189,17 +189,17 @@ In our secret example, we might associate a "`secret`" with an "`account_id`". BLike above, this is done simply by extending the security label with the associated data column:```sql-CREATE TABLE secrets (- id bigserial primary key,- secret text,- account_id integer,- key_id uuid not null DEFAULT 'e348034b-3f07-4878-aad6-000511d12826'::uuid,- nonce bytea default pgsodium.crypto_aead_det_noncegen()+create table secrets (+ id bigserial primary key,+ secret text,+ account_id integer,+ key_id uuid not null default 'e348034b-3f07-4878-aad6-000511d12826'::uuid,+ nonce bytea default pgsodium.crypto_aead_det_noncegen());-SECURITY LABEL FOR pgsodium- ON COLUMN secrets.secret- IS 'ENCRYPT WITH KEY COLUMN key_id ASSOCIATED (account_id) NONCE nonce';+security label for pgsodium+ on column secrets.secret+ is 'ENCRYPT WITH KEY COLUMN key_id ASSOCIATED (account_id) NONCE nonce';```The new label indicates which column is to be associated with the secret, and that's it! Your `account_id` and secret are now protected under the same authentication signature as the secret itself.@@ -209,12 +209,10 @@ The new label indicates which column is to be associated with the secret, and thNow that you have TCE setup for a table, it's easy to use by simply inserting data into the table, and querying that data by looking at its generated view. The view is named `decrypted_<table_name>` and by default is in the same schema as your table:```sql-INSERT INTO secrets (- secret,- account_id-)-VALUES- ('1234-5678-8765-4321', 123);+insert into secrets+ (secret, account_id)+values+ ('1234-5678-8765-4321', 123);```Now that you have inserted data, look at the table and notice how the secret is encrypted. This is the data that is stored on disk, the encrypted card number, the key id, and the account id, **but the key itself is not stored**. This means if someone gets a backup or dump of your database, they cannot decrypt the secret, they do not have the key, only the key ID:diff --git a/apps/docs/pages/guides/database/vault.mdx b/apps/docs/pages/guides/database/vault.mdxindex f13aca64430..683d383219b 100644--- a/apps/docs/pages/guides/database/vault.mdx+++ b/apps/docs/pages/guides/database/vault.mdx@@ -18,7 +18,7 @@ From a product perspective, Supabase groups a number of related features under tUnder the hood, the Vault is a table of Secrets and Encryption Keys that are stored using [Authenticated Encryption](https://en.wikipedia.org/wiki/Authenticated_encryption) on disk. They are then available in decrypted form through a Postgres view so that the secrets can be used by applications from SQL. Because the secrets are stored on disk encrypted and authenticated, any backups or replication streams also preserve this encryption in a way that can't be decrypted or forged.-Supabase provides a dashboard UI for the Vault that makes storing secrets easy. Click a button, type in your secret, and save. Optionally create your own keys you can use to encrypt your secret. Your secret will then be stored on disk encrypted using the specified key.+Supabase provides a dashboard UI for the Vault that makes storing secrets easy. Click a button, type in your secret, and save. Optionally create your own keys you can use to encrypt your secret. Your secret will then be stored on disk encrypted using the specified key.<video width="99%" muted playsInline controls="true"><source@@ -49,14 +49,16 @@ There are two main parts to the Vault UI, Secrets and Encryption Keys:As we mentioned, the Vault uses pgsodium's Transparent Column Encryption (TCE) to store secrets in an authenticated encrypted form. There are some details around that you may be curious about, what does authenticated mean, and where are encryption keys store? This section explains those details.### Authenticated Encryption with Associated Data+The first important feature of TCE is that it uses an [Authenticated Encryption with Associated Data](<https://en.wikipedia.org/wiki/Authenticated_encryption#Authenticated_encryption_with_associated_data_(AEAD)>) encryption algorithm (based on libsodium).### Encryption key location-**Authenticated Encryption** means that in addition to the data being encrypted, it is also signed so that it cannot be forged. You can guarantee that the data was encrypted by someone you trust, which you wouldn't get with encryption alone. The decryption function verifies that the signature is valid *before decrypting the value*.++**Authenticated Encryption** means that in addition to the data being encrypted, it is also signed so that it cannot be forged. You can guarantee that the data was encrypted by someone you trust, which you wouldn't get with encryption alone. The decryption function verifies that the signature is valid _before decrypting the value_.**Associated Data** means that you can include any other columns from the same row as part of the signature computation. This doesn't encrypt those other columns - rather it ensures that your encrypted value is only associated with columns from that row. If an attacker were to copy an encrypted value from another row to the current one, the signature would be rejected (assuming you used a unique column in the associated data).-Another important feature of pgsodium is that the encryption keys are never stored in the database alongside the encrypted data. Instead, only a **Key ID** is stored, which is a reference to the key that is only accessible outside of SQL. Even if an attacker can capture a dump of your entire database, they will see only encrypted data and key IDs, *never the raw key itself*.+Another important feature of pgsodium is that the encryption keys are never stored in the database alongside the encrypted data. Instead, only a **Key ID** is stored, which is a reference to the key that is only accessible outside of SQL. Even if an attacker can capture a dump of your entire database, they will see only encrypted data and key IDs, _never the raw key itself_.This is an important safety precaution - there is little value in storing the encryption key in the database itself as this would be like locking your front door but leaving the key in the lock! Storing the key outside the database fixes this issue.@@ -151,7 +153,7 @@ created_at | 2022-12-14 02:29:21.3625+00updated_at | 2022-12-14 02:29:21.3625+00```-Notice how this view has a `decrypted_secret` column that contains the decrypted secrets. Views are not stored on disk, they are only run at query time, so the secret remains encrypted on disk, and in any backup dumps or replication streams.+Notice how this view has a `decrypted_secret` column that contains the decrypted secrets. Views are not stored on disk, they are only run at query time, so the secret remains encrypted on disk, and in any backup dumps or replication streams.You should ensure that you protect access to this view with the appropriate SQL privilege settings at all times, as anyone that has access to the view has access to decrypted secrets.@@ -180,9 +182,9 @@ updated_at | 2022-12-14 02:51:13.938396+00## Internal Details-To encrypt data, you need a *key id*. You can use the default key id created automatically for every project, or create your own key ids Using the `pgsodium.create_key()` function. Key ids are used to internally derive the encryption key used to encrypt secrets in the vault. Vault users typically do not have access to the key itself, only the key id.+To encrypt data, you need a _key id_. You can use the default key id created automatically for every project, or create your own key ids Using the `pgsodium.create_key()` function. Key ids are used to internally derive the encryption key used to encrypt secrets in the vault. Vault users typically do not have access to the key itself, only the key id.-Both `vault.create_secret()` and `vault.update_secret()` take an optional fourth `new_key_id` argument. This argument can be used to store a different key id for the secret instead of the default value.+Both `vault.create_secret()` and `vault.update_secret()` take an optional fourth `new_key_id` argument. This argument can be used to store a different key id for the secret instead of the default value.```sqlpostgres=> select vault.create_secret('another_s3kre3t_key', 'another_unique_name',@@ -191,16 +193,16 @@ postgres=> select vault.create_secret('another_s3kre3t_key', 'another_unique_namcreate_secret | cec9e005-a44d-4b19-86e1-febf3cd40619```-Which roles should have access to the `vault.secrets` table should be carefully considered. There are two ways to grant access, the first is that the `postgres` user can explicitly grant access to the vault table itself.+Which roles should have access to the `vault.secrets` table should be carefully considered. There are two ways to grant access, the first is that the `postgres` user can explicitly grant access to the vault table itself.## Turning off Statement Logging-When you insert secrets into the vault table with an INSERT statement, those statements get logged by default into the Supabase logs. Since this would mean your secrets are stored unencrypted in the logs, you should turn off statement logging while using the Vault.+When you insert secrets into the vault table with an INSERT statement, those statements get logged by default into the Supabase logs. Since this would mean your secrets are stored unencrypted in the logs, you should turn off statement logging while using the Vault.-While turning off statement logging does hinder you if you're used to looking at the logs to debug your application, it provides a much higher level of security by ensuring that your data does not leak out of the database and into the logs. This is especially critical with encrypted column data, because the statement logs will contain the *unencrypted* secrets. If you *must* store that data encrypted, then you *must* turn off statement logging.+While turning off statement logging does hinder you if you're used to looking at the logs to debug your application, it provides a much higher level of security by ensuring that your data does not leak out of the database and into the logs. This is especially critical with encrypted column data, because the statement logs will contain the _unencrypted_ secrets. If you _must_ store that data encrypted, then you _must_ turn off statement logging.```sql-ALTER SYSTEM SET statement_log = 'none';+alter system set statement_log = 'none';```And then restart your project from the dashboard to enable that change.