Merge pull request #10 from fabriziosalmi/codeflash/optimize-validate_regex-m6xoy1wp

️ Speed up function `validate_regex` by 162%
This commit is contained in:
fab 2025-02-09 15:15:00 +01:00 committed by GitHub
commit 75cdaa01b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,6 +5,7 @@ from collections import defaultdict
import logging import logging
from pathlib import Path from pathlib import Path
from typing import List, Dict, Set, Tuple, Optional from typing import List, Dict, Set, Tuple, Optional
from functools import lru_cache
# Configure logging # Configure logging
logging.basicConfig( logging.basicConfig(
@ -58,6 +59,7 @@ def load_owasp_rules(file_path: Path) -> List[Dict]:
raise raise
@lru_cache(maxsize=None)
def validate_regex(pattern: str) -> bool: def validate_regex(pattern: str) -> bool:
""" """
Validate regex pattern to ensure it is compatible with ModSecurity. Validate regex pattern to ensure it is compatible with ModSecurity.
@ -165,6 +167,51 @@ def main() -> None:
logging.critical(f"[!] Script failed: {e}") logging.critical(f"[!] Script failed: {e}")
exit(1) exit(1)
def load_json(file_path):
"""
Load and parse JSON file.
Args:
file_path (Path): Path to the JSON file to be loaded.
Returns:
dict: Parsed JSON content.
"""
with file_path.open('r', encoding='utf-8') as f:
return json.load(f)
def write_rules_to_file(rules, output_path):
"""
Write ModSecurity rules to a file.
Args:
rules (list): List of ModSecurity rules as strings.
output_path (Path): Path to the output file.
"""
with output_path.open('w', encoding='utf-8') as f:
f.writelines(rules)
def main():
json_data = load_json(INPUT_FILE)
rules = []
rule_id = 1000 # Initial rule ID
for rule in json_data.get('rules', []):
pattern = rule.get('pattern')
category = rule.get('category')
if not pattern or any(unsupported in pattern for unsupported in UNSUPPORTED_PATTERNS):
logging.info(f"[!] Skipping unsupported pattern: {pattern}")
continue
if validate_regex(pattern):
rules.append(MODSEC_RULE_TEMPLATE.format(pattern=pattern, rule_id=rule_id, category=category))
rule_id += 1
output_file_path = OUTPUT_DIR / "rules.conf"
write_rules_to_file(rules, output_file_path)
if __name__ == "__main__": if __name__ == "__main__":
main() main()