Skip to content

Fixed pattern issue#1057

Open
ckbaker10 wants to merge 1 commit intogoss-org:masterfrom
ckbaker10:fix-regex-matcher
Open

Fixed pattern issue#1057
ckbaker10 wants to merge 1 commit intogoss-org:masterfrom
ckbaker10:fix-regex-matcher

Conversation

@ckbaker10
Copy link
Copy Markdown

Checklist
  • make test-all (UNIX) passes. CI will also test this
  • unit and/or integration tests are included (if applicable)
  • documentation is changed or added (if applicable)

Description of change

have-patterns matcher silently ignored the /i (case-insensitive) flag — and any other trailing flags — on /pattern/flags style patterns, causing those patterns to always fail when the output contained mixed-case text.

Root cause: sliceToPatterns classified a string as a regex only when it ended with a bare / (strings.HasSuffix(s, "/")). A pattern like /loglevel (verbose|info)/i ends with /i, so it fell through to the stringPattern branch and was evaluated with a strings.Contains check. The regex and the /i flag were both completely ignored. Additionally, newRegexPattern stripped only the literal trailing / character, so even patterns that did reach the regex path would have had any flags silently included in the compiled expression (causing a compile error or incorrect behaviour depending on the flag string).

Fix:

  • Replaced the strings.HasSuffix(s, "/") check in sliceToPatterns with a new isRegexPattern helper that correctly recognises /pattern/, /pattern/i, /pattern/ms, etc. (allowing only valid Go RE2 inline flag characters i, m, s after the closing delimiter).
  • Updated newRegexPattern to split at the last /, extract any trailing flags, and prepend them as a Go inline flag group ((?flags)) before compiling — e.g. /loglevel (verbose|info)/i is compiled as (?i)loglevel (verbose|info).
  • All existing behaviour for plain /pattern/ patterns and literal string patterns is preserved.

Tests added (matchers/have_patterns_test.go):

  • TestSliceToPatterns_FlaggedRegex — verifies that patterns with trailing flags are classified as regex patterns, not string-contains patterns.
  • TestNewRegexPattern_CaseInsensitiveFlag — unit-tests newRegexPattern with the exact sshd -T output lines from the failing CIS audit controls (5.1.6, 5.1.12, 5.1.14, 5.1.15).
  • TestHavePatternsMatcher_CaseInsensitiveFlag — end-to-end tests through the full HavePatternsMatcher, covering all four SSH-related CIS control failures where /i patterns failed against mixed-case output such as loglevel INFO.

@ckbaker10 ckbaker10 requested a review from aelsabbahy as a code owner April 22, 2026 13:22
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