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

Fixed a bug where a newline was treated as a valid value even if it was included at the end. #4094

Conversation

ytetsuro
Copy link
Contributor

@ytetsuro ytetsuro commented Jan 11, 2021

Description
I want it to be false when

<?php
$validation->check("0\n", 'decimal');

However, the regular expression validation implemented in CodeIgniter4 allowed a trailing newline code.

Checklist:

  • Securely signed commits
  • Component(s) with PHPdocs
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@MGatner
Copy link
Member

MGatner commented Jan 11, 2021

It took me a minute but I think I understand this now. Basically, because we used ^ and $ to check "beginning/end of line" the newlines were not included in the validation. Switching to \A and \z (https://www.php.net/manual/en/regexp.reference.anchors.php) matches based on the entire string which will handle newline as a character to be validated as well.

Would implementing the PCRE_MULTILINE modifier (https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php) be a better solution? I'm not great with regex.

@MGatner
Copy link
Member

MGatner commented Jan 11, 2021

@ytetsuro Please squash this to a single commit. Alternatively, whoever merges it please be sure to squash.

test: Add a test case where the last string is a newline code when alpha_numeric_punct rule.

fix: treated as false if there is a newline at the end.

test: Add a test case where the last string is a newline code when alpha_numeric_space rule.

fix: treated as false if there is a newline at the end.

test: Add a test case where the last string is a newline code when decimal rule.

fix: treated as false if there is a newline at the end.

fix: treated as false if there is a newline at the end for alpha_numeric_space.

test: Add a test case where the last string is a newline code when numeric rule.

fix: treated as false if there is a newline at the end for numeric rule.

test: Add a test case where the last string is a newline code when decimal rule.

fix: treated as false if there is a newline at the end for alpha_dash rule.

test: Add a test case where the last string is a newline code when integer rule.

fix: treated as false if there is a newline at the end for integer rule.

fix: typo.
@ytetsuro ytetsuro force-pushed the fix/NewlinesAtTheEndAreIncludInThePatternMatch branch from 5bbb713 to 108a73f Compare January 11, 2021 14:32
@ytetsuro
Copy link
Contributor Author

Please squash this to a single commit.

fixed.

@ytetsuro
Copy link
Contributor Author

better solution?

It is I think a bad solution.
If you enable PCRE_MULTILINE modifier, it will be enabled when following.

<?php
$foo = <<<EOT
0123
234
567
EOT;
preg_match('/^[0-9]+$/m', $foo); // true

@MGatner
Copy link
Member

MGatner commented Jan 11, 2021

Gotcha, thanks for clarifying! I'm good with this solution then, we'll see if anyone with more regex experience else weighs in.

@@ -44,7 +44,7 @@ public function alpha_space(?string $value = null): bool
return true;
}

return (bool) preg_match('/^[A-Z ]+$/i', $value);
return (bool) preg_match('/\A[A-Z ]+\z/i', $value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add @see https://regex101.com/ link comment so it will be easier to verify in the future if bug/improvement needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samsonasik

please add @see https://regex101.com/ link comment so it will be easier to verify in the future if bug/improvement needed

Do we write this link in the code where all the regular expressions are written?
I don't see the need to add a link.
I think the attached URL is maintained by an individual, but how long will it be valid?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check these:

// See https://regex101.com/r/1GIHTa/1

// See https://regex101.com/r/BCBBKB/1

* @see https://regex101.com/r/OtFn8I/1

Yes, not everwhere yet, but I think it will be better to add it whenever possible for better future.

I've no information about how long it will be valid, but it seems it long enough ,for example, this is from 5 years ago https://regex101.com/library/wL5mP7 .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this link really be easy to maintain?
Every time a regular expression is edited, does the URL also need to be maintained, and will all the maintainers review and manage it?

The system also has the ability to delete the URLs that are created.
After the merge, if the link is deleted, the information will be lost.
I think test code is more constructive than a link that may or may not work.

What do other MEMBERS think about this?
I don't want to add this URL as my commit, but I will reluctantly add it if many members agree with this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vote for adding the link.
As it is positioned directly above the code I would think it is implicit to maintain it (or at least see what it does) when changing the regex itself.
Also, while of course the link might point to a dead end some future day, I consider it to be better to provide information that might get lost instead of not providing info at all in the first place.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have a link. It will make our life easier in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for giving me your input.
I have added your comment.

@MGatner MGatner merged commit 2020c2d into codeigniter4:develop Jan 16, 2021
@ytetsuro ytetsuro deleted the fix/NewlinesAtTheEndAreIncludInThePatternMatch branch January 18, 2021 01:16
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

Successfully merging this pull request may close these issues.

6 participants