BUG #19702: decode() accepts Base64 payload after terminal padding

Started by PG Bug reporting form2 months ago8 messagesbugs
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

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

decode() accepts Base64 alphabet characters after terminal '=' padding and
incorporates them into the decoded bytea value. Once terminal padding
completes a Base64 value, only ignorable whitespace may follow. Applications
relying on decode() to validate Base64 input may consequently process
malformed input as valid data.

Steps to reproduce
------------------
Run the following with psql:

\set ON_ERROR_STOP on
SELECT encode(decode('YQ==Yg==', 'base64'), 'hex') AS decoded_hex;
SELECT encode(decode('YQ==AAAA', 'base64'), 'hex') AS
decoded_hex_after_padding;

Actual result
-------------
decoded_hex
-------------
6162
(1 row)

decoded_hex_after_padding
---------------------------
6100
(1 row)

Expected result
---------------
Both decode() calls should reject their input with SQLSTATE 22023 because
Base64 alphabet characters occur after terminal '=' padding. They should not
silently decode the trailing payload.

Additional information
----------------------
The issue was reproduced on PostgreSQL 20devel, PostgreSQL 18.6, and
PostgreSQL 17.11.

#2shihao zhong
zhong950419@gmail.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19702: decode() accepts Base64 payload after terminal padding

Hi Qifan,

Thanks for reporting that issue.

I can reproduce this on master. The decoder sets "end" at the first "="
and never looks at it again, so later data and later "=" all pass

0001 raises an error for anything but whitespace after the padding, the
same rule base32hex already has. 0002 adds tests. 0003 fixes the two
copies of this code, pg_b64_decode() in src/common and the armor decoder
in pgcrypto. dearmor() shows the same bug when the CRC matches.

This rejects input that used to pass, so I am not sure about the back
branches. I would leave that to the committer.

Thanks,
Shihao

Attachments:

t253856_2
v1-0002-Add-tests-for-data-after-base64-padding.patchapplication/octet-stream; name=v1-0002-Add-tests-for-data-after-base64-padding.patchDownload+25-1
v1-0001-Reject-data-after-padding-in-base64-decoding.patchapplication/octet-stream; name=v1-0001-Reject-data-after-padding-in-base64-decoding.patchDownload+6-6
v1-0003-Reject-data-after-base64-padding-in-other-decoder.patchapplication/octet-stream; name=v1-0003-Reject-data-after-base64-padding-in-other-decoder.patchDownload+30-6
#3Daniel Gustafsson
daniel@yesql.se
In reply to: shihao zhong (#2)
Re: BUG #19702: decode() accepts Base64 payload after terminal padding

On 19 Sep 2026, at 16:59, shihao zhong <zhong950419@gmail.com> wrote:

Hi Qifan,

Thanks for reporting that issue.

I can reproduce this on master. The decoder sets "end" at the first "="
and never looks at it again, so later data and later "=" all pass

Sorry for missing this at the time of it being reported.

0001 raises an error for anything but whitespace after the padding, the
same rule base32hex already has. 0002 adds tests. 0003 fixes the two
copies of this code, pg_b64_decode() in src/common and the armor decoder
in pgcrypto. dearmor() shows the same bug when the CRC matches.

I had to stare the code for some time to convince myself there were no more
bugs hiding in pgcrypto for this wrt RFC9580, but I think it's correct.

This rejects input that used to pass, so I am not sure about the back
branches. I would leave that to the committer.

Thanks for the patches, the attached is a rebased squash of the three patches
since I would prefer to apply them all at once. Given the lack of complaints I
will err on the side of caution and keep this in master only. At least for now.

--
Daniel Gustafsson

Attachments:

t253856_3
0001-Reject-invalid-base64-encoded-values-with-data-after.patchapplication/octet-stream; name=0001-Reject-invalid-base64-encoded-values-with-data-after.patch; x-unix-mode=0644Download+61-11
#4Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Daniel Gustafsson (#3)
Re: BUG #19702: decode() accepts Base64 payload after terminal padding

On Mon, Nov 9, 2026 at 2:26 PM Daniel Gustafsson <daniel@yesql.se> wrote:

On 19 Sep 2026, at 16:59, shihao zhong <zhong950419@gmail.com> wrote:

Hi Qifan,

Thanks for reporting that issue.

I can reproduce this on master. The decoder sets "end" at the first "="
and never looks at it again, so later data and later "=" all pass

Sorry for missing this at the time of it being reported.

0001 raises an error for anything but whitespace after the padding, the
same rule base32hex already has. 0002 adds tests. 0003 fixes the two
copies of this code, pg_b64_decode() in src/common and the armor decoder
in pgcrypto. dearmor() shows the same bug when the CRC matches.

I had to stare the code for some time to convince myself there were no more
bugs hiding in pgcrypto for this wrt RFC9580, but I think it's correct.

This rejects input that used to pass, so I am not sure about the back
branches. I would leave that to the committer.

Thanks for the patches, the attached is a rebased squash of the three patches
since I would prefer to apply them all at once. Given the lack of complaints I
will err on the side of caution and keep this in master only. At least for now.

Thank you for taking care of it. I was also reviewing the patches and
was about to send LGTM.

While I agree that we don't backpatch it to v18 or older, I think it
(or at least the base64url part) should go into v19 as well. v19 is
still in RC and base64url is new in v19, so there are no existing
users relying on the current behavior, and it would be better to make
base64url and base32hex consistent before the release. IIUC the
base64url truncation fix 5f57f58179b was backpatched to v19 for the
same reason.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#5Daniel Gustafsson
daniel@yesql.se
In reply to: Masahiko Sawada (#4)
Re: BUG #19702: decode() accepts Base64 payload after terminal padding

On 9 Nov 2026, at 23:43, Masahiko Sawada <sawada.mshk@gmail.com> wrote:

Thank you for taking care of it. I was also reviewing the patches and
was about to send LGTM.

+1

While I agree that we don't backpatch it to v18 or older, I think it
(or at least the base64url part) should go into v19 as well. v19 is
still in RC and base64url is new in v19, so there are no existing
users relying on the current behavior, and it would be better to make
base64url and base32hex consistent before the release.

That's a good point, I'll prepare a backpatch for 19 for the relevant portions.

--
Daniel Gustafsson

#6Daniel Gustafsson
daniel@yesql.se
In reply to: Daniel Gustafsson (#5)
Re: BUG #19702: decode() accepts Base64 payload after terminal padding

On 10 Nov 2026, at 10:23, Daniel Gustafsson <daniel@yesql.se> wrote:

On 9 Nov 2026, at 23:43, Masahiko Sawada <sawada.mshk@gmail.com> wrote:

While I agree that we don't backpatch it to v18 or older, I think it
(or at least the base64url part) should go into v19 as well. v19 is
still in RC and base64url is new in v19, so there are no existing
users relying on the current behavior, and it would be better to make
base64url and base32hex consistent before the release.

That's a good point, I'll prepare a backpatch for 19 for the relevant portions.

The attached is a patch against master for the full fix, and a cut-down version
fixing just base64url for v19, I thinmk that's what we can do for 19 at this
point. What do you think about this one?

--
Daniel Gustafsson

Attachments:

t253856_6
v19-2-0001-Reject-invalid-base64-encoded-values-with-data.patchapplication/octet-stream; name=v19-2-0001-Reject-invalid-base64-encoded-values-with-data.patch; x-unix-mode=0644Download+23-6
v2-0001-Reject-invalid-base64-encoded-values-with-data-af.patchapplication/octet-stream; name=v2-0001-Reject-invalid-base64-encoded-values-with-data-af.patch; x-unix-mode=0644Download+70-11
#7Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Daniel Gustafsson (#6)
Re: BUG #19702: decode() accepts Base64 payload after terminal padding

On Thu, Nov 12, 2026 at 4:00 PM Daniel Gustafsson <daniel@yesql.se> wrote:

On 10 Nov 2026, at 10:23, Daniel Gustafsson <daniel@yesql.se> wrote:

On 9 Nov 2026, at 23:43, Masahiko Sawada <sawada.mshk@gmail.com> wrote:

While I agree that we don't backpatch it to v18 or older, I think it
(or at least the base64url part) should go into v19 as well. v19 is
still in RC and base64url is new in v19, so there are no existing
users relying on the current behavior, and it would be better to make
base64url and base32hex consistent before the release.

That's a good point, I'll prepare a backpatch for 19 for the relevant portions.

The attached is a patch against master for the full fix, and a cut-down version
fixing just base64url for v19, I thinmk that's what we can do for 19 at this
point. What do you think about this one?

Thank you for updating the patches. I think the following conditions
can be simplified:

---
+ if ((!url && !end) || (url && (!end || pos != 3)))

to

if (!end || (url && pos != 3))

---
+ if ((!url && c > 0 && c < 127) || (url && c > 0 && c < 127 && !end))

to

if (c > 0 && c < 127 && !(url && end))

The rest looks good to me.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#8shihao zhong
zhong950419@gmail.com
In reply to: Masahiko Sawada (#7)
Re: BUG #19702: decode() accepts Base64 payload after terminal padding

Hi,

Thanks Daniel for splitting it.

Thank you for updating the patches. I think the following conditions
can be simplified:

added to the backport patch.

Thanks,
Shihao

Attachments:

t253856_8
v19-3-0001-Reject-invalid-base64-encoded-values-with-data.patchapplication/octet-stream; name=v19-3-0001-Reject-invalid-base64-encoded-values-with-data.patchDownload+23-6
v2-0001-Reject-invalid-base64-encoded-values-with-data-af.patchapplication/octet-stream; name=v2-0001-Reject-invalid-base64-encoded-values-with-data-af.patchDownload+70-11