|
| 1 | +"""Tests for XSS prevention in OAuth callback HTML responses.""" |
| 2 | + |
| 3 | +from auth.oauth_responses import ( |
| 4 | + create_error_response, |
| 5 | + create_server_error_response, |
| 6 | + create_success_response, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +class TestXSSPrevention: |
| 11 | + def test_error_response_escapes_script_tag(self): |
| 12 | + xss_payload = '<script>alert("xss")</script>' |
| 13 | + response = create_error_response(xss_payload) |
| 14 | + body = response.body.decode() |
| 15 | + assert "<script>alert" not in body |
| 16 | + assert "<script>" in body |
| 17 | + |
| 18 | + def test_error_response_escapes_html_entities(self): |
| 19 | + response = create_error_response('Test <b>bold</b> & "quotes"') |
| 20 | + body = response.body.decode() |
| 21 | + assert "<b>" not in body |
| 22 | + assert "<b>" in body |
| 23 | + assert "&" in body |
| 24 | + |
| 25 | + def test_success_response_escapes_user_display(self): |
| 26 | + xss_email = "<img src=x onerror=alert(1)>@evil.com" |
| 27 | + response = create_success_response(verified_user_id=xss_email) |
| 28 | + body = response.body.decode() |
| 29 | + # The raw <img> tag should not appear — only the escaped version |
| 30 | + assert "<img src=" not in body |
| 31 | + assert "<img src=x onerror=alert(1)>@evil.com" in body |
| 32 | + |
| 33 | + def test_success_response_normal_email_displays_correctly(self): |
| 34 | + response = create_success_response(verified_user_id="user@example.com") |
| 35 | + body = response.body.decode() |
| 36 | + assert "user@example.com" in body |
| 37 | + |
| 38 | + def test_success_response_none_user_shows_default(self): |
| 39 | + response = create_success_response(verified_user_id=None) |
| 40 | + body = response.body.decode() |
| 41 | + assert "Google User" in body |
| 42 | + |
| 43 | + def test_server_error_response_escapes_exception(self): |
| 44 | + xss_detail = "FileNotFoundError: /secret/path/<script>alert(1)</script>" |
| 45 | + response = create_server_error_response(xss_detail) |
| 46 | + body = response.body.decode() |
| 47 | + assert "<script>alert" not in body |
| 48 | + assert "<script>" in body |
| 49 | + |
| 50 | + def test_error_response_status_code(self): |
| 51 | + response = create_error_response("test", status_code=403) |
| 52 | + assert response.status_code == 403 |
| 53 | + |
| 54 | + def test_server_error_response_status_code(self): |
| 55 | + response = create_server_error_response("test") |
| 56 | + assert response.status_code == 500 |
| 57 | + |
| 58 | + def test_success_response_status_code(self): |
| 59 | + response = create_success_response("user@example.com") |
| 60 | + assert response.status_code == 200 |
0 commit comments