Commit
e1d917182 added support for base64url encoding, a base64 variant
intended to be safe for usage in URLs and filenames. The padding rules
for base64url and base64 differ in that base64url require no extra '='
padding, but the commit unintentionally relaxed this requirement for
base64 as well. Fix by making sure that the truncation logic check
for the encoding and add a test to make sure.
Backpatch down to v19 where support for base64url was introduced.
Author: Daniel Gustafsson <daniel@yesql.se>
Reviewed-by: David E. Wheeler <david@justatheory.com>
Discussion: https://postgr.es/m/
3258FC72-F5E1-40B9-B5D7-
64478CAF7728@yesql.se
Backpatch-through: 19
}
}
- if (pos == 2)
+ if (url && pos == 2)
{
buf <<= 12;
*p++ = (buf >> 16) & 0xFF;
}
- else if (pos == 3)
+ else if (url && pos == 3)
{
buf <<= 6;
*p++ = (buf >> 16) & 0xFF;
\x01
(1 row)
+-- Make sure the same 1 byte input isn't accepted as base64
+SELECT decode('AQ', 'base64'); -- \x01
+ERROR: invalid base64 end sequence
+HINT: Input data is missing padding, is truncated, or is otherwise corrupted.
-- 2 byte input
SELECT encode('\x0102'::bytea, 'base64url'); -- AQI
encode
SELECT encode('\x01', 'base64url'); -- AQ
SELECT decode('AQ', 'base64url'); -- \x01
+-- Make sure the same 1 byte input isn't accepted as base64
+SELECT decode('AQ', 'base64'); -- \x01
+
-- 2 byte input
SELECT encode('\x0102'::bytea, 'base64url'); -- AQI
SELECT decode('AQI', 'base64url'); -- \x0102