Bug #72264 [Opn]: base64_decode $strict fails with whitespace between padding

From: Date: Thu, 16 Jun 2016 13:45:29 +0000
Subject: Bug #72264 [Opn]: base64_decode $strict fails with whitespace between padding
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201679@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72264&edit=1 ID: 72264 User updated by: lauri dot kentta at gmail dot com Reported by: lauri dot kentta at gmail dot com Summary: base64_decode $strict fails with whitespace between padding Status: Open Type: Bug Package: Strings related PHP Version: 7.0.6 Block user comment: N Private report: N New Comment: > Line breaks are allowed, but as far as I know, not in arbitrary > places, and no other whitespace characters are allowed. RFC 4648 (Base* encoding) doesn't cover these issues; it only says that any characters outside the Base64 alphabet MUST be rejected, UNLESS another specification says otherwise. This means that we can really do as we please, since the same function may be used in different contexts. RFC 2045 (MIME) says that "any characters outside of the base64 alphabet are to be ignored in base64-encoded data". This is what the "non-strict" mode does, so this is fine, even though it's really weird to accept stuff like !"#%&. The current C code specifically tries to ignore white-space even in "strict" mode. I think that the PHP documentation is "wrong" in this regard and that this bug should be fixed. It's obvious that the current "strict" mode is actually intended as a sensible mode for any sensible input, with the benefit that anything weird like leaked errors inside the Base64 string will probably produce FALSE. Many tools add line breaks in Base64 data, so it's nice to accept that. (echo -n U | base64 -w3 == "VQ=\n=\n") == true. Also, changing the current behavior to be more strict would cause a BC break, which is probably not acceptable. PHP doesn't really implement a strict mode for Base64. So maybe some day we should implement all three modes: - MIME mode, the current default, ignores any weirdness. - sensible mode, the current "strict", with Base64 + white space - really strict mode, which only accepts 0-9A-Za-z+/= Previous Comments: ------------------------------------------------------------------------ [2016-06-16 12:51:08] cmb@php.net > Whitespace (at least line breaks) is allowed in Base64. Line breaks are allowed, but as far as I know, not in arbitrary places, and no other whitespace characters are allowed. > In other words, base64_decode("VV ==") and base64_decode("VV= > =") should yield the same result. ACK. Obviously that is not the case now: <https://3v4l.org/4ZV7T>. ------------------------------------------------------------------------ [2016-06-14 15:20:45] lauri dot kentta at gmail dot com Whitespace (at least line breaks) is allowed in Base64. And even with your logic, at least the whitespace should be handled equally in all cases. In other words, base64_decode("VV ==") and base64_decode("VV= =") should yield the same result. ------------------------------------------------------------------------ [2016-06-14 13:14:20] cmb@php.net In my opinion, this is not a bug, but rather expected behavior[1]: | Returns FALSE if input contains character from outside the | base64 alphabet. [1] <http://php.net/manual/en/function.base64-decode.php> ------------------------------------------------------------------------ [2016-05-25 18:55:23] lauri dot kentta at gmail dot com Description: ------------ base64_decode $strict fails with whitespace between padding. Test script: --------------- <?php var_dump(base64_decode("VV= =", true)); Expected result: ---------------- string(1) "U" Actual result: -------------- bool(false) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72264&edit=1

« previous php.bugs (#201679) next »