Skip to content

Commit 83f20c8

Browse files
committed
Break parentage of AssertionFailedException for SchemaViolationException
1 parent 633f23d commit 83f20c8

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/Assert/HMACOutputLengthTrait.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace SimpleSAML\XMLSecurity\Assert;
66

7-
use InvalidArgumentException;
7+
use SimpleSAML\XML\Exception\SchemaViolationException;
8+
use SimpleSAML\XMLSecurity\Exception\ProtocolViolationException;
9+
10+
use function intval;
811

912
/**
1013
* @package simplesamlphp/xml-security
@@ -27,11 +30,24 @@ trait HMACOutputLengthTrait
2730
*/
2831
protected static function validHMACOutputLength(string $value, string $message = ''): void
2932
{
30-
parent::regex(
31-
$value,
32-
self::$HMACOutputLength_regex,
33-
$message ?: '%s is not a valid ds:HMACOutputLengthType',
34-
InvalidArgumentException::class,
35-
);
33+
try {
34+
parent::regex(
35+
$value,
36+
self::$HMACOutputLength_regex,
37+
$message ?: '%s is not a valid ds:HMACOutputLengthType',
38+
);
39+
} catch (AssertionFailedException $e) {
40+
throw new SchemaViolationException($e->getMessage());
41+
}
42+
43+
try {
44+
parent::true(
45+
intval($value) % 8 === 0,
46+
'%s is not devisible by 8 and therefore not a valid ds:HMACOutputLengthType',
47+
);
48+
} catch (AssertionFailedException $e) {
49+
throw new ProtocolViolationException($e->getMessage());
50+
}
51+
3652
}
3753
}

src/Type/HMACOutputLengthValue.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,12 @@ class HMACOutputLengthValue extends IntegerValue
1919
*
2020
* @param string $value
2121
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
22+
* @throws \SimpleSAML\XMLSecurity\Exception\ProtocolViolationException when not devisible by 8
2223
* @return void
2324
*/
2425
protected function validateValue(string $value): void
2526
{
2627
// Note: value must already be sanitized before validating
27-
$value = $this->sanitizeValue($value);
28-
29-
Assert::validHMACOutputLength($value, SchemaViolationException::class);
30-
Assert::true(
31-
intval($value) % 8 === 0,
32-
'%s is not devisible by 8 and therefore not a valid ds:HMACOutputLengthType',
33-
ProtocolViolationException::class,
34-
);
28+
Assert::validHMACOutputLength($this->sanitizeValue($value));
3529
}
3630
}

tests/Assert/HMACOutputLengthTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPUnit\Framework\TestCase;
99
use SimpleSAML\Assert\AssertionFailedException;
1010
use SimpleSAML\XMLSecurity\Assert\Assert;
11+
use SimpleSAML\XMLSecurity\Exception\ProtocolViolationException;
1112

1213
/**
1314
* Class \SimpleSAML\Test\XMLSecurity\Assert\HMACOutputLengthTest
@@ -27,7 +28,7 @@ public function testValidHMACOutputLength(bool $shouldPass, string $HMACOutputLe
2728
try {
2829
Assert::validHMACOutputLength($HMACOutputLength);
2930
$this->assertTrue($shouldPass);
30-
} catch (AssertionFailedException $e) {
31+
} catch (AssertionFailedException|ProtocolViolationException $e) {
3132
$this->assertFalse($shouldPass);
3233
}
3334
}
@@ -41,8 +42,7 @@ public static function provideHMACOutputLength(): array
4142
return [
4243
'empty' => [false, ''],
4344
'valid positive integer' => [true, '128'],
44-
// Indivisible by 8 is caught by the type-class, because schema-wise it's perfectly valid
45-
'valid indivisible by 8' => [true, '4'],
45+
'invalid indivisible by 8' => [false, '4'],
4646
'invalid signed positive integer' => [false, '+128'],
4747
'invalid zero' => [false, '0'],
4848
'invalid leading zeros' => [false, '0000000000000000000128'],

0 commit comments

Comments
 (0)