Skip to content

Commit 886d261

Browse files
committed
check iv length
1 parent 707d669 commit 886d261

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Illuminate/Encryption/Encrypter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,8 @@ protected function getJsonPayload($payload)
206206
*/
207207
protected function validPayload($payload)
208208
{
209-
return is_array($payload) && isset(
210-
$payload['iv'], $payload['value'], $payload['mac']
211-
);
209+
return is_array($payload) && isset($payload['iv'], $payload['value'], $payload['mac']) &&
210+
strlen(base64_decode($payload['iv'], true)) === openssl_cipher_iv_length($this->cipher);
212211
}
213212

214213
/**

tests/Encryption/EncrypterTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,19 @@ public function testExceptionThrownWithDifferentKey()
102102
$b = new Encrypter(str_repeat('b', 16));
103103
$b->decrypt($a->encrypt('baz'));
104104
}
105+
106+
/**
107+
* @expectedException \Illuminate\Contracts\Encryption\DecryptException
108+
* @expectedExceptionMessage The payload is invalid.
109+
*/
110+
public function testExceptionThrownWhenIvIsTooLong()
111+
{
112+
$e = new Encrypter(str_repeat('a', 16));
113+
$payload = $e->encrypt('foo');
114+
$data = json_decode(base64_decode($payload), true);
115+
$data['iv'] .= $data['value'][0];
116+
$data['value'] = substr($data['value'], 1);
117+
$modified_payload = base64_encode(json_encode($data));
118+
$e->decrypt($modified_payload);
119+
}
105120
}

0 commit comments

Comments
 (0)