Skip to content

Commit

Permalink
Break parentage of AssertionFailedException for SchemaViolationException
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 18, 2025
1 parent 633f23d commit 83f20c8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
30 changes: 23 additions & 7 deletions src/Assert/HMACOutputLengthTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace SimpleSAML\XMLSecurity\Assert;

use InvalidArgumentException;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XMLSecurity\Exception\ProtocolViolationException;

use function intval;

/**
* @package simplesamlphp/xml-security
Expand All @@ -27,11 +30,24 @@ trait HMACOutputLengthTrait
*/
protected static function validHMACOutputLength(string $value, string $message = ''): void
{
parent::regex(
$value,
self::$HMACOutputLength_regex,
$message ?: '%s is not a valid ds:HMACOutputLengthType',
InvalidArgumentException::class,
);
try {
parent::regex(
$value,
self::$HMACOutputLength_regex,
$message ?: '%s is not a valid ds:HMACOutputLengthType',
);
} catch (AssertionFailedException $e) {
throw new SchemaViolationException($e->getMessage());
}

try {
parent::true(
intval($value) % 8 === 0,
'%s is not devisible by 8 and therefore not a valid ds:HMACOutputLengthType',
);
} catch (AssertionFailedException $e) {
throw new ProtocolViolationException($e->getMessage());
}

}
}
10 changes: 2 additions & 8 deletions src/Type/HMACOutputLengthValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,12 @@ class HMACOutputLengthValue extends IntegerValue
*
* @param string $value
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
* @throws \SimpleSAML\XMLSecurity\Exception\ProtocolViolationException when not devisible by 8
* @return void
*/
protected function validateValue(string $value): void
{
// Note: value must already be sanitized before validating
$value = $this->sanitizeValue($value);

Assert::validHMACOutputLength($value, SchemaViolationException::class);
Assert::true(
intval($value) % 8 === 0,
'%s is not devisible by 8 and therefore not a valid ds:HMACOutputLengthType',
ProtocolViolationException::class,
);
Assert::validHMACOutputLength($this->sanitizeValue($value));
}
}
6 changes: 3 additions & 3 deletions tests/Assert/HMACOutputLengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\XMLSecurity\Assert\Assert;
use SimpleSAML\XMLSecurity\Exception\ProtocolViolationException;

/**
* Class \SimpleSAML\Test\XMLSecurity\Assert\HMACOutputLengthTest
Expand All @@ -27,7 +28,7 @@ public function testValidHMACOutputLength(bool $shouldPass, string $HMACOutputLe
try {
Assert::validHMACOutputLength($HMACOutputLength);
$this->assertTrue($shouldPass);
} catch (AssertionFailedException $e) {
} catch (AssertionFailedException|ProtocolViolationException $e) {
$this->assertFalse($shouldPass);
}
}
Expand All @@ -41,8 +42,7 @@ public static function provideHMACOutputLength(): array
return [
'empty' => [false, ''],
'valid positive integer' => [true, '128'],
// Indivisible by 8 is caught by the type-class, because schema-wise it's perfectly valid
'valid indivisible by 8' => [true, '4'],
'invalid indivisible by 8' => [false, '4'],
'invalid signed positive integer' => [false, '+128'],
'invalid zero' => [false, '0'],
'invalid leading zeros' => [false, '0000000000000000000128'],
Expand Down

0 comments on commit 83f20c8

Please sign in to comment.