From 76cd04ef642e41d6f1b2277778c129fccde8744d Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <dgustafsson@postgresql.org>
Date: Thu, 12 Nov 2026 22:47:26 +0100
Subject: [PATCH v2] Reject invalid base64 encoded values with data after
 padding

When decoding Base64 encoded values the code accepted input after
the padding bytes, which should have resulted in an error since
the input isn't valid Base64.  To avoid problems with data at
rest in a minor version, this fix is not backpatched.  The fixes
for base64url are however backpatched to 19 since base64url is a
new feature in 19 and no existing data exist.

The bug was independently reported by Qifan Liu and Atish Prakash.

Author: Shihao Zhong <zhong950419@gmail.com>
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Reported-by: Qifan Liu <imchifan@163.com>
Reported-by: Atish Prakash <125atishprak@gmail.com>
Discussion: https://postgr.es/m/19702-9ed4a131fcfadb9d@postgresql.org
Discussion: https://postgr.es/m/19843-ebef332578fa09da@postgresql.org
Backpatch-through: 19
---
 contrib/pgcrypto/expected/pgp-armor.out | 10 ++++++++++
 contrib/pgcrypto/pgp-armor.c            |  8 ++++++--
 contrib/pgcrypto/sql/pgp-armor.sql      | 10 ++++++++++
 src/backend/utils/adt/encode.c          | 11 ++++++-----
 src/common/base64.c                     |  7 ++++---
 src/test/regress/expected/strings.out   | 24 ++++++++++++++++++++++++
 src/test/regress/sql/strings.sql        | 10 ++++++++++
 7 files changed, 70 insertions(+), 10 deletions(-)

diff --git a/contrib/pgcrypto/expected/pgp-armor.out b/contrib/pgcrypto/expected/pgp-armor.out
index 0f5ff461805..b3662a642f0 100644
--- a/contrib/pgcrypto/expected/pgp-armor.out
+++ b/contrib/pgcrypto/expected/pgp-armor.out
@@ -100,6 +100,16 @@ em9va2E=
 -----END PGP MESSAGE-----
 ');
 ERROR:  Corrupt ascii-armor
+-- corrupt (data after padding)
+select dearmor('
+-----BEGIN PGP MESSAGE-----
+
+YQ==
+AAAA
+=Pr0b
+-----END PGP MESSAGE-----
+');
+ERROR:  Corrupt ascii-armor
 -- corrupt (no space after the colon)
 select * from pgp_armor_headers('
 -----BEGIN PGP MESSAGE-----
diff --git a/contrib/pgcrypto/pgp-armor.c b/contrib/pgcrypto/pgp-armor.c
index bfc90af063d..ddd40d72a6e 100644
--- a/contrib/pgcrypto/pgp-armor.c
+++ b/contrib/pgcrypto/pgp-armor.c
@@ -119,9 +119,9 @@ pg_base64_decode(const uint8 *src, unsigned len, uint8 *dst)
 		else if (c == '=')
 		{
 			/*
-			 * end sequence
+			 * end sequence, after it only the second "=" of "==" is allowed
 			 */
-			if (!end)
+			if (!end || pos != 3)
 			{
 				if (pos == 2)
 					end = 1;
@@ -137,6 +137,10 @@ pg_base64_decode(const uint8 *src, unsigned len, uint8 *dst)
 		else
 			return PXE_PGP_CORRUPT_ARMOR;
 
+		/* no data is allowed after padding */
+		if (end && c != '=')
+			return PXE_PGP_CORRUPT_ARMOR;
+
 		/*
 		 * add it to buffer
 		 */
diff --git a/contrib/pgcrypto/sql/pgp-armor.sql b/contrib/pgcrypto/sql/pgp-armor.sql
index 736b54206f0..d4a4743b4c8 100644
--- a/contrib/pgcrypto/sql/pgp-armor.sql
+++ b/contrib/pgcrypto/sql/pgp-armor.sql
@@ -55,6 +55,16 @@ em9va2E=
 -----END PGP MESSAGE-----
 ');
 
+-- corrupt (data after padding)
+select dearmor('
+-----BEGIN PGP MESSAGE-----
+
+YQ==
+AAAA
+=Pr0b
+-----END PGP MESSAGE-----
+');
+
 -- corrupt (no space after the colon)
 select * from pgp_armor_headers('
 -----BEGIN PGP MESSAGE-----
diff --git a/src/backend/utils/adt/encode.c b/src/backend/utils/adt/encode.c
index b9d4f2811d7..7aa56466718 100644
--- a/src/backend/utils/adt/encode.c
+++ b/src/backend/utils/adt/encode.c
@@ -536,8 +536,8 @@ pg_base64_decode_internal(const char *src, size_t len, char *dst, bool url)
 
 		if (c == '=')
 		{
-			/* end sequence */
-			if (!end)
+			/* end sequence, after it only the second "=" of "==" is allowed */
+			if (!end || pos != 3)
 			{
 				if (pos == 2)
 					end = 1;
@@ -556,7 +556,8 @@ pg_base64_decode_internal(const char *src, size_t len, char *dst, bool url)
 		else
 		{
 			b = -1;
-			if (c > 0 && c < 127)
+			/* no data is allowed after padding */
+			if (c > 0 && c < 127 && !end)
 				b = b64lookup[(unsigned char) c];
 			if (b < 0)
 			{
@@ -583,12 +584,12 @@ pg_base64_decode_internal(const char *src, size_t len, char *dst, bool url)
 		}
 	}
 
-	if (url && pos == 2)
+	if (url && !end && pos == 2)
 	{
 		buf <<= 12;
 		*p++ = (buf >> 16) & 0xFF;
 	}
-	else if (url && pos == 3)
+	else if (url && !end && pos == 3)
 	{
 		buf <<= 6;
 		*p++ = (buf >> 16) & 0xFF;
diff --git a/src/common/base64.c b/src/common/base64.c
index aaaefc2921a..9cd92cacb59 100644
--- a/src/common/base64.c
+++ b/src/common/base64.c
@@ -134,8 +134,8 @@ pg_b64_decode(const char *src, int len, uint8 *dst, int dstlen)
 
 		if (c == '=')
 		{
-			/* end sequence */
-			if (!end)
+			/* end sequence, after it only the second "=" of "==" is allowed */
+			if (!end || pos != 3)
 			{
 				if (pos == 2)
 					end = 1;
@@ -155,7 +155,8 @@ pg_b64_decode(const char *src, int len, uint8 *dst, int dstlen)
 		else
 		{
 			b = -1;
-			if (c > 0 && c < 127)
+			/* no data is allowed after padding */
+			if (c > 0 && c < 127 && !end)
 				b = b64lookup[(unsigned char) c];
 			if (b < 0)
 			{
diff --git a/src/test/regress/expected/strings.out b/src/test/regress/expected/strings.out
index fa29abfd829..68b5767718d 100644
--- a/src/test/regress/expected/strings.out
+++ b/src/test/regress/expected/strings.out
@@ -2905,6 +2905,30 @@ SELECT decode(encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea,
  \x1234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef00011234567890abcdef0001
 (1 row)
 
+-- nothing but whitespace may follow base64 padding
+SELECT decode(E'YQ=\n= \n', 'base64');  -- OK
+ decode 
+--------
+ \x61
+(1 row)
+
+SELECT decode('YQ==Yg==', 'base64');  -- error
+ERROR:  invalid symbol "Y" found while decoding base64 sequence
+SELECT decode('YQ==AAAA', 'base64');  -- error
+ERROR:  invalid symbol "A" found while decoding base64 sequence
+SELECT decode('YQ=a', 'base64');  -- error
+ERROR:  invalid symbol "a" found while decoding base64 sequence
+SELECT decode('YQ======', 'base64');  -- error
+ERROR:  unexpected "=" while decoding base64 sequence
+SELECT decode('YQ=', 'base64url');  -- error
+ERROR:  invalid base64url end sequence
+HINT:  Input data is missing padding, is truncated, or is otherwise corrupted.
+SELECT decode('YQ===', 'base64url');  -- error
+ERROR:  unexpected "=" while decoding base64url sequence
+SELECT decode('YQ=A', 'base64url');  -- error
+ERROR:  invalid symbol "A" found while decoding base64url sequence
+SELECT decode('YQ==YQ==', 'base64url');  -- error
+ERROR:  invalid symbol "Y" found while decoding base64url sequence
 SELECT encode('\x1234567890abcdef00', 'escape');
            encode            
 -----------------------------
diff --git a/src/test/regress/sql/strings.sql b/src/test/regress/sql/strings.sql
index 7d9c7275a02..cea86e63941 100644
--- a/src/test/regress/sql/strings.sql
+++ b/src/test/regress/sql/strings.sql
@@ -986,6 +986,16 @@ SELECT decode('1234567890abcdef00', 'hex');
 SELECT encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea, 'base64');
 SELECT decode(encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea,
                      'base64'), 'base64');
+-- nothing but whitespace may follow base64 padding
+SELECT decode(E'YQ=\n= \n', 'base64');  -- OK
+SELECT decode('YQ==Yg==', 'base64');  -- error
+SELECT decode('YQ==AAAA', 'base64');  -- error
+SELECT decode('YQ=a', 'base64');  -- error
+SELECT decode('YQ======', 'base64');  -- error
+SELECT decode('YQ=', 'base64url');  -- error
+SELECT decode('YQ===', 'base64url');  -- error
+SELECT decode('YQ=A', 'base64url');  -- error
+SELECT decode('YQ==YQ==', 'base64url');  -- error
 SELECT encode('\x1234567890abcdef00', 'escape');
 SELECT decode(encode('\x1234567890abcdef00', 'escape'), 'escape');
 
-- 
2.39.3 (Apple Git-146)

=