-
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: OpenAPI plugins send CSRF request header (#3754)
* feat: Swagger sends CSRF request header * feat: RapiDoc sends CSRF request header * test: Add tests for Swagger & RapiDoc with CSRF * test: csrf config with httponly cookie
- Loading branch information
Showing
2 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from litestar import Litestar | ||
from litestar.config.csrf import CSRFConfig | ||
from litestar.openapi.config import OpenAPIConfig | ||
from litestar.openapi.plugins import RapidocRenderPlugin, SwaggerRenderPlugin | ||
from litestar.testing import TestClient | ||
|
||
rapidoc_fragment = ".addEventListener('before-try'," | ||
swagger_fragment = "requestInterceptor:" | ||
|
||
|
||
def test_rapidoc_csrf() -> None: | ||
app = Litestar( | ||
csrf_config=CSRFConfig(secret="litestar"), | ||
openapi_config=OpenAPIConfig( | ||
title="Litestar Example", | ||
version="0.0.1", | ||
render_plugins=[RapidocRenderPlugin()], | ||
), | ||
) | ||
|
||
with TestClient(app=app) as client: | ||
resp = client.get("/schema/rapidoc") | ||
assert resp.status_code == 200 | ||
assert resp.headers["content-type"] == "text/html; charset=utf-8" | ||
assert rapidoc_fragment in resp.text | ||
|
||
|
||
def test_swagger_ui_csrf() -> None: | ||
app = Litestar( | ||
csrf_config=CSRFConfig(secret="litestar"), | ||
openapi_config=OpenAPIConfig( | ||
title="Litestar Example", | ||
version="0.0.1", | ||
render_plugins=[SwaggerRenderPlugin()], | ||
), | ||
) | ||
|
||
with TestClient(app=app) as client: | ||
resp = client.get("/schema/swagger") | ||
assert resp.status_code == 200 | ||
assert resp.headers["content-type"] == "text/html; charset=utf-8" | ||
assert swagger_fragment in resp.text | ||
|
||
|
||
def test_plugins_csrf_httponly() -> None: | ||
app = Litestar( | ||
csrf_config=CSRFConfig(secret="litestar", cookie_httponly=True), | ||
openapi_config=OpenAPIConfig( | ||
title="Litestar Example", | ||
version="0.0.1", | ||
render_plugins=[RapidocRenderPlugin(), SwaggerRenderPlugin()], | ||
), | ||
) | ||
|
||
with TestClient(app=app) as client: | ||
resp = client.get("/schema/rapidoc") | ||
assert resp.status_code == 200 | ||
assert rapidoc_fragment not in resp.text | ||
|
||
resp = client.get("/schema/swagger") | ||
assert resp.status_code == 200 | ||
assert swagger_fragment not in resp.text |