Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert::uuid(...) false positives on UUIDs with trailing newlines #307

Open
Malik12tree opened this issue Nov 18, 2024 · 0 comments
Open

Comments

@Malik12tree
Copy link

Assert::uuid(...) returns false positives on UUIDs with trailing newlines

Test Cases:

Assert::uuid("c9df9150-6536-42e0-802a-266f7ccc189c");     // Valid as expected
Assert::uuid("c9df9150-6536-42e0-802a-266f7ccc189c\r");   // Invalid as expected
Assert::uuid("c9df9150-6536-42e0-802a-266f7ccc189c\r\n"); // Invalid as expected
Assert::uuid("c9df9150-6536-42e0-802a-266f7ccc189c\n");   // Currently Valid (incorrect)

Proposed Cause:

if (!\preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) {

The regular expression above checks if the test subject starts and ends with a UUID (by using ^ and $). However, the $ anchor not only anchors the end of the string, but also an immediate newline character. (as stated here)

Proposed Solution:

To prevent false positives, we can make use of the PCRE_DOLLAR_ENDONLY flag (D) that will constraint the $ anchor to match the end of the string only, not a new character.

- if (!\preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) { 
+ if (!\preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/D', $value)) { 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant