Skip to content

Commit 3e1e418

Browse files
committed
trim CIDR-values before validating them & increase coverage
1 parent 9b1b49e commit 3e1e418

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/Type/CIDRValue.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111
*/
1212
class CIDRValue extends SAMLStringValue
1313
{
14+
/**
15+
* Sanitize the content of the element.
16+
*
17+
* @param string $value The unsanitized value
18+
* @throws \Exception on failure
19+
* @return string
20+
*/
21+
protected function sanitizeValue(string $value): string
22+
{
23+
return static::collapseWhitespace(static::normalizeWhitespace($value));
24+
}
25+
26+
1427
/**
1528
* Validate the content of the element.
1629
*

tests/SAML2/Type/CIDRValueTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\SAML2\Type;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\Attributes\Group;
10+
use PHPUnit\Framework\TestCase;
11+
use SimpleSAML\Assert\AssertionFailedException;
12+
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
13+
use SimpleSAML\SAML2\Type\CIDRValue;
14+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
15+
16+
/**
17+
* Class \SimpleSAML\Test\SAML2\Type\CIDRValueTest
18+
*
19+
* @package simplesamlphp/saml2
20+
*/
21+
#[Group('type')]
22+
#[CoversClass(CIDRValue::class)]
23+
final class CIDRValueTest extends TestCase
24+
{
25+
/**
26+
* @param boolean $shouldPass
27+
* @param string $cidr
28+
*/
29+
#[DataProvider('provideCIDR')]
30+
public function testCIDR(bool $shouldPass, string $cidr): void
31+
{
32+
try {
33+
CIDRValue::fromString($cidr);
34+
$this->assertTrue($shouldPass);
35+
} catch (AssertionFailedException | ProtocolViolationException | SchemaViolationException $e) {
36+
$this->assertFalse($shouldPass);
37+
}
38+
}
39+
40+
41+
/**
42+
* @return array<string, array{0: bool, 1: string}>
43+
*/
44+
public static function provideCIDR(): array
45+
{
46+
return [
47+
'ipv4' => [true, '192.168.0.1/32'],
48+
'ipv6' => [true, '2001:0000:130F:0000:0000:09C0:876A:130B/128'],
49+
'ipv4 too long' => [false, '192.168.0.1.5/32'],
50+
'whitespace suffix' => [true, '192.168.1.5/32 '],
51+
'whitespace prefix' => [true, ' 192.168.1.5/32'],
52+
'whitespace center' => [false, '192.168. 1.5/32'],
53+
'ipv6 too long' => [false, '2001:0000:130F:0000:0000:09C0:876A:130B:130F:805B/128'],
54+
'ipv6 mixed notation' => [false, '805B:2D9D:DC28::FC57:212.200.31.255'],
55+
'ipv6 shortened notation' => [false, '::ffff:192.1.56.10/96'],
56+
'ipv6 compressed notation' => [false, '::212.200.31.255'],
57+
'ipv4 without length' => [false, '192.168.0.1'],
58+
'ipv6 wihtout length' => [false, '2001:0000:130F:0000:0000:09C0:876A:130B'],
59+
'ipv4 out of bounds length' => [false, '192.168.0.1/33'],
60+
'ipv6 out of bounds length' => [false, '2001:0000:130F:0000:0000:09C0:876A:130B/129'],
61+
'ipv4 out of bounds address' => [false, '256.168.0.1/32'],
62+
'ipv6 out of bounds address' => [false, '2001:0000:130G:0000:0000:09C0:876A:130B/128'],
63+
];
64+
}
65+
}

0 commit comments

Comments
 (0)