-
Notifications
You must be signed in to change notification settings - Fork 384
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
Optimize method of stripping validation response of affixed HTML comments #6021
Optimize method of stripping validation response of affixed HTML comments #6021
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #6021 +/- ##
==========================================
Coverage 75.26% 75.27%
- Complexity 5703 5709 +6
==========================================
Files 218 218
Lines 17275 17283 +8
==========================================
+ Hits 13002 13009 +7
- Misses 4273 4274 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Plugin builds for b222c5d are ready 🛎️!
|
@@ -1848,7 +1848,7 @@ public static function validate_url( $url ) { | |||
$response = ltrim( $response ); | |||
|
|||
// Strip HTML comments that may have been injected at the end of the response (e.g. by a caching plugin). | |||
$response = preg_replace( '/<!--.*?-->\s*$/s', '', $response ); | |||
$response = preg_replace( '/}\s*?<!--.*?-->\s*$/', '}', $response ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The removal of the s
will cause a problem in that it will not match a comment like this:
…
</body></html>
<!--
generated 2 seconds ago
generated in 1.134 seconds
served from batcache in 0.003 seconds
expires in 298 seconds
-->
I've added a failing test case to demonstrate this: 9f7ef9c.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the s
flag in b7c7e29.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think an alternative approach is needed, perhaps atomic grouping? This would seem to be avoidable if the regex engine started searching from the end of the string rather than the beginning.
I've tried several different variants of atomic grouping, but all either failed to find a match or led to catastrophic backtracing, unfortunately. I've also looked into searching from the end of the string, but PCRE doesn't support that feature. One flaw I just noticed in the current regex is that it is possible for catastrophic backtracing to occur if the HTML comment being matched contains too much text, as demonstrated here. I'm not sure how to resolve that as yet. |
How about not using a regex, but rather I'm not entirely sure what that means in terms of performance, but:
|
Actually, I'm not sure it is even possible to cycle backwards char-by-char when you can have multibyte chars. I don't think you can detect that when scanning from back to front. |
How about something like this: https://3v4l.org/orG5Y |
That seems like a great alternative. Any objections @westonruter? |
Yes, that looks good. I made a small tweak to make sure that the |
Co-authored-by: Alain Schlesser <[email protected]> Co-authored-by: Weston Ruter <[email protected]>
This reverts commit 0cfa913.
Summary
Fixes #6011
Checklist