Skip to content

Commit ab7c59e

Browse files
authored
Fix detached and non-encoded non-UTF8 string (#495)
Fix detached and non-encoded non-UTF8 string
1 parent 74e75f3 commit ab7c59e

36 files changed

+86
-37
lines changed

src/Component/Encryption/JWELoader.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use RuntimeException;
1212
use Throwable;
1313

14+
/**
15+
* @see \Jose\Tests\Component\Encryption\JWELoaderTest
16+
*/
1417
class JWELoader
1518
{
1619
public function __construct(

src/Component/Signature/JWS.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Jose\Component\Core\JWT;
99
use function count;
1010

11+
/**
12+
* @see \Jose\Tests\Component\Signature\JWSTest
13+
*/
1114
class JWS implements JWT
1215
{
1316
/**

src/Component/Signature/JWSBuilder.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ public function create(): self
6969
*/
7070
public function withPayload(string $payload, bool $isPayloadDetached = false): self
7171
{
72-
if (mb_detect_encoding($payload, 'UTF-8', true) === false) {
73-
throw new InvalidArgumentException('The payload must be encoded in UTF-8');
74-
}
7572
$clone = clone $this;
7673
$clone->payload = $payload;
7774
$clone->isPayloadDetached = $isPayloadDetached;
@@ -124,6 +121,13 @@ public function build(): JWS
124121
$encodedPayload = $this->isPayloadEncoded === false ? $this->payload : Base64UrlSafe::encodeUnpadded(
125122
$this->payload
126123
);
124+
125+
if ($this->isPayloadEncoded === false && $this->isPayloadDetached === false) {
126+
mb_detect_encoding($this->payload, 'UTF-8', true) !== false || throw new InvalidArgumentException(
127+
'The payload must be encoded in UTF-8'
128+
);
129+
}
130+
127131
$jws = new JWS($this->payload, $encodedPayload, $this->isPayloadDetached);
128132
foreach ($this->signatures as $signature) {
129133
/** @var MacAlgorithm|SignatureAlgorithm $algorithm */

src/Component/Signature/JWSLoader.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Jose\Component\Signature\Serializer\JWSSerializerManager;
1212
use Throwable;
1313

14+
/**
15+
* @see \Jose\Tests\Component\Signature\JWSLoaderTest
16+
*/
1417
class JWSLoader
1518
{
1619
public function __construct(

tests/Component/Encryption/CompressionTestCase.php renamed to tests/Component/Encryption/CompressionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @internal
1616
*/
17-
final class CompressionTestCase extends EncryptionTestCase
17+
final class CompressionTest extends EncryptionTestCase
1818
{
1919
#[Test]
2020
public function getValidCompressionAlgorithm(): void

tests/Component/Encryption/ECDHESWithX25519EncryptionTestCase.php renamed to tests/Component/Encryption/ECDHESWithX25519EncryptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @internal
1414
*/
15-
final class ECDHESWithX25519EncryptionTestCase extends EncryptionTestCase
15+
final class ECDHESWithX25519EncryptionTest extends EncryptionTestCase
1616
{
1717
/**
1818
* @see https://tools.ietf.org/html/rfc7516#appendix-B

tests/Component/Encryption/EncrypterTestCase.php renamed to tests/Component/Encryption/EncrypterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* @internal
1616
*/
17-
final class EncrypterTestCase extends EncryptionTestCase
17+
final class EncrypterTest extends EncryptionTestCase
1818
{
1919
#[Test]
2020
public function encryptWithJWTInput(): void

tests/Component/Encryption/InvalidCurveAttackTestCase.php renamed to tests/Component/Encryption/InvalidCurveAttackTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* @internal
1212
*/
13-
final class InvalidCurveAttackTestCase extends EncryptionTestCase
13+
final class InvalidCurveAttackTest extends EncryptionTestCase
1414
{
1515
#[Test]
1616
public function curveCheckNegativeP256AttackPt1(): void

tests/Component/Encryption/JWEFlattenedTestCase.php renamed to tests/Component/Encryption/JWEFlattenedTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* @internal
1313
*/
14-
final class JWEFlattenedTestCase extends EncryptionTestCase
14+
final class JWEFlattenedTest extends EncryptionTestCase
1515
{
1616
/**
1717
* @see https://tools.ietf.org/html/rfc7516#appendix-A.5

tests/Component/Encryption/JWELoaderTestCase.php renamed to tests/Component/Encryption/JWELoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @internal
1616
*/
17-
final class JWELoaderTestCase extends EncryptionTestCase
17+
final class JWELoaderTest extends EncryptionTestCase
1818
{
1919
private ?JWELoader $jweLoader = null;
2020

0 commit comments

Comments
 (0)