1. import re
  2. def normalize_headers(headers):
  3. """
  4. Normalizes request headers, handling case, whitespace, and basic sanity checks.
  5. Args:
  6. headers (dict): A dictionary of request headers.
  7. Returns:
  8. dict: A dictionary of normalized headers. Returns an empty dictionary if input is invalid.
  9. """
  10. if not isinstance(headers, dict):
  11. print("Error: Input must be a dictionary.")
  12. return {}
  13. normalized_headers = {}
  14. for key, value in headers.items():
  15. if not isinstance(key, str):
  16. print(f"Warning: Invalid header key: {key}. Skipping.") #Log warning instead of error
  17. continue
  18. if not isinstance(value, str):
  19. normalized_headers[key] = str(value) #Convert to string if not already
  20. continue
  21. # Normalize case to lowercase
  22. normalized_key = key.lower()
  23. normalized_value = value.lower()
  24. # Remove leading/trailing whitespace
  25. normalized_key = normalized_key.strip()
  26. normalized_value = normalized_value.strip()
  27. #Sanity check: Remove potentially problematic characters
  28. normalized_key = re.sub(r'[^a-z0-9_]+', '', normalized_key)
  29. normalized_value = re.sub(r'[^a-z0-9_.,\s-]+', '', normalized_value) #Allow periods, commas, spaces and hyphens
  30. if not normalized_key:
  31. print(f"Warning: Empty header key after sanitization: {key}. Skipping.")
  32. continue
  33. normalized_headers[normalized_key] = normalized_value
  34. return normalized_headers

Add your comment