Skip to content

Commit be7ebcd

Browse files
committed
Be lenient in what we accept, like dateTime/time with trailing sub-second zeros
1 parent 7520e73 commit be7ebcd

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

src/XMLSchema/Type/DateTimeValue.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
1212
use SimpleSAML\XMLSchema\Type\Interface\AbstractAnySimpleType;
1313

14+
use function rtrim;
15+
use function strlen;
16+
use function substr;
17+
1418
/**
1519
* @package simplesaml/xml-common
1620
*/
@@ -28,7 +32,28 @@ class DateTimeValue extends AbstractAnySimpleType
2832
*/
2933
protected function sanitizeValue(string $value): string
3034
{
31-
return static::collapseWhitespace(static::normalizeWhitespace($value));
35+
$normalized = static::collapseWhitespace(static::normalizeWhitespace($value));
36+
37+
// Trim any trailing zero's from the sub-seconds
38+
$decimal = strrpos($normalized, '.');
39+
if ($decimal !== false) {
40+
$timezone = strrpos($normalized, '+') ?? strrpos($normalized, '-') ?? strrpos($normalized, 'Z');
41+
if ($timezone !== false) {
42+
$subseconds = substr($normalized, $decimal + 1, strlen($normalized) - $timezone);
43+
} else {
44+
$subseconds = substr($normalized, $decimal + 1);
45+
}
46+
47+
$subseconds = rtrim($subseconds, '0');
48+
if ($subseconds === '') {
49+
return substr($normalized, 0, $decimal);
50+
}
51+
return substr($normalized, 0, $decimal + 1)
52+
. $subseconds
53+
. (($timezone === false) ? '' : substr($normalized, $timezone));
54+
}
55+
56+
return $normalized;
3257
}
3358

3459

src/XMLSchema/Type/TimeValue.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
99
use SimpleSAML\XMLSchema\Type\Interface\AbstractAnySimpleType;
1010

11+
use function rtrim;
12+
use function strlen;
13+
use function substr;
14+
1115
/**
1216
* @package simplesaml/xml-common
1317
*/
@@ -23,7 +27,28 @@ class TimeValue extends AbstractAnySimpleType
2327
*/
2428
protected function sanitizeValue(string $value): string
2529
{
26-
return static::collapseWhitespace(static::normalizeWhitespace($value));
30+
$normalized = static::collapseWhitespace(static::normalizeWhitespace($value));
31+
32+
// Trim any trailing zero's from the sub-seconds
33+
$decimal = strrpos($normalized, '.');
34+
if ($decimal !== false) {
35+
$timezone = strrpos($normalized, '+') ?? strrpos($normalized, '-') ?? strrpos($normalized, 'Z');
36+
if ($timezone !== false) {
37+
$subseconds = substr($normalized, $decimal + 1, strlen($normalized) - $timezone);
38+
} else {
39+
$subseconds = substr($normalized, $decimal + 1);
40+
}
41+
42+
$subseconds = rtrim($subseconds, '0');
43+
if ($subseconds === '') {
44+
return substr($normalized, 0, $decimal);
45+
}
46+
return substr($normalized, 0, $decimal + 1)
47+
. $subseconds
48+
. (($timezone === false) ? '' : substr($normalized, $timezone));
49+
}
50+
51+
return $normalized;
2752
}
2853

2954

tests/XMLSchema/Type/DateTimeValueTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public static function provideValidDateTime(): array
6161
{
6262
return [
6363
'whitespace collapse' => [true, ' 2001-10-26T21:32:52 '],
64+
'trailing sub-second zero' => [true, '2001-10-26T21:32:52.1230'],
65+
'all trailing sub-second zero' => [true, '2001-10-26T21:32:52.00'],
6466
];
6567
}
6668

tests/XMLSchema/Type/TimeValueTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public static function provideValidTime(): array
4747
{
4848
return [
4949
'whitespace collapse' => [true, "\n 21:32:52.12679\t "],
50+
'trailing sub-second zero' => [true, '21:32:52.1230'],
51+
'all trailing sub-second zero' => [true, '21:32:52.00'],
5052
];
5153
}
5254

0 commit comments

Comments
 (0)