Skip to content

Commit 4040256

Browse files
committed
use safe mb_strlen instead of strlen (for mbstring func overload errors)
1 parent 58439c5 commit 4040256

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

src/Encryption.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class Encryption
2626
*/
2727
public static function padPayload($payload, $automatic)
2828
{
29-
$payloadLen = strlen($payload);
29+
$payloadLen = Utils::safe_strlen($payload);
3030
$padLen = $automatic ? self::MAX_PAYLOAD_LENGTH - $payloadLen : 0;
3131
return pack('n*', $padLen).str_pad($payload, $padLen + $payloadLen, chr(0), STR_PAD_LEFT);
3232
}
@@ -138,12 +138,12 @@ private static function hkdf($salt, $ikm, $info, $length)
138138
*/
139139
private static function createContext($clientPublicKey, $serverPublicKey)
140140
{
141-
if (strlen($clientPublicKey) !== 65) {
141+
if (Utils::safe_strlen($clientPublicKey) !== 65) {
142142
throw new \ErrorException('Invalid client public key length');
143143
}
144144

145145
// This one should never happen, because it's our code that generates the key
146-
if (strlen($serverPublicKey) !== 65) {
146+
if (Utils::safe_strlen($serverPublicKey) !== 65) {
147147
throw new \ErrorException('Invalid server public key length');
148148
}
149149

@@ -163,7 +163,7 @@ private static function createContext($clientPublicKey, $serverPublicKey)
163163
* @throws \ErrorException
164164
*/
165165
private static function createInfo($type, $context) {
166-
if (strlen($context) !== 135) {
166+
if (Utils::safe_strlen($context) !== 135) {
167167
throw new \ErrorException('Context argument has invalid size');
168168
}
169169

src/Utils.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the WebPush library.
5+
*
6+
* (c) Louis Lagrange <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Minishlink\WebPush;
13+
14+
class Utils
15+
{
16+
public static function safe_strlen($string) {
17+
return mb_strlen($string, "8bit");
18+
}
19+
}

src/WebPush.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function __construct(array $apiKeys = array(), $defaultOptions = array(),
7575
public function sendNotification($endpoint, $payload = null, $userPublicKey = null, $userAuthToken = null, $flush = false, $options = array())
7676
{
7777
if(isset($payload)) {
78-
if (strlen($payload) > Encryption::MAX_PAYLOAD_LENGTH) {
78+
if (Utils::safe_strlen($payload) > Encryption::MAX_PAYLOAD_LENGTH) {
7979
throw new \ErrorException('Size of payload must not be greater than '.Encryption::MAX_PAYLOAD_LENGTH.' octets.');
8080
}
8181

@@ -178,7 +178,7 @@ private function prepareAndSend(array $notifications)
178178
$encrypted = Encryption::encrypt($payload, $userPublicKey, $userAuthToken, $this->nativePayloadEncryptionSupport);
179179

180180
$headers = array(
181-
'Content-Length' => strlen($encrypted['cipherText']),
181+
'Content-Length' => Utils::safe_strlen($encrypted['cipherText']),
182182
'Content-Type' => 'application/octet-stream',
183183
'Content-Encoding' => 'aesgcm',
184184
'Encryption' => 'keyid="p256dh";salt="'.$encrypted['salt'].'"',
@@ -204,7 +204,7 @@ private function prepareAndSend(array $notifications)
204204
$headers['Topic'] = $options['topic'];
205205
}
206206

207-
if (substr($endpoint, 0, strlen(self::GCM_URL)) === self::GCM_URL) {
207+
if (substr($endpoint, 0, Utils::safe_strlen(self::GCM_URL)) === self::GCM_URL) {
208208
if (array_key_exists('GCM', $this->apiKeys)) {
209209
$headers['Authorization'] = 'key='.$this->apiKeys['GCM'];
210210
} else {

tests/EncryptionTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
use Minishlink\WebPush\Encryption;
13+
use Minishlink\WebPush\Utils;
1314

1415
class EncryptionTest extends PHPUnit_Framework_TestCase
1516
{
@@ -23,13 +24,13 @@ public function testPadPayload($payload)
2324
$res = Encryption::padPayload($payload, true);
2425

2526
$this->assertContains('test', $res);
26-
$this->assertEquals(4080, strlen($res));
27+
$this->assertEquals(4080, Utils::safe_strlen($res));
2728
}
2829

2930
public function payloadProvider()
3031
{
3132
return array(
32-
array('test'),
33+
array('testé'),
3334
array(str_repeat('test', 1019)),
3435
array(str_repeat('test', 1019).'te'),
3536
);

0 commit comments

Comments
 (0)