BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0 - Mailing list pgsql-bugs

From PG Bug reporting form
Subject BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0
Date
Msg-id 19714-2c7439b39f73bde9@postgresql.org
Whole thread
Responses Re: BUG #19714: pgcrypto pgp_sym_encrypt accepts nonnumeric s2k-mode as mode 0
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      19714
Logged by:          Qifan Liu
Email address:      imchifan@163.com
PostgreSQL version: 18.6
Operating system:   Linux on amd64
Description:

pgp_sym_encrypt accepts the malformed option s2k-mode=not_a_number and
produces usable ciphertext. The documented s2k-mode values are numeric modes
0, 1, and 3, so nonnumeric text should be rejected rather than silently
selecting mode 0. This can cause encryption to use a different string-to-key
mode than the caller specified. The impact is localized to pgcrypto option
validation.

Steps to reproduce
------------------
CREATE EXTENSION pgcrypto;

SELECT pgp_sym_decrypt(
         pgp_sym_encrypt('payload',
                         'key',
                         's2k-mode=not_a_number'),
         'key') = 'payload' AS malformed_s2k_mode_accepted;

Actual result
-------------
 malformed_s2k_mode_accepted
-----------------------------
 t
(1 row)

The malformed value is accepted, and the produced ciphertext decrypts
successfully.

Expected result
---------------
pgp_sym_encrypt should reject s2k-mode=not_a_number with an error because
s2k-mode accepts only the documented numeric values. It should not interpret
malformed text as mode 0 or produce ciphertext.

Additional information
----------------------
The issue was reproduced on PostgreSQL 20devel, PostgreSQL 18.6, and
PostgreSQL 17.11.
Inference: the behavior is consistent with numeric conversion that maps text
without a valid numeric prefix to zero before validating the resulting mode.





pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #19713: WindowAgg qual pushdown gives wrong partition count when scale(numeric) distinguishes equal values
Next
From: PG Bug reporting form
Date:
Subject: BUG #19715: pg_restore_attribute_stats() rejects range statistics for a domain over int4multirange