Skip to content

Commit 61ec392

Browse files
committed
Test xs:string for valid characters according to the XML 1.1 specifications
1 parent 23e7422 commit 61ec392

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/XML/Assert/StringTrait.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,52 @@
44

55
namespace SimpleSAML\XML\Assert;
66

7+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
8+
79
/**
810
* @package simplesamlphp/xml-common
911
*/
1012
trait StringTrait
1113
{
14+
private static string $string_regex = '/^
15+
[
16+
\x09
17+
\x0A
18+
\x0D
19+
\x{20}-\x{7E}
20+
\x{85}
21+
\x{A0}-\x{D7FF}
22+
\x{E000}-\x{FDCF}
23+
\x{FDF0}-\x{FFFD}
24+
\x{10000}-\x{1FFFD}
25+
\x{20000}-\x{2FFFD}
26+
\x{30000}-\x{3FFFD}
27+
\x{40000}-\x{4FFFD}
28+
\x{50000}-\x{5FFFD}
29+
\x{60000}-\x{6FFFD}
30+
\x{70000}-\x{7FFFD}
31+
\x{80000}-\x{8FFFD}
32+
\x{90000}-\x{9FFFD}
33+
\x{A0000}-\x{AFFFD}
34+
\x{B0000}-\x{BFFFD}
35+
\x{C0000}-\x{CFFFD}
36+
\x{D0000}-\x{DFFFD}
37+
\x{E0000}-\x{EFFFD}
38+
\x{F0000}-\x{FFFFD}
39+
\x{100000}-\x{10FFFD}
40+
]*$/Dxu';
41+
42+
1243
/**
1344
* @param string $value
1445
* @param string $message
1546
*/
1647
protected static function validString(string $value, string $message = ''): void
1748
{
49+
Assert::regex(
50+
$value,
51+
self::$string_regex,
52+
SchemaViolationException::class,
53+
);
1854
}
1955
}

src/XMLSchema/Type/StringValue.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace SimpleSAML\XMLSchema\Type;
66

7+
use SimpleSAML\XML\Assert\Assert;
8+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
79
use SimpleSAML\XMLSchema\Type\Interface\AbstractAnySimpleType;
810

911
/**
@@ -12,4 +14,16 @@
1214
class StringValue extends AbstractAnySimpleType
1315
{
1416
public const string SCHEMA_TYPE = 'string';
17+
18+
19+
/**
20+
* Validate the value.
21+
*
22+
* @param string $value
23+
* @throws \SimpleSAML\XMLSchema\Exception\SchemaViolationException on failure
24+
*/
25+
protected function validateValue(string $value): void
26+
{
27+
Assert::validString($value, SchemaViolationException::class);
28+
}
1529
}

tests/XML/Assert/StringTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\Attributes\DataProvider;
99
use PHPUnit\Framework\TestCase;
10-
use SimpleSAML\Assert\AssertionFailedException;
1110
use SimpleSAML\XML\Assert\Assert;
11+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
12+
13+
use function chr;
1214

1315
/**
1416
* Class \SimpleSAML\Test\XML\Assert\StringTest
@@ -28,7 +30,7 @@ public function testString(bool $shouldPass, string $str): void
2830
try {
2931
Assert::validString($str);
3032
$this->assertTrue($shouldPass);
31-
} catch (AssertionFailedException $e) {
33+
} catch (SchemaViolationException $e) {
3234
$this->assertFalse($shouldPass);
3335
}
3436
}
@@ -42,6 +44,9 @@ public static function provideString(): array
4244
return [
4345
'preserve spaces' => [true, ' Snoopy '],
4446
'replace whitespace' => [true, " Snoopy\t\n\rrulez "],
47+
'html' => [true, "<em>SimpleSAMLphp</em>"],
48+
'unicode' => [true, 'ünïcöde €Φ汉'],
49+
'invalid character' => [false, "Valid text with " . chr(0) . " invalid null byte"],
4550
];
4651
}
4752
}

0 commit comments

Comments
 (0)