Skip to content

Commit

Permalink
[mms] Don't use JSON driver to pack if input contains non-UTF8 data (…
Browse files Browse the repository at this point in the history
…Bug #13275).
  • Loading branch information
slusarz committed Jun 25, 2014
1 parent c1201e1 commit d33b22b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
6 changes: 5 additions & 1 deletion framework/Pack/lib/Horde/Pack.php
Expand Up @@ -126,7 +126,11 @@ public function pack($data, array $opts = array())
* 128 - RESERVED for future use (if set, indicates that initial
* byte will extend into next byte).
* Packed (and compressed data) follows this byte. */
$packed = $val->pack($data);
try {
$packed = $val->pack($data);
} catch (Horde_Pack_Exception $e) {
continue;
}

if ($opts['compress'] !== false) {
if ($opts['compress'] === 0) {
Expand Down
7 changes: 6 additions & 1 deletion framework/Pack/lib/Horde/Pack/Driver/Json.php
Expand Up @@ -37,12 +37,17 @@ static public function supported()
*/
public function pack($data)
{
$d = json_encode($data);
if (json_last_error() === 5) {
throw new Horde_Pack_Exception('Non UTF-8 data cannot be JSON packed.');
}

/* For JSON, we need to keep track whether the initial data was
* an object or class. The initial JSON character is one of the
* following:
* 0: Non-array
* 1: Array */
return intval(is_array($data)) . json_encode($data);
return intval(is_array($data)) . $d;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions framework/Pack/package.xml
Expand Up @@ -21,7 +21,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Don&apos;t use JSON driver to pack if input contains non-UTF8 data (Bug #13275).
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand Down Expand Up @@ -182,7 +182,7 @@
<date>2014-04-03</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Don&apos;t use JSON driver to pack if input contains non-UTF8 data (Bug #13275).
</notes>
</release>
</changelog>
Expand Down
21 changes: 21 additions & 0 deletions framework/Pack/test/Horde/Pack/PackTest.php
Expand Up @@ -34,4 +34,25 @@ public function testExpectedExceptionOnSerialize()
serialize($pack);
}

// Bug #13275
public function testNonUtf8Pack()
{
// ISO-8859-1 string
$data = base64_decode('VORzdA==');

$pack = new Horde_Pack();

$p = $pack->pack($data, array(
'drivers' => array(
'Horde_Pack_Driver_Json',
'Horde_Pack_Driver_Serialize'
)
));

$this->assertEquals(
$data,
$pack->unpack($p)
);
}

}

0 comments on commit d33b22b

Please sign in to comment.