-
Notifications
You must be signed in to change notification settings - Fork 8
fix: Use the correct regular expression to parse Cmnd_Alias and other aliases #68
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
Conversation
Reviewer's GuideUpdate alias parsing regex in scan_sudoers.py to use eBNF‐compliant NAME tokens and handle optional spacing around “=”, and add a new test suite to verify alias definitions with and without spaces. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
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.
Hey @richm - I've reviewed your changes - here's some feedback:
- The alias regex patterns are almost identical—consider refactoring the common pattern generation into a helper function or template to avoid duplication and simplify future maintenance.
- You can improve readability and performance by converting your capturing groups into non-capturing groups (?:…) where you don’t need to retain matches, and by removing redundant quantifiers.
- It would be valuable to add a negative test case verifying that alias names starting with non-uppercase letters are rejected, to ensure the regex strictly enforces the upper-case NAME rule.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The alias regex patterns are almost identical—consider refactoring the common pattern generation into a helper function or template to avoid duplication and simplify future maintenance.
- You can improve readability and performance by converting your capturing groups into non-capturing groups (?:…) where you don’t need to retain matches, and by removing redundant quantifiers.
- It would be valuable to add a negative test case verifying that alias names starting with non-uppercase letters are rejected, to ensure the regex strictly enforces the upper-case NAME rule.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #68 +/- ##
=======================================
Coverage ? 52.31%
=======================================
Files ? 1
Lines ? 346
Branches ? 0
=======================================
Hits ? 181
Misses ? 165
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| # I'm assuming these are ASCII - so the pattern used for NAME is ([A-Z][A-Z0-9_]*) | ||
| cmnd_alias_re = re.compile( | ||
| r"(^Cmnd_Alias)+\s+(\S+)+\s*\={1}\s*((\S+,{1}\s*)+\S+|\S+)\s*(\:)*(.*)*$" | ||
| r"(^Cmnd_Alias)+\s+([A-Z][A-Z0-9_]*)\s*\=\s*((\S+,\s*)+\S+|\S+)\s*(\:)*(.*)*$" |
Check failure
Code scanning / CodeQL
Inefficient regular expression High
| # I'm assuming these are ASCII - so the pattern used for NAME is ([A-Z][A-Z0-9_]*) | ||
| cmnd_alias_re = re.compile( | ||
| r"(^Cmnd_Alias)+\s+(\S+)+\s*\={1}\s*((\S+,{1}\s*)+\S+|\S+)\s*(\:)*(.*)*$" | ||
| r"(^Cmnd_Alias)+\s+([A-Z][A-Z0-9_]*)\s*\=\s*((\S+,\s*)+\S+|\S+)\s*(\:)*(.*)*$" |
Check failure
Code scanning / CodeQL
Inefficient regular expression High
| ) | ||
| host_alias_re = re.compile( | ||
| r"(^Host_Alias)+\s+(\S+)+\s*\={1}\s*((\S+,{1}\s*)+\S+|\S+)\s*(\:)*(.*)*$" | ||
| r"(^Host_Alias)+\s+([A-Z][A-Z0-9_]*)\s*\=\s*((\S+,\s*)+\S+|\S+)\s*(\:)*(.*)*$" |
Check failure
Code scanning / CodeQL
Inefficient regular expression High
| ) | ||
| host_alias_re = re.compile( | ||
| r"(^Host_Alias)+\s+(\S+)+\s*\={1}\s*((\S+,{1}\s*)+\S+|\S+)\s*(\:)*(.*)*$" | ||
| r"(^Host_Alias)+\s+([A-Z][A-Z0-9_]*)\s*\=\s*((\S+,\s*)+\S+|\S+)\s*(\:)*(.*)*$" |
Check failure
Code scanning / CodeQL
Inefficient regular expression High
| ) | ||
| runas_alias_re = re.compile( | ||
| r"(^Runas_Alias)+\s+(\S+)+\s*\={1}\s*((\S+,{1}\s*)+\S+|\S+)\s*(\:)*(.*)*$" | ||
| r"(^Runas_Alias)+\s+([A-Z][A-Z0-9_]*)\s*\=\s*((\S+,\s*)+\S+|\S+)\s*(\:)*(.*)*$" |
Check failure
Code scanning / CodeQL
Inefficient regular expression High
| ) | ||
| runas_alias_re = re.compile( | ||
| r"(^Runas_Alias)+\s+(\S+)+\s*\={1}\s*((\S+,{1}\s*)+\S+|\S+)\s*(\:)*(.*)*$" | ||
| r"(^Runas_Alias)+\s+([A-Z][A-Z0-9_]*)\s*\=\s*((\S+,\s*)+\S+|\S+)\s*(\:)*(.*)*$" |
Check failure
Code scanning / CodeQL
Inefficient regular expression High
| ) | ||
| user_alias_re = re.compile( | ||
| r"(^User_Alias)+\s+(\S+)+\s*\={1}\s*((\S+,{1}\s*)+\S+|\S+)\s*(\:)*(.*)*$" | ||
| r"(^User_Alias)+\s+([A-Z][A-Z0-9_]*)\s*\=\s*((\S+,\s*)+\S+|\S+)\s*(\:)*(.*)*$" |
Check failure
Code scanning / CodeQL
Inefficient regular expression High
| ) | ||
| user_alias_re = re.compile( | ||
| r"(^User_Alias)+\s+(\S+)+\s*\={1}\s*((\S+,{1}\s*)+\S+|\S+)\s*(\:)*(.*)*$" | ||
| r"(^User_Alias)+\s+([A-Z][A-Z0-9_]*)\s*\=\s*((\S+,\s*)+\S+|\S+)\s*(\:)*(.*)*$" |
Check failure
Code scanning / CodeQL
Inefficient regular expression High
… aliases. Cause: The regex was not taking into consideration that the Cmnd_Alias value does not have to have spaces on either side of the `=`. The same is true for other Alias values. Consequence: The regex would never terminate, and the role would appear to hang. Fix: Ensure the regex complies with the eBNF definition of the field from the sudoers file specification. Result: The Alias values are parsed correctly. https://issues.redhat.com/browse/RHEL-106261 Signed-off-by: Rich Megginson <rmeggins@redhat.com>
|
[citest] |
Cause: The regex was not taking into consideration that the Cmnd_Alias value
does not have to have spaces on either side of the
=. The same is true forother Alias values.
Consequence: The regex would never terminate, and the role would appear to hang.
Fix: Ensure the regex complies with the eBNF definition of the field from the
sudoers file specification.
Result: The Alias values are parsed correctly.
https://issues.redhat.com/browse/RHEL-106261
Signed-off-by: Rich Megginson rmeggins@redhat.com
Summary by Sourcery
Fix alias parsing regex to align with sudoers specification and add tests to validate parsing with and without spaces around '='.
Bug Fixes:
Tests: