Skip to content

Commit 430c152

Browse files
authored
Merge pull request #16 from simplesamlphp/bugfix/regexp-not-ending-with-newline
Make regexps more strict and refuse trailing newlines
2 parents d880f20 + 2b2818d commit 430c152

File tree

6 files changed

+16
-9
lines changed

6 files changed

+16
-9
lines changed

src/CustomAssertionTrait.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,26 @@
2323
trait CustomAssertionTrait
2424
{
2525
/** @var string */
26-
private static string $nmtoken_regex = '/^[\w.:-]+$/u';
26+
private static string $nmtoken_regex = '/^[\w.:-]+$/Du';
2727

2828
/** @var string */
29-
private static string $nmtokens_regex = '/^([\w.:-]+)([\s][\w.:-]+)*$/u';
29+
private static string $nmtokens_regex = '/^([\w.:-]+)([\s][\w.:-]+)*$/Du';
3030

3131
/** @var string */
32-
private static string $datetime_regex = '/-?[0-9]{4}-(((0(1|3|5|7|8)|1(0|2))-(0[1-9]|(1|2)[0-9]|3[0-1]))|((0(4|6|9)|11)-(0[1-9]|(1|2)[0-9]|30))|(02-(0[1-9]|(1|2)[0-9])))T([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])(\.[0-999])?((\+|-)([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9])|Z)?/i';
32+
private static string $datetime_regex = '/-?[0-9]{4}-(((0(1|3|5|7|8)|1(0|2))-(0[1-9]|(1|2)[0-9]|3[0-1]))|((0(4|6|9)|11)-(0[1-9]|(1|2)[0-9]|30))|(02-(0[1-9]|(1|2)[0-9])))T([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])(\.[0-999])?((\+|-)([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9])|Z)?/Di';
3333

3434
/** @var string */
35-
private static string $duration_regex = '/^([-+]?)P(?!$)(?:(?<years>\d+(?:[\.\,]\d+)?)Y)?(?:(?<months>\d+(?:[\.\,]\d+)?)M)?(?:(?<weeks>\d+(?:[\.\,]\d+)?)W)?(?:(?<days>\d+(?:[\.\,]\d+)?)D)?(T(?=\d)(?:(?<hours>\d+(?:[\.\,]\d+)?)H)?(?:(?<minutes>\d+(?:[\.\,]\d+)?)M)?(?:(?<seconds>\d+(?:[\.\,]\d+)?)S)?)?$/';
35+
private static string $duration_regex = '/^([-+]?)P(?!$)(?:(?<years>\d+(?:[\.\,]\d+)?)Y)?(?:(?<months>\d+(?:[\.\,]\d+)?)M)?(?:(?<weeks>\d+(?:[\.\,]\d+)?)W)?(?:(?<days>\d+(?:[\.\,]\d+)?)D)?(T(?=\d)(?:(?<hours>\d+(?:[\.\,]\d+)?)H)?(?:(?<minutes>\d+(?:[\.\,]\d+)?)M)?(?:(?<seconds>\d+(?:[\.\,]\d+)?)S)?)?$/D';
3636

3737
/** @var string */
38-
private static string $qname_regex = '/^[a-zA-Z_][\w.-]*:[a-zA-Z_][\w.-]*$/';
38+
private static string $qname_regex = '/^[a-zA-Z_][\w.-]*:[a-zA-Z_][\w.-]*$/D';
3939

4040
/** @var string */
41-
private static string $ncname_regex = '/^[a-zA-Z_][\w.-]*$/';
41+
private static string $ncname_regex = '/^[a-zA-Z_][\w.-]*$/D';
4242

4343
/** @var string */
4444
private static string $base64_regex = '/^(?:[a-z0-9+\/]{4})*(?:[a-z0-9+\/]{2}==|[a-z0-9+\/]{3}=)?$/i';
4545

46-
/** @var string */
47-
private static string $hostname_regex = '/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/';
48-
4946
/***********************************************************************************
5047
* NOTE: Custom assertions may be added below this line. *
5148
* They SHOULD be marked as `private` to ensure the call is forced *

tests/Assert/DurationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public static function provideDuration(): array
5959
[false, 'P2M1Y'],
6060
[false, 'P'],
6161
[false, 'PT15.S'],
62+
// Trailing newlines are forbidden
63+
[false, "P20M\n"],
6264
];
6365
}
6466
}

tests/Assert/NCNameTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public static function provideNCName(): array
4646
[false, 'Te*st'],
4747
[false, '1Test'],
4848
[false, 'Te:st'],
49+
// Trailing newlines are forbidden
50+
[false, "Test\n"],
4951
];
5052
}
5153
}

tests/Assert/NMTokenTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public static function provideNMToken(): array
4949
[false, 'foo bar'],
5050
// Commas are forbidden
5151
[false, 'foo,bar'],
52+
// Trailing newlines are forbidden
53+
[false, "foobar\n"],
5254
];
5355
}
5456
}

tests/Assert/NMTokensTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public static function provideNMTokens(): array
5050
[false, 'foo "bar" baz'],
5151
// Commas are forbidden
5252
[false, 'foo,bar'],
53+
// Trailing newlines are forbidden
54+
[false, "foobar\n"],
5355
];
5456
}
5557
}

tests/Assert/QNameTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public static function provideQName(): array
4646
[true, 'Test'],
4747
[false, '1Test'],
4848
[false, 'Te*st'],
49+
// Trailing newlines are forbidden
50+
[false, "some:Test\n"],
4951
];
5052
}
5153
}

0 commit comments

Comments
 (0)