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

Improved messaging around file inclusion/exclusion patterns #81

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions wordfence/cli/malwarescan/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
},
"include-files-pattern": {
"short_name": "N",
"description": "PCRE regex allow pattern. Only matching filenames will"
" be scanned.",
"description": "Python regex allow pattern. Only matching filenames "
"will be scanned.",
"context": "ALL",
"argument_type": "OPTION_REPEATABLE",
"default": None,
Expand All @@ -100,8 +100,8 @@
},
"exclude-files-pattern": {
"short_name": "X",
"description": "PCRE regex deny pattern. Matching filenames will not "
"be scanned.",
"description": "Python regex deny pattern. Matching filenames will "
"not be scanned.",
"context": "ALL",
"argument_type": "OPTION_REPEATABLE",
"default": None,
Expand Down
16 changes: 14 additions & 2 deletions wordfence/cli/malwarescan/malwarescan.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,25 @@ def _initialize_file_filter(self) -> filtering.FileFilter:
if self.config.include_files_pattern is not None:
has_include_overrides = True
for pattern in self.config.include_files_pattern:
filter.add(filtering.filter_pattern(pattern))
try:
filter.add(filtering.filter_pattern(pattern))
except filtering.InvalidPatternException:
raise Exception(
'File inclusion pattern must be a Python regex, '
f'received: "{pattern}"'
)
if self.config.exclude_files is not None:
for name in self.config.exclude_files:
filter.add(filtering.filter_filename(name), False)
if self.config.exclude_files_pattern is not None:
for pattern in self.config.exclude_files_pattern:
filter.add(filtering.filter_pattern(pattern), False)
try:
filter.add(filtering.filter_pattern(pattern), False)
except filtering.InvalidPatternException:
raise Exception(
'File exclusion pattern must be a Python regex, '
f'received: "{pattern}"'
)
if not has_include_overrides:
filter.add(filtering.filter_php)
filter.add(filtering.filter_html)
Expand Down
13 changes: 11 additions & 2 deletions wordfence/scanning/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ def __call__(self, path: str) -> bool:
return matches_regex(self.pattern, path)


class InvalidPatternException(Exception):

def __init__(self, pattern: str):
self.pattern = pattern


def filter_pattern(regex: str) -> Callable[[str], bool]:
pattern = re.compile(regex)
return Filter(pattern)
try:
pattern = re.compile(regex)
return Filter(pattern)
except re.error:
raise InvalidPatternException(regex)