Giter VIP home page Giter VIP logo

Comments (2)

ioguix avatar ioguix commented on June 15, 2024 1

I really wonder why in the first place decrypting an empty string is failing. Any idea @michelp?

from pgsodium.

henningko avatar henningko commented on June 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.