fix(postprocessing): raise SigmaConfigurationError on invalid regex in ReplaceQueryTransformation - #523
Open
Romil2112 wants to merge 2 commits into
Conversation
…n ReplaceQueryTransformation ReplaceQueryTransformation.__post_init__ called re.compile() without a try/except, so an invalid pattern surfaced as a bare re.error rather than the SigmaConfigurationError callers expect. Wraps the call to match the pattern already used by ReplaceStringTransformation, ExtractFieldsTransformation, and ExternalSourceBaseTransformation. Adds a regression test that constructs ReplaceQueryTransformation with an invalid pattern and asserts SigmaConfigurationError is raised. Fixes SigmaHQ#522
…cludeFieldCondition IncludeFieldCondition.__post_init__ compiled user-supplied field patterns in a bare list comprehension with no error handling. An invalid regex pattern raised re.error (stdlib) instead of SigmaConfigurationError, inconsistent with every other condition and transformation in the codebase that wraps re.compile(). ExcludeFieldCondition inherits __post_init__ from IncludeFieldCondition and gets the fix for free. Adds a regression test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two places in the codebase call
re.compile()on user-supplied patterns without wrapping the call, so an invalid regex raises a barere.errorfrom the stdlib instead of theSigmaConfigurationErrorcallers expect.Every other transformation/condition that compiles a user-supplied pattern already wraps it:
ReplaceStringTransformationSigmaRegularExpressionErrorExtractFieldsTransformationSigmaRegularExpressionErrorExternalSourceBaseTransformationSigmaConfigurationErrorMatchValueConditionSigmaRegularExpressionErrorReplaceQueryTransformationre.error← inconsistentIncludeFieldCondition(mode=re)re.error← inconsistentFix
Both fixed in this PR:
sigma/processing/postprocessing.py—ReplaceQueryTransformation.__post_init__: wrapre.compile(self.pattern)→ raiseSigmaConfigurationErrorsigma/processing/conditions/fields.py—IncludeFieldCondition.__post_init__(also coversExcludeFieldCondition, which inherits__post_init__): replace the bare list comprehension with a loop that wraps eachre.compile()→ raiseSigmaConfigurationErrorChanges
sigma/processing/postprocessing.pysigma/processing/conditions/fields.pytests/test_postprocessing_transformations.py:test_replace_query_transformation_invalid_regextests/test_processing_conditions.py:test_include_field_condition_invalid_reTesting
All 1521 existing tests continue to pass; both new tests confirm the fixes.
Closes #522