'"Illegal argument to function" in Postgres when doing select on a view with pgp_sym_decrypt() expression

I am using pg_sym_decrypt/encrypt to decrypt/encrypt some of the fields in my table. Decryption happens on select in a separate EntityComputed view that has a one-to-one relationship with the Entity that stores the encrypted fields.

I get this error when doing a select over my data:

"driverError":{
  "length":92,
  "name":"error",
  "severity":"ERROR",
  "code":"39000",
  "file":"pgp-pgsql.c",
  "line":"610",
  "routine":"decrypt_internal"
},

Using TypeORM. "Human-readable" of exception says "Illegal argument to function". Query looks like this:

SELECT 
  "Feedback"."id" AS "Feedback_id", 
  "Feedback"."created_at" AS "Feedback_created_at", 
  "Feedback"."updated_at" AS "Feedback_updated_at", 
  "Feedback"."lang_pair" AS "Feedback_lang_pair", 
  "Feedback"."target" AS "Feedback_target", 
  "Feedback"."source" AS "Feedback_source", 
  "Feedback"."translation_rating" AS "Feedback_translation_rating", 
  "Feedback"."external_id" AS "Feedback_external_id", 
  "Feedback"."client_id" AS "Feedback_client_id", 
  "Feedback"."updated_translation_rating" AS "Feedback_updated_translation_rating", 
  "Feedback"."status" AS "Feedback_status", 
  "Feedback"."feedback_source" AS "Feedback_feedback_source", 
  "Feedback"."third_party_id" AS "Feedback_third_party_id", 
  "Feedback"."license_id" AS "Feedback_license_id", 
  "Feedback_computed"."comment" AS "Feedback_computed_comment", 
  "Feedback_computed"."source_text" AS "Feedback_computed_source_text", 
  "Feedback_computed"."target_text" AS "Feedback_computed_target_text", 
  "Feedback_computed"."suggested_translation" AS "Feedback_computed_suggested_translation", 
  "Feedback_computed"."latest_translation" AS "Feedback_computed_latest_translation", 
  "Feedback_computed"."id" AS "Feedback_computed_id" 
FROM 
  "feedback" "Feedback" 
  LEFT JOIN "feedback_computed" "Feedback_computed" ON "Feedback_computed"."id" = "Feedback"."id"

Feedback_computed is the view that calls pgp_sym_decrypt().

What might cause this issue? Or can you please help me debug it? How do I get more information about what's wrong?

Edit: FeedbackComputed view definition:

create view feedback_computed (id, comment, source_text, target_text, suggested_translation, latest_translation) as
SELECT feedback.id,
       pgp_sym_decrypt(feedback.comment_encrypted, '<encryption_key>'::text,
                       '<encryption_options>'::text)                                                                    AS comment,
       pgp_sym_decrypt(feedback.source_text_encrypted, '<encryption_key>'::text,
                       '<encryption_options>'::text)                                                                    AS source_text,
       pgp_sym_decrypt(feedback.target_text_encrypted, '<encryption_key>'::text,
                       '<encryption_options>'::text)                                                                    AS target_text,
       pgp_sym_decrypt(feedback.suggested_translation_encrypted, '<encryption_key>'::text,
                       'cipher-algo=aes256'::text)                                                                    AS suggested_translation,
       pgp_sym_decrypt(feedback.latest_translation_encrypted, '<encryption_key>'::text,
                       '<encryption_o>'::text)                                                                    AS latest_translation
FROM feedback feedback;

alter table feedback_computed
    owner to feedback;



Solution 1:[1]

So my issue was that it turns out that stage environment migrations that added the view weren't provided with encryption password (second argument to pgp_sym_decrypt), which meant that view got compiled with null value as password, which is why it gives "Illegal argumen to function".

Would've been much easier to fix if pgcrypto didn't give such cryptic error messages.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 comonadd