Skip to content

Commit 6ab26da

Browse files
committed
Use local versions of StringElementTrait and URIElementTrait to comply with stricter SAML 2.0 specs
1 parent 92085db commit 6ab26da

34 files changed

Lines changed: 158 additions & 553 deletions

src/Assert/CustomAssertionTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ private static function validURI(string $value, string $message = ''): void
6363
}
6464

6565
try {
66-
BaseAssert::notWhitespaceOnly($value, $message ?: '%s is not a SAML2-compliant URI');
67-
6866
// If it doesn't have a scheme, it's not an absolute URI
6967
BaseAssert::regex($value, self::$scheme_regex, $message ?: '%s is not a SAML2-compliant URI');
7068
} catch (AssertionFailedException $e) {
@@ -82,6 +80,7 @@ private static function validEntityID(string $value, string $message = ''): void
8280
static::validURI($value);
8381

8482
try {
83+
BaseAssert::notWhitespaceOnly($value);
8584
BaseAssert::maxLength(
8685
$value,
8786
C::ENTITYID_MAX_LENGTH,

src/XML/StringElementTrait.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\SAML2\XML;
6+
7+
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
9+
use SimpleSAML\XML\StringElementTrait as BaseStringElementTrait;
10+
11+
/**
12+
* Trait extending the default StringElementTrait to comply with the restrictions added by the SAML 2.0 specifications.
13+
*
14+
* @package simplesamlphp/saml2
15+
*/
16+
trait StringElementTrait
17+
{
18+
use BaseStringElementTrait;
19+
20+
/**
21+
* Validate the content of the element.
22+
*
23+
* @param string $content The value to go in the XML textContent
24+
* @throws \Exception on failure
25+
* @return void
26+
*/
27+
protected function validateContent(/** @scrutinizer ignore-unused */ string $content): void
28+
{
29+
/**
30+
* 1.3.1 String Values
31+
*
32+
* All SAML string values have the type xs:string, which is built in to the W3C XML Schema Datatypes
33+
* specification [Schema2]. Unless otherwise noted in this specification or particular profiles, all strings in
34+
* SAML messages MUST consist of at least one non-whitespace character (whitespace is defined in the
35+
* XML Recommendation [XML] Section 2.3).
36+
*/
37+
Assert::notWhitespaceOnly($content, ProtocolViolationException::class);
38+
}
39+
}

src/XML/URIElementTrait.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\SAML2\XML;
6+
7+
use SimpleSAML\Assert\Assert;
8+
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
9+
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
10+
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
use SimpleSAML\XML\URIElementTrait as BaseURIElementTrait;
12+
13+
/**
14+
* Trait extending the default URIElementTrait to comply with the restrictions added by the SAML 2.0 specifications.
15+
*
16+
* @package simplesamlphp/saml2
17+
*/
18+
trait URIElementTrait
19+
{
20+
use BaseURIElementTrait;
21+
22+
/**
23+
* Validate the content of the element.
24+
*
25+
* @param string $content The value to go in the XML textContent
26+
* @throws \Exception on failure
27+
* @return void
28+
*/
29+
protected function validateContent(string $content): void
30+
{
31+
/**
32+
* 1.3.2 URI Values
33+
*
34+
* Unless otherwise indicated in this specification, all URI reference values used within SAML-defined
35+
* elements or attributes MUST consist of at least one non-whitespace character, and are REQUIRED to be
36+
* absolute [RFC 2396].
37+
*/
38+
Assert::notWhitespaceOnly($content, ProtocolViolationException::class);
39+
SAMLAssert::validURI($content, SchemaViolationException::class);
40+
}
41+
}

src/XML/ecp/RelayState.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
10+
use SimpleSAML\SAML2\XML\StringElementTrait;
1011
use SimpleSAML\SOAP\Constants as C;
1112
use SimpleSAML\XML\Exception\InvalidDOMElementException;
1213
use SimpleSAML\XML\Exception\MissingAttributeException;
1314
use SimpleSAML\XML\SchemaValidatableElementInterface;
1415
use SimpleSAML\XML\SchemaValidatableElementTrait;
15-
use SimpleSAML\XML\StringElementTrait;
1616

1717
/**
1818
* Class representing the ECP RelayState element.

src/XML/emd/RepublishTarget.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
namespace SimpleSAML\SAML2\XML\emd;
66

77
use SimpleSAML\Assert\Assert;
8-
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
9-
use SimpleSAML\XML\StringElementTrait;
8+
use SimpleSAML\SAML2\XML\URIElementTrait;
109

1110
/**
1211
* Class implementing RepublishTarget.
@@ -15,7 +14,9 @@
1514
*/
1615
final class RepublishTarget extends AbstractEmdElement
1716
{
18-
use StringElementTrait;
17+
use URIElementTrait {
18+
URIElementTrait::validateContent as baseValidateContent;
19+
}
1920

2021

2122
/**
@@ -36,7 +37,7 @@ public function __construct(string $content)
3637
*/
3738
protected function validateContent(string $content): void
3839
{
39-
SAMLAssert::validURI($content);
40+
$this->baseValidateContent($content);
4041
Assert::same($content, 'http://edugain.org/');
4142
}
4243
}

src/XML/md/AbstractLocalizedName.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\SAML2\Exception\ArrayValidationException;
10+
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11+
use SimpleSAML\SAML2\XML\StringElementTrait;
1012
use SimpleSAML\XML\ArrayizableElementInterface;
1113
use SimpleSAML\XML\Constants as C;
1214
use SimpleSAML\XML\Exception\InvalidDOMElementException;
1315
use SimpleSAML\XML\Exception\MissingAttributeException;
14-
use SimpleSAML\XML\StringElementTrait;
1516

1617
use function array_key_first;
1718

@@ -35,25 +36,12 @@ final public function __construct(
3536
protected string $language,
3637
string $value,
3738
) {
38-
Assert::notEmpty($language, 'xml:lang cannot be empty.');
39+
Assert::notWhitespaceOnly($language, ProtocolViolationException::class);
3940

4041
$this->setContent($value);
4142
}
4243

4344

44-
/**
45-
* Validate the content of the element.
46-
*
47-
* @param string $content The value to go in the XML textContent
48-
* @throws \Exception on failure
49-
* @return void
50-
*/
51-
protected function validateContent(string $content): void
52-
{
53-
Assert::notEmpty($content);
54-
}
55-
56-
5745
/**
5846
* Get the language this string is localized in.
5947
*

src/XML/md/AdditionalMetadataLocation.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
use DOMElement;
88
use SimpleSAML\Assert\Assert;
99
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
10+
use SimpleSAML\SAML2\XML\URIElementTrait;
1011
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11-
use SimpleSAML\XML\Exception\SchemaViolationException;
1212
use SimpleSAML\XML\SchemaValidatableElementInterface;
1313
use SimpleSAML\XML\SchemaValidatableElementTrait;
14-
use SimpleSAML\XML\StringElementTrait;
1514

1615
use function trim;
1716

@@ -23,7 +22,7 @@
2322
final class AdditionalMetadataLocation extends AbstractMdElement implements SchemaValidatableElementInterface
2423
{
2524
use SchemaValidatableElementTrait;
26-
use StringElementTrait;
25+
use URIElementTrait;
2726

2827

2928
/**
@@ -52,19 +51,6 @@ public function getNamespace(): string
5251
}
5352

5453

55-
/**
56-
* Validate the content of the element.
57-
*
58-
* @param string $content The value to go in the XML textContent
59-
* @throws \Exception on failure
60-
* @return void
61-
*/
62-
protected function validateContent(string $content): void
63-
{
64-
SAMLAssert::validURI($content, SchemaViolationException::class); // Covers the empty string
65-
}
66-
67-
6854
/**
6955
* Initialize an AdditionalMetadataLocation element.
7056
*

src/XML/md/AffiliateMember.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace SimpleSAML\SAML2\XML\md;
66

77
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
8+
use SimpleSAML\SAML2\XML\StringElementTrait;
89
use SimpleSAML\XML\SchemaValidatableElementInterface;
910
use SimpleSAML\XML\SchemaValidatableElementTrait;
10-
use SimpleSAML\XML\StringElementTrait;
1111

1212
/**
1313
* Class implementing AffiliateMember.

src/XML/md/AttributeProfile.php

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44

55
namespace SimpleSAML\SAML2\XML\md;
66

7-
use DOMElement;
8-
use SimpleSAML\Assert\Assert;
9-
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
10-
use SimpleSAML\XML\Exception\InvalidDOMElementException;
7+
use SimpleSAML\SAML2\XML\StringElementTrait;
118
use SimpleSAML\XML\SchemaValidatableElementInterface;
129
use SimpleSAML\XML\SchemaValidatableElementTrait;
13-
use SimpleSAML\XML\StringElementTrait;
1410

1511
/**
1612
* Class implementing AttributeProfile.
@@ -30,35 +26,4 @@ public function __construct(string $content)
3026
{
3127
$this->setContent($content);
3228
}
33-
34-
35-
/**
36-
* Validate the content of the element.
37-
*
38-
* @param string $content The value to go in the XML textContent
39-
* @throws \Exception on failure
40-
* @return void
41-
*/
42-
protected function validateContent(string $content): void
43-
{
44-
SAMLAssert::validURI($content);
45-
}
46-
47-
48-
/**
49-
* Convert XML into a AttributeProfile
50-
*
51-
* @param \DOMElement $xml The XML element we should load
52-
* @return static
53-
*
54-
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
55-
* If the qualified name of the supplied element is wrong
56-
*/
57-
public static function fromXML(DOMElement $xml): static
58-
{
59-
Assert::same($xml->localName, 'AttributeProfile', InvalidDOMElementException::class);
60-
Assert::same($xml->namespaceURI, AttributeProfile::NS, InvalidDOMElementException::class);
61-
62-
return new static($xml->textContent);
63-
}
6429
}

src/XML/md/Company.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
namespace SimpleSAML\SAML2\XML\md;
66

7-
use SimpleSAML\Assert\Assert;
7+
use SimpleSAML\SAML2\XML\StringElementTrait;
88
use SimpleSAML\XML\SchemaValidatableElementInterface;
99
use SimpleSAML\XML\SchemaValidatableElementTrait;
10-
use SimpleSAML\XML\StringElementTrait;
1110

1211
/**
1312
* Class implementing Company.
@@ -27,17 +26,4 @@ public function __construct(string $content)
2726
{
2827
$this->setContent($content);
2928
}
30-
31-
32-
/**
33-
* Validate the content of the element.
34-
*
35-
* @param string $content The value to go in the XML textContent
36-
* @throws \Exception on failure
37-
* @return void
38-
*/
39-
protected function validateContent(string $content): void
40-
{
41-
Assert::notEmpty($content, 'Company cannot be empty');
42-
}
4329
}

0 commit comments

Comments
 (0)