Comments (2)
I really wonder why in the first place decrypting an empty string is failing. Any idea @michelp?
from pgsodium.
I have the same issue when dealing with decrypting empty strings. I have modified the trigger to treat empty strings:
CREATE OR REPLACE FUNCTION public.emails_encrypt_secret_summary()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
BEGIN
-- Encrypt the token if it's not null and key_id is not null
NEW.summary := CASE
WHEN NEW.summary IS NULL THEN
NULL
-- When empty string, save empty string
WHEN NEW.summary = '' THEN
''
ELSE
CASE
WHEN NEW.key_id IS NULL THEN
NULL
ELSE
pg_catalog.encode(
pgsodium.crypto_aead_det_encrypt(
pg_catalog.convert_to(NEW.summary, 'utf8'),
pg_catalog.convert_to(('')::text, 'utf8'),
NEW.key_id::uuid,
NULL
),
'base64'
)
END
END;
RETURN NEW;
END;
$function$;
Note that you'd also need to handle the decrypt view:
DROP VIEW IF EXISTS public.decrypted_emails;
CREATE OR REPLACE VIEW public.decrypted_emails AS
SELECT emails.id,
emails.user_id,
emails.body,
emails.summary,
CASE
WHEN emails.summary IS NULL THEN NULL::text
WHEN emails.summary = '' THEN ''
ELSE
CASE
WHEN emails.key_id IS NULL THEN NULL::text
ELSE convert_from(pgsodium.crypto_aead_det_decrypt(decode(emails.summary, 'base64'::text), convert_to(''::text, 'utf8'::name), emails.key_id, NULL::bytea), 'utf8'::name)
END
END AS decrypted_summary,
Or you could just treat empty as null:
CREATE OR REPLACE FUNCTION public.emails_encrypt_secret_summary()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
BEGIN
-- Check if key_id is not already set, else set it to user's key_id
IF NEW.key_id IS NULL THEN
NEW.key_id := (
SELECT key_id
FROM user_preferences
WHERE user_preferences.user_id = NEW.user_id
);
END IF;
-- Encrypt the token if it's not null and key_id is not null
NEW.summary := CASE
WHEN NEW.summary IS NULL OR NEW.summary = '' THEN
NULL
ELSE
CASE
WHEN NEW.key_id IS NULL THEN
NULL
ELSE
pg_catalog.encode(
pgsodium.crypto_aead_det_encrypt(
pg_catalog.convert_to(NEW.summary, 'utf8'),
pg_catalog.convert_to(('')::text, 'utf8'),
NEW.key_id::uuid,
NULL
),
'base64'
)
END
END;
RETURN NEW;
END;
$function$;
from pgsodium.
Related Issues (20)
- Avoid secret logging HOT 1
- Wrong memory space allocated HOT 5
- Better NULL input checking? HOT 3
- TCE: triggers recreated twice per column HOT 1
- Update secret when updating associated data
- TCE : Update with using Old Value HOT 1
- installation rpm pgsodium HOT 1
- Add error message when the security label isn't syntactically correct
- Error creating security label using Postgres 14/15 and pgsodium 3.1.5 HOT 2
- setting security label in two different tables fails HOT 5
- Incoherency between fields `pgsodium.key.status` and `pgsodium.key.expires`
- Grant `REFERENCES` on `pgsodium.key` to pgsodium_keyiduser?
- Updated value in secret column does not get encrypted HOT 5
- Running into `fatal error: 'sodium.h' file not found` error during installation HOT 4
- Failing pgtap Tests
- How to manage pgsodium_root.key in primary-secondary cluster
- jsonb type support HOT 1
- Help section needed - Backup/Restore of database
- ERROR: pgsodium_derive_helper: pgsodium_derive: no server secret key defined HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pgsodium.