Skip to content

Commit

Permalink
check iv length
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 30, 2018
1 parent 707d669 commit 886d261
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Illuminate/Encryption/Encrypter.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,8 @@ protected function getJsonPayload($payload)
*/
protected function validPayload($payload)
{
return is_array($payload) && isset(
$payload['iv'], $payload['value'], $payload['mac']
);
return is_array($payload) && isset($payload['iv'], $payload['value'], $payload['mac']) &&
strlen(base64_decode($payload['iv'], true)) === openssl_cipher_iv_length($this->cipher);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Encryption/EncrypterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,19 @@ public function testExceptionThrownWithDifferentKey()
$b = new Encrypter(str_repeat('b', 16));
$b->decrypt($a->encrypt('baz'));
}

/**
* @expectedException \Illuminate\Contracts\Encryption\DecryptException
* @expectedExceptionMessage The payload is invalid.
*/
public function testExceptionThrownWhenIvIsTooLong()
{
$e = new Encrypter(str_repeat('a', 16));
$payload = $e->encrypt('foo');
$data = json_decode(base64_decode($payload), true);
$data['iv'] .= $data['value'][0];
$data['value'] = substr($data['value'], 1);
$modified_payload = base64_encode(json_encode($data));
$e->decrypt($modified_payload);
}
}

0 comments on commit 886d261

Please sign in to comment.