import re
def sanitize_file_contents(file_content, allowed_chars=None, min_length=0, max_length=None):
"""
Sanitizes file contents for validation checks with fallback logic.
Args:
file_content (str): The content of the file to sanitize.
allowed_chars (str, optional): A string containing the characters allowed in the file content.
If None, all characters are allowed. Defaults to None.
min_length (int, optional): The minimum length of the file content. Defaults to 0.
max_length (int, optional): The maximum length of the file content. Defaults to None (no limit).
Returns:
str: The sanitized file content. Returns the original content if sanitization fails.
"""
if allowed_chars is None:
allowed_chars = r"^\w+$" # Allow alphanumeric characters
if not isinstance(file_content, str):
return file_content #or raise TypeError("Input must be a string")
if not re.match(allowed_chars, file_content):
# Fallback: Remove invalid characters
sanitized_content = re.sub(r'[^\w\s]', '', file_content) #remove special characters
if not re.match(allowed_chars, sanitized_content):
return file_content #return original if still invalid
if len(file_content) < min_length:
# Fallback: Pad with spaces
sanitized_content = file_content.ljust(min_length, ' ')
if max_length is not None and len(file_content) > max_length:
# Fallback: Truncate
sanitized_content = file_content[:max_length]
return sanitized_content
Add your comment