""" Security tests for OWASP Top 10 vulnerabilities ================================================= Tests for common web application security vulnerabilities. """ import pytest pytestmark = pytest.mark.security class TestSQLInjection: """Tests for SQL injection vulnerabilities.""" def test_search_sql_injection(self, client): """Search should be safe from SQL injection.""" payloads = [ "'; DROP TABLE companies; --", "1' OR '1'='1", "1; DELETE FROM users WHERE '1'='1", "' UNION SELECT * FROM users --", "admin'--", ] for payload in payloads: response = client.get(f'/search?q={payload}') # Should not crash - 200, 302 (redirect), or 400 are acceptable assert response.status_code in [200, 302, 400], f"Unexpected status for SQL injection test: {response.status_code}" def test_login_sql_injection(self, client): """Login should be safe from SQL injection.""" payloads = [ ("admin' OR '1'='1", "anything"), ("admin'--", "anything"), ("' OR 1=1--", "' OR 1=1--"), ] for email, password in payloads: response = client.post('/login', data={ 'email': email, 'password': password, }) # Should not log in with injection assert response.status_code in [200, 302, 400] # If 200, should show login page (not dashboard) class TestXSS: """Tests for Cross-Site Scripting vulnerabilities.""" def test_search_xss_escaped(self, client): """Search results should escape XSS payloads.""" payloads = [ '', '', '">', "javascript:alert('xss')", ] for payload in payloads: response = client.get(f'/search?q={payload}', follow_redirects=True) assert response.status_code == 200 # Payload should be escaped, not raw assert b'