Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Throw if tag is passed but is not supported #41479

Merged
merged 3 commits into from Mar 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/Illuminate/Encryption/Encrypter.php
Expand Up @@ -159,11 +159,9 @@ public function decrypt($payload, $unserialize = true)

$iv = base64_decode($payload['iv']);

$tag = empty($payload['tag']) ? null : base64_decode($payload['tag']);

if (self::$supportedCiphers[strtolower($this->cipher)]['aead'] && strlen($tag) !== 16) {
throw new DecryptException('Could not decrypt the data.');
}
$this->ensureTagIsValid(
$tag = empty($payload['tag']) ? null : base64_decode($payload['tag'])
);

// Here we will decrypt the value. If we are able to successfully decrypt it
// we will then unserialize it and return it out to the caller. If we are
Expand Down Expand Up @@ -255,6 +253,23 @@ protected function validMac(array $payload)
);
}

/**
* Ensure the given tag is a valid tag given the selected cipher.
*
* @param string $tag
* @return void
*/
protected function ensureTagIsValid($tag)
{
if (self::$supportedCiphers[strtolower($this->cipher)]['aead'] && strlen($tag) !== 16) {
throw new DecryptException('Could not decrypt the data.');
}

if (! self::$supportedCiphers[strtolower($this->cipher)]['aead'] && is_string($tag)) {
throw new DecryptException('Unable to use tag because the cipher algorithm does not support AEAD.');
}
}

/**
* Get the encryption key.
*
Expand Down