Skip to content

Conversation

@richm
Copy link
Contributor

@richm richm commented Jul 30, 2025

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

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:

  • Update regex for Cmnd_Alias, Host_Alias, Runas_Alias, and User_Alias to require uppercase alias names and handle '=' without surrounding spaces

Tests:

  • Add playbook-based tests to verify alias definitions parse correctly both with and without spaces around '='

@richm richm requested a review from spetrosi as a code owner July 30, 2025 23:49
@sourcery-ai
Copy link

sourcery-ai bot commented Jul 30, 2025

Reviewer's Guide

Update 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

Change Details Files
Refine alias regex patterns to match sudoers eBNF NAME and optional spacing
  • Use ([A-Z][A-Z0-9_]) instead of \S+ for alias names
  • Replace \s={1}\s* with \s*=\s* for consistent spacing
  • Add comment referencing sudoers spec and NAME definition
library/scan_sudoers.py
Add functional tests for alias parsing scenarios
  • Create tests/tests_scan_sudoers.yml to validate alias definitions with/without spaces
  • Define alias_values and dynamically generate sudoers content
  • Assert correct formatting and header presence after role execution
tests/tests_scan_sudoers.yml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a 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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codecov
Copy link

codecov bot commented Jul 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@c82f25b). Learn more about missing BASE report.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

# 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

This part of the regular expression may cause exponential backtracking on strings starting with 'Cmnd_Alias A=' and containing many repetitions of '!,'.
# 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

This part of the regular expression may cause exponential backtracking on strings starting with 'Cmnd_Alias A=!' and containing many repetitions of 'a'.
)
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

This part of the regular expression may cause exponential backtracking on strings starting with 'Host_Alias A=' and containing many repetitions of '!,'.
)
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

This part of the regular expression may cause exponential backtracking on strings starting with 'Host_Alias A=!' and containing many repetitions of 'a'.
)
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

This part of the regular expression may cause exponential backtracking on strings starting with 'Runas_Alias A=' and containing many repetitions of '!,'.
)
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

This part of the regular expression may cause exponential backtracking on strings starting with 'Runas_Alias A=!' and containing many repetitions of 'a'.
)
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

This part of the regular expression may cause exponential backtracking on strings starting with 'User_Alias A=' and containing many repetitions of '!,'.
)
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

This part of the regular expression may cause exponential backtracking on strings starting with 'User_Alias A=!' and containing many repetitions of 'a'.
… 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>
@richm richm changed the title fix: Use the correct regular expression to parse Cmnd_Alias and other aliases. fix: Use the correct regular expression to parse Cmnd_Alias and other aliases Jul 31, 2025
@richm
Copy link
Contributor Author

richm commented Jul 31, 2025

[citest]

@richm richm requested a review from radosroka July 31, 2025 00:03
@richm richm merged commit 87d3b31 into linux-system-roles:main Aug 1, 2025
35 of 37 checks passed
@richm richm deleted the RHEL-106261 branch August 1, 2025 12:23
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.

1 participant