Validate input content for potentially problematic characters or patterns. Args: input_text: The input text to validate Returns: Tuple[bool, str]: (is_valid, error_message)
(input_text: str)
| 722 | |
| 723 | |
| 724 | def validate_input_content(input_text: str) -> Tuple[bool, str]: |
| 725 | """Validate input content for potentially problematic characters or patterns. |
| 726 | |
| 727 | Args: |
| 728 | input_text: The input text to validate |
| 729 | |
| 730 | Returns: |
| 731 | Tuple[bool, str]: (is_valid, error_message) |
| 732 | """ |
| 733 | if not input_text or input_text.isspace(): |
| 734 | return False, "Input content cannot be empty or only whitespace." |
| 735 | |
| 736 | # Check for minimum length |
| 737 | if len(input_text.strip()) < 2: |
| 738 | return False, "Input content must be at least 2 characters long." |
| 739 | |
| 740 | # Check for maximum length (e.g., 100KB) |
| 741 | if len(input_text.encode("utf-8")) > 100 * 1024: |
| 742 | return False, "Input content exceeds maximum size of 100KB." |
| 743 | |
| 744 | # Check for high concentration of special characters |
| 745 | special_chars = set("!@#$%^&*()_+[]{}|\\;:'\",.<>?`~") |
| 746 | special_char_count = sum(1 for c in input_text if c in special_chars) |
| 747 | special_char_ratio = special_char_count / len(input_text) |
| 748 | |
| 749 | if special_char_ratio > 0.3: # More than 30% special characters |
| 750 | return ( |
| 751 | False, |
| 752 | "Input contains too many special characters. Please check your input.", |
| 753 | ) |
| 754 | |
| 755 | # Check for control characters |
| 756 | control_chars = set( |
| 757 | chr(i) for i in range(32) if i not in [9, 10, 13] |
| 758 | ) # Allow tab, newline, carriage return |
| 759 | if any(c in control_chars for c in input_text): |
| 760 | return False, "Input contains invalid control characters." |
| 761 | |
| 762 | # Check for proper UTF-8 encoding |
| 763 | try: |
| 764 | input_text.encode("utf-8").decode("utf-8") |
| 765 | except UnicodeError: |
| 766 | return False, "Input contains invalid Unicode characters." |
| 767 | |
| 768 | return True, "" |
| 769 | |
| 770 | |
| 771 | def sanitize_input_content(input_text: str) -> str: |
no outgoing calls
no test coverage detected