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

Fix detached and non-encoded non-UTF8 string #495

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Component/Encryption/JWELoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use RuntimeException;
use Throwable;

/**
* @see \Jose\Tests\Component\Encryption\JWELoaderTest
*/
class JWELoader
{
public function __construct(
Expand Down
3 changes: 3 additions & 0 deletions src/Component/Signature/JWS.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Jose\Component\Core\JWT;
use function count;

/**
* @see \Jose\Tests\Component\Signature\JWSTest
*/
class JWS implements JWT
{
/**
Expand Down
10 changes: 7 additions & 3 deletions src/Component/Signature/JWSBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ public function create(): self
*/
public function withPayload(string $payload, bool $isPayloadDetached = false): self
{
if (mb_detect_encoding($payload, 'UTF-8', true) === false) {
throw new InvalidArgumentException('The payload must be encoded in UTF-8');
}
$clone = clone $this;
$clone->payload = $payload;
$clone->isPayloadDetached = $isPayloadDetached;
Expand Down Expand Up @@ -124,6 +121,13 @@ public function build(): JWS
$encodedPayload = $this->isPayloadEncoded === false ? $this->payload : Base64UrlSafe::encodeUnpadded(
$this->payload
);

if ($this->isPayloadEncoded === false && $this->isPayloadDetached === false) {
mb_detect_encoding($this->payload, 'UTF-8', true) !== false || throw new InvalidArgumentException(
'The payload must be encoded in UTF-8'
);
}

$jws = new JWS($this->payload, $encodedPayload, $this->isPayloadDetached);
foreach ($this->signatures as $signature) {
/** @var MacAlgorithm|SignatureAlgorithm $algorithm */
Expand Down
3 changes: 3 additions & 0 deletions src/Component/Signature/JWSLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Jose\Component\Signature\Serializer\JWSSerializerManager;
use Throwable;

/**
* @see \Jose\Tests\Component\Signature\JWSLoaderTest
*/
class JWSLoader
{
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class CompressionTestCase extends EncryptionTestCase
final class CompressionTest extends EncryptionTestCase
{
#[Test]
public function getValidCompressionAlgorithm(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @internal
*/
final class ECDHESWithX25519EncryptionTestCase extends EncryptionTestCase
final class ECDHESWithX25519EncryptionTest extends EncryptionTestCase
{
/**
* @see https://tools.ietf.org/html/rfc7516#appendix-B
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @internal
*/
final class EncrypterTestCase extends EncryptionTestCase
final class EncrypterTest extends EncryptionTestCase
{
#[Test]
public function encryptWithJWTInput(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @internal
*/
final class InvalidCurveAttackTestCase extends EncryptionTestCase
final class InvalidCurveAttackTest extends EncryptionTestCase
{
#[Test]
public function curveCheckNegativeP256AttackPt1(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @internal
*/
final class JWEFlattenedTestCase extends EncryptionTestCase
final class JWEFlattenedTest extends EncryptionTestCase
{
/**
* @see https://tools.ietf.org/html/rfc7516#appendix-A.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class JWELoaderTestCase extends EncryptionTestCase
final class JWELoaderTest extends EncryptionTestCase
{
private ?JWELoader $jweLoader = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @internal
*/
final class JWESplitTestCase extends EncryptionTestCase
final class JWESplitTest extends EncryptionTestCase
{
#[Test]
public function aJweObjectWithMoreThanOneRecipientCanBeSplittedIntoSeveralJweObjects(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class A128KWAndA128GCMEncryptionProtectedContentOnlyTestCase extends EncryptionTestCase
final class A128KWAndA128GCMEncryptionProtectedContentOnlyTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class A128KWAndA128GCMEncryptionTestCase extends EncryptionTestCase
final class A128KWAndA128GCMEncryptionTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class A128KWAndA128GCMEncryptionWithAdditionalAuthenticatedDataTestCase extends EncryptionTestCase
final class A128KWAndA128GCMEncryptionWithAdditionalAuthenticatedDataTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class A128KWAndA128GCMEncryptionWithCompressionTestCase extends EncryptionTestCase
final class A128KWAndA128GCMEncryptionWithCompressionTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class A128KWAndA128GCMEncryptionWithSpecificProtectedHeaderValuesTestCase extends EncryptionTestCase
final class A128KWAndA128GCMEncryptionWithSpecificProtectedHeaderValuesTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class A256GCMKWAndA128CBC_HS256EncryptionTestCase extends EncryptionTestCase
final class A256GCMKWAndA128CBC_HS256EncryptionTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class DirAndA128GCMEncryptionTestCase extends EncryptionTestCase
final class DirAndA128GCMEncryptionTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class ECDH_ES_A128KWAndA128GCMEncryptionTestCase extends EncryptionTestCase
final class ECDH_ES_A128KWAndA128GCMEncryptionTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class ECDH_ES_AndA128CBC_HS256EncryptionTestCase extends EncryptionTestCase
final class ECDH_ES_AndA128CBC_HS256EncryptionTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @internal
*/
final class MultipleRecipientEncryptionTestCase extends EncryptionTestCase
final class MultipleRecipientEncryptionTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @internal
*/
final class PBES2_HS512_A256KWAndA128CBC_HS256EncryptionTestCase extends EncryptionTestCase
final class PBES2_HS512_A256KWAndA128CBC_HS256EncryptionTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class RSA1_5AndA128CBC_HS256EncryptionTestCase extends EncryptionTestCase
final class RSA1_5AndA128CBC_HS256EncryptionTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class RSA_OAEPAndA256GCMEncryptionTestCase extends EncryptionTestCase
final class RSA_OAEPAndA256GCMEncryptionTest extends EncryptionTestCase
{
/**
* Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @internal
*/
final class RSAEncryptionTestCase extends EncryptionTestCase
final class RSAEncryptionTest extends EncryptionTestCase
{
/**
* @see https://tools.ietf.org/html/rfc7516
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @internal
*/
final class RSAKeyEncryptionTestCase extends EncryptionTestCase
final class RSAKeyEncryptionTest extends EncryptionTestCase
{
#[Test]
public function invalidKey(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class RSAKeyWithoutAllPrimesTestCase extends EncryptionTestCase
final class RSAKeyWithoutAllPrimesTest extends EncryptionTestCase
{
#[DataProvider('dataEncryptionAlgorithms')]
#[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @internal
*/
final class NestingTestCase extends NestedTokenTestCase
final class NestingTest extends NestedTokenTestCase
{
#[Test]
public function decryption(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @internal
*/
final class ForeignJWTTestCase extends SignatureTestCase
final class ForeignJWTTest extends SignatureTestCase
{
/*
* The following test uses an assertion created with another library.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @internal
*/
final class JWSFlattenedTestCase extends SignatureTestCase
final class JWSFlattenedTest extends SignatureTestCase
{
/**
* @see https://tools.ietf.org/html/rfc7516#appendix-A.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @internal
*/
final class JWSLoaderTestCase extends SignatureTestCase
final class JWSLoaderTest extends SignatureTestCase
{
private ?JWSLoader $jwsLoader = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @internal
*/
final class JWSSplitTestCase extends SignatureTestCase
final class JWSSplitTest extends SignatureTestCase
{
#[Test]
public function aJwsObjectWithMoreThanOneRecipientCanBeSplittedIntoSeveralJwsObjects(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @internal
*/
final class JWSTestCase extends SignatureTestCase
final class JWSTest extends SignatureTestCase
{
#[Test]
public function jWS(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @internal
*/
final class MultipleSignaturesTestCase extends SignatureTestCase
final class MultipleSignaturesTest extends SignatureTestCase
{
#[Test]
public function multipleSignatures(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @internal
*/
final class NestingTestCase extends SignatureTestCase
final class NestingTest extends SignatureTestCase
{
#[Test]
public function signatureVerification(): void
Expand Down
Loading
Loading