Skip to content

Commit 29e0957

Browse files
fix: Sanitize URI regular expressions (OID & License Plate) (#9)
* fix: regex to catch OID longer than 24 caracteres * fix: license plate regex - License plate is considered valid if it has 7 or 8 numbers
1 parent 3d578e9 commit 29e0957

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/Support/Uri.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public static function sanitize(string $uri): string
1818
return preg_replace(
1919
[
2020
'/\/(?<=\/)([A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12})(?=\/)?/i',
21-
'/\/(?<=\/)([A-Z]{3}-?\d[0-9A-Z]\d{2})(?=\/)?/i',
22-
'/\/(?<=\/)[0-9A-F]{24}(?=\/)?/i',
21+
'/\/(?<=\/)((?=.{7,8}$)|(?=.{7,8}\/))([A-Z]{3}-?\d[0-9A-Z]\d{2})/i',
22+
'/\/(?<=\/)[0-9A-F]{24,}(?=\/)?/i',
2323
'/\/(?<=\/)\d+(?=\/)?/',
2424
],
2525
[

tests/UriTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function testSanitizeLicensePlatesStrings(): void
4949
self::assertSame('/v7/test/<LICENSE-PLATE>/<LICENSE-PLATE>/<LICENSE-PLATE>', Uri::sanitize('/v7/test/PET9349/PET9349/PET9349'));
5050
self::assertSame('/v8/test/<LICENSE-PLATE>/<LICENSE-PLATE>/<LICENSE-PLATE>/', Uri::sanitize('/v8/test/PET9D49/PET9D49/PET9D49/'));
5151
self::assertSame('/v8/test/<LICENSE-PLATE>/<LICENSE-PLATE>/<LICENSE-PLATE>/', Uri::sanitize('/v8/test/PET9349/PET9349/PET9349/'));
52+
self::assertSame('/v8/test/PET9349FOOBAR/foo/<LICENSE-PLATE>', Uri::sanitize('/v8/test/PET9349FOOBAR/foo/PET9349'));
5253
}
5354

5455
public function testClearUriUuids(): void
@@ -68,6 +69,7 @@ public function testClearUriUuids(): void
6869
public function testClearUriOids(): void
6970
{
7071
$oid = '650229807612bba4984d1fc7';
72+
$oidLonger = 'ddb21302b3c66b5111bb99a907f783e2a29947f0';
7173

7274
self::assertSame('/v1/test', Uri::sanitize('/v1/test'));
7375
self::assertSame('/v2/test/<OID>', Uri::sanitize("/v2/test/{$oid}"));
@@ -77,5 +79,7 @@ public function testClearUriOids(): void
7779
self::assertSame('/v6/test/<OID>/<OID>/', Uri::sanitize("/v6/test/{$oid}/{$oid}/"));
7880
self::assertSame('/v7/test/<OID>/<OID>/<OID>', Uri::sanitize("/v7/test/{$oid}/{$oid}/{$oid}"));
7981
self::assertSame('/v8/test/<OID>/<OID>/<OID>/', Uri::sanitize("/v8/test/{$oid}/{$oid}/{$oid}/"));
82+
self::assertSame('/v2/token/<OID>/foo/<OID>', Uri::sanitize("/v2/token/{$oidLonger}/foo/{$oid}"));
83+
self::assertSame('/v3/token/<OID>/foo/<OID>/bar', Uri::sanitize("/v3/token/$oidLonger/foo/{$oid}/bar"));
8084
}
8185
}

0 commit comments

Comments
 (0)