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

⚡️ Speed up function sanitize_pattern by 11,547% #12

Merged
merged 1 commit into from
Feb 9, 2025
Merged
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
49 changes: 27 additions & 22 deletions json2nginx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
from pathlib import Path
from collections import defaultdict
from functools import lru_cache

# Configure logging
logging.basicConfig(
Expand Down Expand Up @@ -39,34 +40,15 @@ def load_owasp_rules(file_path):
def validate_regex(pattern):
"""Validate if a pattern is a valid regex."""
try:
re.compile(pattern)
_compile_pattern(pattern)
return True
except re.error:
return False


def sanitize_pattern(pattern):
"""Sanitize and validate OWASP patterns for Nginx compatibility."""
if any(
keyword in pattern
for keyword in ["@pmFromFile", "!@eq", "!@within", "@lt"]
):
logging.warning(f"Skipping unsupported pattern: {pattern}")
return None

if pattern.startswith("@rx "):
sanitized_pattern = pattern.replace("@rx ", "").strip()
if validate_regex(sanitized_pattern):
return re.escape(sanitized_pattern).replace(r'\@', '@')
else:
logging.warning(f"Invalid regex in pattern: {sanitized_pattern}")
return None

if validate_regex(pattern):
return re.escape(pattern).replace(r'\@', '@')
else:
logging.warning(f"Invalid regex in pattern: {pattern}")
return None
"""Wrapper function to use caching for patterns."""
return _sanitize_pattern(pattern)


def generate_nginx_waf(rules):
Expand Down Expand Up @@ -168,6 +150,29 @@ def main():
logging.critical(f"Script failed: {e}")
exit(1)

@lru_cache(maxsize=128)
def _compile_pattern(pattern):
"""Compile the regex pattern with caching to avoid recompilation."""
return re.compile(pattern)

@lru_cache(maxsize=128)
def _sanitize_pattern(pattern):
"""Sanitize and validate OWASP patterns for Nginx compatibility."""
if any(keyword in pattern for keyword in ["@pmFromFile", "!@eq", "!@within", "@lt"]):
logging.warning(f"Skipping unsupported pattern: {pattern}")
return None

if pattern.startswith("@rx "):
sanitized_pattern = pattern.replace("@rx ", "").strip()
else:
sanitized_pattern = pattern

if validate_regex(sanitized_pattern):
return re.escape(sanitized_pattern).replace(r'\@', '@')
else:
logging.warning(f"Invalid regex in pattern: {sanitized_pattern}")
return None


if __name__ == "__main__":
main()