Skip to content

Commit 28bd786

Browse files
[9.x] Fixed errors occurring when encrypted cookies has been tampered with (#45313)
* Fixed errors occurring when encrypted cookies has been tampered with * Corrected code style * Update Encrypter.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 945c45e commit 28bd786

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/Illuminate/Encryption/Encrypter.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,21 @@ protected function getJsonPayload($payload)
229229
*/
230230
protected function validPayload($payload)
231231
{
232-
return is_array($payload) && isset($payload['iv'], $payload['value'], $payload['mac']) &&
233-
strlen(base64_decode($payload['iv'], true)) === openssl_cipher_iv_length(strtolower($this->cipher));
232+
if (! is_array($payload)) {
233+
return false;
234+
}
235+
236+
foreach (['iv', 'value', 'mac'] as $item) {
237+
if (! isset($payload[$item]) || ! is_string($payload[$item])) {
238+
return false;
239+
}
240+
}
241+
242+
if (isset($payload['tag']) && ! is_string($payload['tag'])) {
243+
return false;
244+
}
245+
246+
return strlen(base64_decode($payload['iv'], true)) === openssl_cipher_iv_length(strtolower($this->cipher));
234247
}
235248

236249
/**

tests/Encryption/EncrypterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,27 @@ public function testSupportedMethodAcceptsAnyCasing()
204204
$this->assertTrue(Encrypter::supported($key, 'aes-128-CBC'));
205205
$this->assertTrue(Encrypter::supported($key, 'aes-128-cbc'));
206206
}
207+
208+
public function provideTamperedData()
209+
{
210+
return [
211+
[['iv' => ['value_in_array'], 'value' => '', 'mac' => '']],
212+
[['iv' => '', 'value' => '', 'mac' => '']],
213+
[['iv' => '', 'value' => ['value_in_array'], 'mac' => '']],
214+
[['iv' => '', 'value' => '', 'mac' => ['value_in_array']]],
215+
[['iv' => '', 'value' => '', 'mac' => ['value_in_array'], 'tag' => ['value_in_array']]],
216+
];
217+
}
218+
219+
/**
220+
* @dataProvider provideTamperedData
221+
*/
222+
public function testTamperedPayloadWillGetRejected($payload)
223+
{
224+
$this->expectException(DecryptException::class);
225+
$this->expectExceptionMessage('The payload is invalid.');
226+
227+
$enc = new Encrypter(str_repeat('x', 16));
228+
$enc->decrypt(base64_encode(json_encode($payload)));
229+
}
207230
}

0 commit comments

Comments
 (0)