php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52327 base64_decode() improper handling of leading padding.
Submitted: 2010-07-13 11:12 UTC Modified: 2010-11-26 21:59 UTC
From: yuri-sevatz at hotmail dot com Assigned: iliaa
Status: Closed Package: *Compression related
PHP Version: 5.3.2 OS: ALL
Private report: No CVE-ID:
 [2010-07-13 11:12 UTC] yuri-sevatz at hotmail dot com
Description:
------------
According to the MIME standard that we're following, this isn't supposed to work when strict mode is enabled.  Equal-signs are strictly reserved for padding characters at the _end_ of a base64 MIME string.

Leading '=' signs are allowed in each block of 4 bytes because the condition that checks for invalid placement of the '=' signs only checks if (i % 4) == 1, 

Whereas it should check if (i % 4) is 0 or 1.

You should be able to change the circumstances for a valid '=' placement, as follows, to conform to the standard:

// to see if bit position 2 is set (for all values of i = n*4+2 and i = n*4+3)
if (i & 2)
    // means that the '=' is in a valid location



I would also suggest *carefully* looking over the block at the end of the base64_decode() function... as it seems that if (ch == base64_pad) after ch is ALWAYS set to '\0' is an impossible scenario.

Test script:
---------------
<?php 
echo base64_decode('=VGhl=ICc9=JyBz=eW1i=b2xz=IGFy=ZW4n=dCBh=bGxv=d2Vk=IHdo=ZXJl=IGkg=cHV0=IHRo=ZW0g=by5P');
?>


Expected result:
----------------
should return FALSE/NULL/Errornous value

Actual result:
--------------
echos:

The '=' symbols aren't allowed where i put them o.O

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-26 21:53 UTC] iliaa@php.net
-Assigned To: +Assigned To: iliaa
 [2010-11-26 21:59 UTC] iliaa@php.net
Automatic comment from SVN on behalf of iliaa
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=305779
Log: Fixed bug #52327 (base64_decode() improper handling of leading padding in strict mode)
 [2010-11-26 21:59 UTC] iliaa@php.net
-Status: Assigned +Status: Closed
 [2010-11-26 21:59 UTC] iliaa@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Fixed when strict mode is being used.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 23:01:58 2014 UTC