-
-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure key contents is used for all hashing algorithms
On v3.4.0 we introduced the `Lcobucci\JWT\Signer\Key\LocalFileReference`, which was designed to be used with OpenSSL-based algorithms and avoid having to load the file contents into user-land to sign/verify tokens. However, other algorithms don't understand the scheme `file://` and will incorrectly use the file path as the signing key. This modifies and deprecates `LocalFileReference` to avoid that misleading behaviour. Co-authored-by: Anton Smirnov <[email protected]> Co-authored-by: Marco Pivetta <[email protected]>
- Loading branch information
1 parent
511629a
commit c45bb8b
Showing
4 changed files
with
45 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,16 @@ | |
use Lcobucci\JWT\Builder; | ||
use Lcobucci\JWT\CheckForDeprecations; | ||
use Lcobucci\JWT\Parser; | ||
use Lcobucci\JWT\Signer\Key\InMemory; | ||
use Lcobucci\JWT\Signer\Key\LocalFileReference; | ||
use Lcobucci\JWT\Token; | ||
use Lcobucci\JWT\Signature; | ||
use Lcobucci\JWT\Signer\Hmac\Sha256; | ||
use Lcobucci\JWT\Signer\Hmac\Sha512; | ||
use ReflectionProperty; | ||
use function file_put_contents; | ||
use function sys_get_temp_dir; | ||
use function tempnam; | ||
|
||
/** | ||
* @author Luís Otávio Cobucci Oblonczyk <[email protected]> | ||
|
@@ -197,4 +203,39 @@ public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs() | |
$this->assertEquals('world', $token->getClaim('hello')); | ||
$this->assertTrue($token->verify($this->signer, 'testing')); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @coversNothing | ||
* | ||
* @see \Lcobucci\JWT\Signer\Key::setContent() | ||
*/ | ||
public function signatureValidationWithLocalFileKeyReferenceWillOperateWithKeyContents() | ||
{ | ||
$key = tempnam(sys_get_temp_dir(), 'key'); | ||
file_put_contents($key, 'just a dummy key'); | ||
|
||
$validKey = LocalFileReference::file($key); | ||
$invalidKey = InMemory::plainText($key); | ||
$signer = new Sha256(); | ||
|
||
// 3.4.x implicitly extracts key contents, when `file://` is detected | ||
$reflectionContents = new ReflectionProperty(InMemory::class, 'content'); | ||
$reflectionContents->setAccessible(true); | ||
$reflectionContents->setValue($invalidKey, 'file://' . $key); | ||
|
||
$token = (new Builder()) | ||
->withClaim('foo', 'bar') | ||
->getToken($signer, $validKey); | ||
|
||
self::assertFalse( | ||
$token->verify($signer, $invalidKey), | ||
'Token cannot be validated against the **path** of the key' | ||
); | ||
self::assertTrue( | ||
$token->verify($signer, $validKey), | ||
'Token can be validated against the **contents** of the key' | ||
); | ||
} | ||
} |