Skip to content

Commit 600e230

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

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/XML/Assert/StringTrait.php

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

55
namespace SimpleSAML\XML\Assert;
66

7+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
8+
79
/**
810
* @package simplesamlphp/xml-common
911
*/
@@ -15,5 +17,10 @@ trait StringTrait
1517
*/
1618
protected static function validString(string $value, string $message = ''): void
1719
{
20+
Assert::regex(
21+
$value,
22+
'/^[\x09\x0A\x0D\x{20}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]*$/u',
23+
SchemaViolationException::class,
24+
);
1825
}
1926
}

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use PHPUnit\Framework\TestCase;
1010
use SimpleSAML\Assert\AssertionFailedException;
1111
use SimpleSAML\XML\Assert\Assert;
12+
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
13+
14+
use function chr;
1215

1316
/**
1417
* Class \SimpleSAML\Test\XML\Assert\StringTest
@@ -28,7 +31,7 @@ public function testString(bool $shouldPass, string $str): void
2831
try {
2932
Assert::validString($str);
3033
$this->assertTrue($shouldPass);
31-
} catch (AssertionFailedException $e) {
34+
} catch (AssertionFailedException|SchemaViolationException $e) {
3235
$this->assertFalse($shouldPass);
3336
}
3437
}
@@ -42,6 +45,9 @@ public static function provideString(): array
4245
return [
4346
'preserve spaces' => [true, ' Snoopy '],
4447
'replace whitespace' => [true, " Snoopy\t\n\rrulez "],
48+
'html' => [true, "<em>SimpleSAMLphp</em>"],
49+
'unicode' => [true, 'ünïcöde €Φ汉'],
50+
'invalid character' => [false, "Valid text with " . chr(0) . " invalid null byte"],
4551
];
4652
}
4753
}

0 commit comments

Comments
 (0)