Skip to content

Commit 0bac9b3

Browse files
authored
Upgrade PHPStan (#216)
1 parent 7ed2390 commit 0bac9b3

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"require-dev": {
2828
"phpunit/phpunit": "^7.0",
29-
"phpstan/phpstan": "^0.9.2"
29+
"phpstan/phpstan": "^0.11"
3030
},
3131
"autoload": {
3232
"psr-4" : {

src/Encryption.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public static function deterministicEncrypt(string $payload, string $userPublicK
8787
// get local key pair
8888
list($localPublicKeyObject, $localPrivateKeyObject) = $localKeyObject;
8989
$localPublicKey = hex2bin(Utils::serializePublicKey($localPublicKeyObject));
90+
if (!$localPublicKey) {
91+
throw new \ErrorException('Failed to convert local public key from hexadecimal to binary');
92+
}
9093

9194
// get user public key object
9295
[$userPublicKeyObjectX, $userPublicKeyObjectY] = Utils::unserializePublicKey($userPublicKey);
@@ -98,6 +101,9 @@ public static function deterministicEncrypt(string $payload, string $userPublicK
98101
// get shared secret from user public key and local private key
99102
$sharedSecret = $curve->mul($userPublicKeyObject->getPoint(), $localPrivateKeyObject->getSecret())->getX();
100103
$sharedSecret = hex2bin(str_pad(gmp_strval($sharedSecret, 16), 64, '0', STR_PAD_LEFT));
104+
if (!$sharedSecret) {
105+
throw new \ErrorException('Failed to convert shared secret from hexadecimal to binary');
106+
}
101107

102108
// section 4.3
103109
$ikm = self::getIKM($userAuthToken, $userPublicKey, $localPublicKey, $sharedSecret, $contentEncoding);
@@ -115,6 +121,7 @@ public static function deterministicEncrypt(string $payload, string $userPublicK
115121

116122
// encrypt
117123
// "The additional data passed to each invocation of AEAD_AES_128_GCM is a zero-length octet sequence."
124+
$tag = '';
118125
$encryptedText = openssl_encrypt($payload, 'aes-128-gcm', $contentEncryptionKey, OPENSSL_RAW_DATA, $nonce, $tag);
119126

120127
// return values in url safe base64

src/VAPID.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ public static function validate(array $vapid): array
6060
gmp_init(bin2hex(Base64Url::decode($jwk->get('x'))), 16),
6161
gmp_init(bin2hex(Base64Url::decode($jwk->get('y'))), 16)
6262
));
63-
$vapid['publicKey'] = base64_encode(hex2bin(Utils::serializePublicKey($publicKey)));
63+
64+
$binaryPublicKey = hex2bin(Utils::serializePublicKey($publicKey));
65+
if (!$binaryPublicKey) {
66+
throw new \ErrorException('Failed to convert VAPID public key from hexadecimal to binary');
67+
}
68+
$vapid['publicKey'] = base64_encode($binaryPublicKey);
6469
$vapid['privateKey'] = base64_encode(str_pad(Base64Url::decode($jwk->get('d')), 2 * self::PRIVATE_KEY_LENGTH, '0', STR_PAD_LEFT));
6570
}
6671

@@ -122,6 +127,9 @@ public static function getVapidHeaders(string $audience, string $subject, string
122127
'exp' => $expiration,
123128
'sub' => $subject,
124129
], JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
130+
if (!$jwtPayload) {
131+
throw new \ErrorException('Failed to encode JWT payload in JSON');
132+
}
125133

126134
list($x, $y) = Utils::unserializePublicKey($publicKey);
127135
$jwk = JWK::create([
@@ -163,16 +171,27 @@ public static function getVapidHeaders(string $audience, string $subject, string
163171
* DO NOT create keys at each initialization! Save those keys and reuse them.
164172
*
165173
* @return array
174+
* @throws \ErrorException
166175
*/
167176
public static function createVapidKeys(): array
168177
{
169178
$curve = NistCurve::curve256();
170179
$privateKey = $curve->createPrivateKey();
171180
$publicKey = $curve->createPublicKey($privateKey);
172181

182+
$binaryPublicKey = hex2bin(Utils::serializePublicKey($publicKey));
183+
if (!$binaryPublicKey) {
184+
throw new \ErrorException('Failed to convert VAPID public key from hexadecimal to binary');
185+
}
186+
187+
$binaryPrivateKey = hex2bin(str_pad(gmp_strval($privateKey->getSecret(), 16), 2 * self::PRIVATE_KEY_LENGTH, '0', STR_PAD_LEFT));
188+
if (!$binaryPrivateKey) {
189+
throw new \ErrorException('Failed to convert VAPID private key from hexadecimal to binary');
190+
}
191+
173192
return [
174-
'publicKey' => base64_encode(hex2bin(Utils::serializePublicKey($publicKey))),
175-
'privateKey' => base64_encode(hex2bin(str_pad(gmp_strval($privateKey->getSecret(), 16), 2 * self::PRIVATE_KEY_LENGTH, '0', STR_PAD_LEFT)))
193+
'publicKey' => base64_encode($binaryPublicKey),
194+
'privateKey' => base64_encode($binaryPrivateKey)
176195
];
177196
}
178197
}

src/WebPush.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public function flush(?int $batchSize = null): \Generator
152152
{
153153
if (null === $this->notifications || empty($this->notifications)) {
154154
yield from [];
155+
return;
155156
}
156157

157158
if (null === $batchSize) {

0 commit comments

Comments
 (0)