1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML\mdui; |
6
|
|
|
|
7
|
|
|
use SimpleSAML\Assert\Assert; |
8
|
|
|
use SimpleSAML\SAML2\Exception\InvalidArgumentException; |
9
|
|
|
use SimpleSAML\SAML2\Exception\ProtocolViolationException; |
10
|
|
|
use SimpleSAML\SAML2\XML\StringElementTrait; |
11
|
|
|
use SimpleSAML\XML\SchemaValidatableElementInterface; |
12
|
|
|
use SimpleSAML\XML\SchemaValidatableElementTrait; |
13
|
|
|
|
14
|
|
|
use function filter_var; |
15
|
|
|
use function preg_replace; |
16
|
|
|
use function rtrim; |
17
|
|
|
use function sprintf; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class implementing DomainHint. |
21
|
|
|
* |
22
|
|
|
* @package simplesamlphp/saml2 |
23
|
|
|
*/ |
24
|
|
|
final class DomainHint extends AbstractMduiElement implements SchemaValidatableElementInterface |
25
|
|
|
{ |
26
|
|
|
use SchemaValidatableElementTrait; |
27
|
|
|
use StringElementTrait; |
|
|
|
|
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $content |
32
|
|
|
*/ |
33
|
|
|
public function __construct(string $content) |
34
|
|
|
{ |
35
|
|
|
$this->setContent($content); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sanitize the content of the element. |
41
|
|
|
* |
42
|
|
|
* @param string $content The unsanitized textContent |
43
|
|
|
* @throws \Exception on failure |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
protected function sanitizeContent(string $content): string |
47
|
|
|
{ |
48
|
|
|
// Remove prefixed schema and/or trailing whitespace + forward slashes |
49
|
|
|
return rtrim(preg_replace('#^http[s]?://#i', '', $content), " \n\r\t\v\x00/"); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Validate the content of the element. |
55
|
|
|
* |
56
|
|
|
* @param string $content The value to go in the XML textContent |
57
|
|
|
* @throws \Exception on failure |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
protected function validateContent(string $content): void |
61
|
|
|
{ |
62
|
|
|
$sanitizedContent = $this->sanitizeContent($content); |
63
|
|
|
Assert::notWhitespaceOnly($sanitizedContent, ProtocolViolationException::class); |
64
|
|
|
|
65
|
|
|
if (!filter_var($sanitizedContent, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) { |
66
|
|
|
throw new InvalidArgumentException(sprintf('DomainHint is not a valid hostname; %s', $sanitizedContent)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|