Skip to content

Commit a368e42

Browse files
authored
chore: use more explicit error handling with json_encode (#437)
Remove `JSON_NUMERIC_CHECK` because setting enables autocasting which is not intended and can cause random runtime errors. According to documentation: `Encodes numeric strings as numbers.`
1 parent bb8e894 commit a368e42

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/VAPID.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,18 @@ public static function getVapidHeaders(
113113
'alg' => 'ES256',
114114
];
115115

116-
$jwtPayload = json_encode([
117-
'aud' => $audience,
118-
'exp' => $expiration,
119-
'sub' => $subject,
120-
], JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
121-
if (!$jwtPayload) {
122-
throw new \ErrorException('Failed to encode JWT payload in JSON');
116+
117+
try {
118+
$jwtPayload = json_encode(
119+
[
120+
'aud' => $audience,
121+
'exp' => $expiration,
122+
'sub' => $subject,
123+
],
124+
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
125+
);
126+
} catch (\JsonException $e) {
127+
throw new \ErrorException('Failed to encode JWT payload in JSON: '.$e->getMessage());
123128
}
124129

125130
[$x, $y] = Utils::unserializePublicKey($publicKey);

0 commit comments

Comments
 (0)