Skip to content

Commit 3e43126

Browse files
authored
Replace custom URI/URL validation with simplesamlphp/assert (#1951)
* refactor: use simplesamlphp assert for URI and URL validation * refactor: remove unused parse() method from EngineBlock_Validator_Uri
1 parent b8d8b16 commit 3e43126

6 files changed

Lines changed: 108 additions & 40 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"pimple/pimple": "^3.6",
3737
"ramsey/uuid": "^4.9.1",
3838
"robrichards/xmlseclibs": "^3.1.3",
39+
"simplesamlphp/assert": "^1.9.1",
3940
"simplesamlphp/saml2": "^4.19",
4041
"symfony/asset": "^7.4",
4142
"symfony/console": "^7.4",

composer.lock

Lines changed: 60 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

library/EngineBlock/Attributes/Validator/Type.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
* limitations under the License.
1717
*/
1818

19+
use SimpleSAML\Assert\Assert;
20+
use SimpleSAML\Assert\AssertionFailedException;
21+
1922
class EngineBlock_Attributes_Validator_Type extends EngineBlock_Attributes_Validator_Abstract
2023
{
2124
const ERROR_ATTRIBUTE_VALIDATOR_URI = 'error_attribute_validator_type_uri';
@@ -75,7 +78,9 @@ public function validate(array $attributes)
7578

7679
case 'URL':
7780
foreach ($attributeValues as $attributeValue) {
78-
if (filter_var($attributeValue, FILTER_VALIDATE_URL) === false) {
81+
try {
82+
Assert::validURL($attributeValue);
83+
} catch (AssertionFailedException $e) {
7984
$this->_messages[] = array(
8085
self::ERROR_ATTRIBUTE_VALIDATOR_URL,
8186
$this->_attributeName,

library/EngineBlock/Validator/Uri.php

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,26 @@
1616
* limitations under the License.
1717
*/
1818

19+
use SimpleSAML\Assert\Assert;
20+
use SimpleSAML\Assert\AssertionFailedException;
21+
1922
/**
20-
* Validate URIs according to RFC-3986.
21-
*
22-
* See: http://www.rfc-editor.org/errata_search.php?rfc=3986
23-
*
24-
* Note that this is a VERY permissive regex
23+
* Validate URIs using simplesamlphp/assert.
2524
*/
2625
class EngineBlock_Validator_Uri
2726
{
28-
const REGEX = '/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/';
29-
3027
/**
3128
* @param string $string
3229
* @return bool
3330
*/
3431
public function validate($uri)
3532
{
36-
return (bool) preg_match(self::REGEX, $uri);
37-
}
38-
39-
/**
40-
* Parses the given uri with the regex, this is useful for debugging
41-
*
42-
* @param string $uri
43-
* @return array
44-
*/
45-
public static function parse($uri)
46-
{
47-
preg_match(self::REGEX, $uri, $matches);
48-
49-
$keys = ['match'];
50-
$keys[] = 'scheme+separator';
51-
$keys[] = 'scheme';
52-
$keys[] = 'host+separator';
53-
$keys[] = 'host';
54-
$keys[] = 'path';
55-
$keys[] = 'query+separator';
56-
$keys[] = 'query';
57-
$keys[] = 'anchor+separator';
58-
$keys[] = 'anchor';
33+
try {
34+
Assert::validURI($uri);
35+
} catch (AssertionFailedException $e) {
36+
return false;
37+
}
5938

60-
$keysMatched = array_slice($keys, 0, count($matches));
61-
return array_combine($keysMatched, $matches);
39+
return true;
6240
}
6341
}

tests/library/EngineBlock/Test/Attributes/Validator/TypeTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
use PHPUnit\Framework\Attributes\DataProvider;
1920
use PHPUnit\Framework\TestCase;
2021

2122
class EngineBlock_Test_TypeTest extends TestCase
@@ -26,13 +27,22 @@ class EngineBlock_Test_TypeTest extends TestCase
2627
* @param $options
2728
* @param $attributes
2829
*/
29-
#[\PHPUnit\Framework\Attributes\DataProvider('validAttributesProvider')]
30+
#[DataProvider('validAttributesProvider')]
3031
public function testAttributeValidates($attributeName, $options, $attributes)
3132
{
3233
$validator = new EngineBlock_Attributes_Validator_Type($attributeName, $options);
3334
$this->assertTrue($validator->validate($attributes));
3435
}
3536

37+
#[DataProvider('invalidAttributesProvider')]
38+
public function testAttributeValidationFails($attributeName, $options, $attributes, $expectedMessage)
39+
{
40+
$validator = new EngineBlock_Attributes_Validator_Type($attributeName, $options);
41+
42+
$this->assertFalse($validator->validate($attributes));
43+
$this->assertSame([$expectedMessage, $attributeName, $options, $attributes[$attributeName][0]], $validator->getMessages()[0]);
44+
}
45+
3646
public static function validAttributesProvider()
3747
{
3848
return array(
@@ -59,7 +69,8 @@ public static function validAttributesProvider()
5969
'options' => 'URI',
6070
'attributes' => array(
6171
'foo' => array(
62-
'?'
72+
'?',
73+
'urn:mace:dir:entitlement:common-lib-terms',
6374
)
6475
)
6576
),
@@ -78,4 +89,20 @@ public static function validAttributesProvider()
7889
)
7990
);
8091
}
92+
93+
public static function invalidAttributesProvider()
94+
{
95+
return array(
96+
array(
97+
'attributeName' => 'foo',
98+
'options' => 'URL',
99+
'attributes' => array(
100+
'foo' => array(
101+
'mailto:test@example.org',
102+
)
103+
),
104+
'expectedMessage' => EngineBlock_Attributes_Validator_Type::ERROR_ATTRIBUTE_VALIDATOR_URL,
105+
),
106+
);
107+
}
81108
}

tests/library/EngineBlock/Test/Validator/UriTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
2020
use PHPUnit\Framework\TestCase;
2121

22-
/**
23-
* @todo write test which tests failing...this validator is so permissive it is VERY hard to let it fail
24-
*/
2522
class EngineBlock_Test_Validator_UriTest extends TestCase
2623
{
2724
use MockeryPHPUnitIntegration;
@@ -46,7 +43,8 @@ public static function validUriProvider()
4643
{
4744
return array(
4845
array('http://example.com'), // Pretty standard http url
49-
array('urn:mace:dir:entitlement:common-lib-terms') // Saml entitlement
46+
array('urn:mace:dir:entitlement:common-lib-terms'), // Saml entitlement
47+
array('?'),
5048
);
5149
}
5250
}

0 commit comments

Comments
 (0)