| 1 |
from fastapi.openapi.docs import get_swagger_ui_html |
| 2 |
|
| 3 |
|
| 4 |
def test_init_oauth_html_chars_are_escaped(): |
| 5 |
xss_payload = "Evil</script><script>alert(1)</script>" |
| 6 |
html = get_swagger_ui_html( |
| 7 |
openapi_url="/openapi.json", |
| 8 |
title="Test", |
| 9 |
init_oauth={"appName": xss_payload}, |
| 10 |
) |
| 11 |
body = bytes(html.body).decode() |
| 12 |
|
| 13 |
assert "</script><script>" not in body |
| 14 |
assert "\\u003c/script\\u003e\\u003cscript\\u003e" in body |
| 15 |
|
| 16 |
|
| 17 |
def test_swagger_ui_parameters_html_chars_are_escaped(): |
| 18 |
html = get_swagger_ui_html( |
| 19 |
openapi_url="/openapi.json", |
| 20 |
title="Test", |
| 21 |
swagger_ui_parameters={"customKey": "<img src=x onerror=alert(1)>"}, |
| 22 |
) |
| 23 |
body = bytes(html.body).decode() |
| 24 |
assert "<img src=x onerror=alert(1)>" not in body |
| 25 |
assert "\\u003cimg" in body |
| 26 |
|
| 27 |
|
| 28 |
def test_normal_init_oauth_still_works(): |
| 29 |
html = get_swagger_ui_html( |
| 30 |
openapi_url="/openapi.json", |
| 31 |
title="Test", |
| 32 |
init_oauth={"clientId": "my-client", "appName": "My App"}, |
| 33 |
) |
| 34 |
body = bytes(html.body).decode() |
| 35 |
assert '"clientId": "my-client"' in body |
| 36 |
assert '"appName": "My App"' in body |
| 37 |
assert "ui.initOAuth" in body |