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

Capture original regex in non-matching group when an exact match is r… #7778

Merged

Conversation

libklein
Copy link
Contributor

@libklein libklein commented Apr 3, 2022

Fixes #7776.

Tools::convetToRegex pre-/appends "^"/"$" to the built regex if an exact match is requested. This breaks expressions such as "v|", which, instead of matching v or the empty string, matches v or the end of line after substitution: "^v|$" is parsed as "^(v|$)".

Surrounding the original regex with an non-capturing group fixes this error.

See https://pcre.org/pcre.txt Sections "Vertical Bar" and "Subpatterns".

Testing strategy

Added test to TestFdoSecrets and TestTools.

Type of change

  • ✅ Bug fix (non-breaking change that fixes an issue)

@codecov-commenter
Copy link

codecov-commenter commented Apr 3, 2022

Codecov Report

Attention: Patch coverage is 96.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 64.47%. Comparing base (6f20f0e) to head (b6d140c).
Report is 422 commits behind head on develop.

Files with missing lines Patch % Lines
src/core/Tools.cpp 96.67% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #7778      +/-   ##
===========================================
+ Coverage    64.46%   64.47%   +0.01%     
===========================================
  Files          339      339              
  Lines        43610    43637      +27     
===========================================
+ Hits         28110    28133      +23     
- Misses       15500    15504       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@droidmonkey
Copy link
Member

Was thinking this was where the error was, great change!

@Aetf
Copy link
Contributor

Aetf commented Apr 3, 2022

Why not use QRegularExpression::escape to escape the string
if exact match is requested? The current fix breaks on inputs like )v|(?:

In some sense this is pretty similar to protecting against SQL injection...

Edit: there's also QRegularExpression::wildcardToRegularExpression.
But then I noticed both functions are only available in Qt 5.15...

@droidmonkey
Copy link
Member

droidmonkey commented Apr 3, 2022

That is why lol, not available yet for our minimum distros. Does the Qt function choke on these "malicious" type strings as well?

@Aetf
Copy link
Contributor

Aetf commented Apr 3, 2022

I haven't tried but I don't think so.

Here's the implementation, we should do something similar

https://code.woboq.org/qt5/qtbase/src/corelib/text/qregularexpression.cpp.html#1899

https://code.woboq.org/qt5/qtbase/src/corelib/text/qregularexpression.cpp.html#1818

@droidmonkey
Copy link
Member

I think I prefer just copying the 5.15 source code into our tools with attribution.

@libklein
Copy link
Contributor Author

libklein commented Apr 5, 2022

I agree and will work on that and some further tests later tonight to hopefully make 2.7.1.

@libklein
Copy link
Contributor Author

libklein commented Apr 6, 2022

That is why lol, not available yet for our minimum distros. Does the Qt function choke on these "malicious" type strings as well?

Actually, the QT implementation of exact matches (https://doc.qt.io/qt-5/qregularexpression.html#anchoredPattern) does exactly the same thing were doing right now. The "malicious string" is malicious only because it is not a proper regular expression, which is something Tools::convertToRegex should require.

As we'd need the copy the existing implementation (https://github.com/qt/qtbase/blob/2eb7a92aa373f10f4e6828dad640b393279637a3/src/corelib/text/qregularexpression.cpp#L2053, available > qt 5.12) into our source anyway, i'd just leave the function as is.

I'd still implement QRegularExpression::escape (and expose it), simply because it allows us to granularize tests. See the latest commit. I'll add further tests if you agree with the approach.

Also, does someone know if the attribution is correct?

@libklein libklein force-pushed the fix/string-to-regex-exact-match branch from 845ed38 to ee489e0 Compare April 6, 2022 19:49
@droidmonkey
Copy link
Member

Should invoke the GPL 3 statements from their license block at the top of the qregularexpression.cpp.

@libklein
Copy link
Contributor Author

libklein commented Apr 7, 2022

I've fixed some more bugs with overlapping flag values, added some further tests, and the QT license header.

After spending some time browsing through the codebase i'd suggest splitting convertToRegex into createWildcardRegex and createRegex (perhaps even get rid of the latter one) as these are orthogonal anyway.

src/core/Tools.cpp Outdated Show resolved Hide resolved
src/core/Tools.cpp Outdated Show resolved Hide resolved
src/core/Tools.cpp Outdated Show resolved Hide resolved
@libklein libklein force-pushed the fix/string-to-regex-exact-match branch from 4bf81e9 to e171c88 Compare May 21, 2022 14:34
@droidmonkey droidmonkey added this to the v2.7.2 milestone May 28, 2022
@droidmonkey droidmonkey added pr: backport pending Pull request yet to be backported to a previous release bug labels May 28, 2022
@droidmonkey droidmonkey force-pushed the fix/string-to-regex-exact-match branch from e171c88 to be1052d Compare June 4, 2022 20:28
* Fixes keepassxreboot#7776

Implement QRegularExpression::escape within Tools::convertToRegex to allow usage on older Qt versions.

Also wrap EXACT_MODIFIER patterns in a non-capture group to prevent misinterpreted regex.
@droidmonkey droidmonkey force-pushed the fix/string-to-regex-exact-match branch from be1052d to b6d140c Compare June 4, 2022 21:17
@droidmonkey droidmonkey merged commit e16c007 into keepassxreboot:develop Jun 6, 2022
@droidmonkey droidmonkey added pr: backported Pull request backported to previous release and removed pr: backport pending Pull request yet to be backported to a previous release labels Jun 27, 2022
@phoerious phoerious added pr: bugfix Pull request that fixes a bug and removed bug labels Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: backported Pull request backported to previous release pr: bugfix Pull request that fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Secret service attribute search containing a '|' (vertical bar) returns all entries
6 participants