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

Optimize the regex parser for InvalidResponse #1034

Merged

Conversation

peternied
Copy link
Member

Description

Rather than using a backtracking pattern which could have performance impact, switch to using explict character ranges which work for json path seperated with periods.

Mitigates sonar lint rule java:S5852 [1]

Check List

  • New functionality includes testing
    • All tests pass, including unit test, integration test and doctest
  • New functionality has been documented
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Rather than using a backtracking pattern which could have performance
impact, switch to using explict character ranges which work for json
path seperated with periods.

Mitigates sonar lint rule java:S5852 [1]

- [1] https://rules.sonarsource.com/java/RSPEC-5852/?search=Using%20slow%20regular%20expressions%20is%20security-sensitive

Signed-off-by: Peter Nied <[email protected]>
Copy link

codecov bot commented Sep 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.55%. Comparing base (d5f5be6) to head (39e67ae).
Report is 2 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1034      +/-   ##
============================================
- Coverage     80.55%   80.55%   -0.01%     
  Complexity     2735     2735              
============================================
  Files           365      365              
  Lines         13611    13611              
  Branches        941      941              
============================================
- Hits          10965    10964       -1     
  Misses         2068     2068              
- Partials        578      579       +1     
Flag Coverage Δ
gradle-test 78.58% <100.00%> (-0.01%) ⬇️
python-test 90.11% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@@ -17,7 +17,7 @@

@Slf4j
public class InvalidResponse extends RfsException {
private static final Pattern UNKNOWN_SETTING = Pattern.compile("unknown setting \\[(.+?)\\].+");
private static final Pattern UNKNOWN_SETTING = Pattern.compile("unknown setting \\[([a-zA-Z0-9_.-]+)\\].+");
Copy link
Member

Choose a reason for hiding this comment

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

Would the issue also be resolved by changing .+? -> .+

Copy link
Member Author

@peternied peternied Sep 30, 2024

Choose a reason for hiding this comment

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

Yes, but that expression wouldn't be correct since it could match the closing bracket or a host of invalid characters that opensearch would never use in a setting name.

  • ✔️ unknown setting [foo.bar] other stuff -> foo.bar
  • unknown setting [foo.bar] other stuff ] -> foo.bar] other stuff

Copy link
Member

@AndreKurait AndreKurait Sep 30, 2024

Choose a reason for hiding this comment

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

I see.. the issue is that ] shouldn't be a matching character in the inner match.

"unknown setting \\[([^\\]\n]+?)\\].+" this would be the most similar to the original logic that doesn't have the bug

This change seems safe to set the characters inside the [] to those in this PR

@peternied
Copy link
Member Author

Needed to retry the gradle tests, created Flaky Test: WorkCoordinatorTest.testAcquireLeaseForQuery [1]

@peternied peternied merged commit f28e131 into opensearch-project:main Sep 30, 2024
14 checks passed
@peternied peternied deleted the optmize-invalid-response-regex branch September 30, 2024 19:25
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.

2 participants