""" Copyright 2024-2026 ReplyPier Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ """ Test backend message sanitization """ from app.utils.sanitize import sanitize_message class TestSanitizeMessage: """Test message sanitization to prevent XSS""" def test_sanitize_link_tag(self): """Test that tags are completely removed including content""" malicious = 'Click me' result = sanitize_message(malicious) assert ' tags are completely removed""" malicious = 'Hidden' result = sanitize_message(malicious) assert 'Trust No AI' result = sanitize_message(malicious) assert 'Trust No AI' result = sanitize_message(malicious) assert 'Click me' result = sanitize_message(malicious) assert 'javascript:' not in result.lower() def test_sanitize_onclick_handler(self): """Test that onclick event handlers are removed""" malicious = '' result = sanitize_message(malicious) assert 'onclick' not in result.lower() def test_sanitize_onerror_handler(self): """Test that onerror event handlers are removed""" malicious = '' result = sanitize_message(malicious) assert 'onerror' not in result.lower() def test_sanitize_data_uri(self): """Test that data: URIs (except images) are removed""" malicious = 'Click' result = sanitize_message(malicious) assert 'data:text/html' not in result.lower() def test_sanitize_removes_img_tags(self): """Test that all img tags are removed for security (no external resource loading)""" safe = '' result = sanitize_message(safe) # All img tags are removed to prevent external resource loading and tracking assert '' result = sanitize_message(malicious) assert '' result = sanitize_message(malicious) assert ' click ''' result = sanitize_message(malicious) assert '' result = sanitize_message(malicious) assert 'Click' result = sanitize_message(malicious) assert 'vbscript' not in result.lower() def test_sanitize_base_tag(self): """Test base tag is removed""" malicious = '' result = sanitize_message(malicious) assert '