Skip to content

Commit 819e91e

Browse files
committed
Added doc for csrf
1 parent 7f4ecbe commit 819e91e

File tree

2 files changed

+65
-23
lines changed

2 files changed

+65
-23
lines changed

docs/overview/middleware.md

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,7 @@ Actions that can be performed by middleware functions:
3939
- End the request-response cycle if need be
4040
- Each middleware class or function must call `app` or `call_next` respectively else the request will be left without response
4141

42-
## **Dependency Injection**
43-
This is still feature is still in progress
44-
45-
```python
46-
import typing as t
47-
from starlette.types import ASGIApp
48-
from ellar.di import injectable
49-
50-
51-
@injectable
52-
class MyCustomService:
53-
pass
54-
5542

56-
class EllarASGIMiddlewareStructure:
57-
def __init__(self, app: ASGIApp, service: MyCustomService, **other_options: t.Any):
58-
self.app = app
59-
self.options = other_options
60-
self.custom_service = service
61-
62-
```
6343
## **Application Middleware**
6444
Ellar applies some ASGI middleware necessary for resource protection, error handling, and context management.
6545
They include:
@@ -69,9 +49,11 @@ They include:
6949
- **`RequestServiceProviderMiddleware`**: - This inherits from `ServerErrorMiddleware`. It provides DI context during request and
7050
also ensures that application exceptions may return a custom 500 page, or display an application traceback in DEBUG mode.
7151
- **`RequestVersioningMiddleware`**: This computes resource versioning info from request object based on configured resource versioning scheme at the application level.
72-
- **`ExceptionMiddleware`**: - Adds exception handlers, so that particular types of expected exception cases can be associated with handler functions. For example raising `HTTPException(status_code=404)` within an endpoint will end up rendering a custom 404 page.
52+
- **`ExceptionMiddleware`**: - Adds exception handlers, so that some common exception raised with the application can be associated with handler functions. For example raising `HTTPException(status_code=404)` within an endpoint will end up rendering a custom 404 page.
53+
- **`SessionMiddleware`**: controls session state using the session strategy configured in the application.
54+
- **`IdentityMiddleware`**: controls all registered authentication schemes and provides user identity to all request
7355

74-
## Applying Middleware
56+
## **Applying Middleware**
7557
Middleware can be applied through the application `config` - `MIDDLEWARES` variable.
7658

7759
Let's apply some middleware in our previous project. At the project root level, open `config.py`.
@@ -95,6 +77,36 @@ class DevelopmentConfig(BaseConfig):
9577
!!! Hint
9678
This is how to apply any `ASGI` middlewares such as `GZipMiddleware`, `EllarASGIMiddlewareStructure`, and others available in the `Starlette` library.
9779

80+
## **Dependency Injection**
81+
In section above, we saw how middleware are registered to the application. But what if the middleware class depends on other services, how then should we configure it?
82+
The `Middleware` does all the work.
83+
84+
For example, lets modify the `GZipMiddleware` class and make it depend on `Config` service.
85+
```python
86+
from ellar.core import Config
87+
from ellar.core.middleware import GZipMiddleware, Middleware
88+
from ellar.common.types import ASGIApp
89+
90+
91+
class CustomGZipMiddleware(GZipMiddleware):
92+
def __init__(self, app: ASGIApp, config: Config, minimum_size: int = 500, compresslevel: int = 9):
93+
super().__init__(app, minimum_size, compresslevel)
94+
self._config = config
95+
96+
## And in Config.py
97+
...
98+
99+
class DevelopmentConfig(BaseConfig):
100+
DEBUG: bool = True
101+
# Application middlewares
102+
MIDDLEWARE: list[Middleware] = [
103+
Middleware(CustomGZipMiddleware, minimum_size=1000)
104+
]
105+
```
106+
107+
In the example above, `Middleware` that wraps `CustomGZipMiddleware` with ensure dependent classes in `CustomGZipMiddleware` are resolved when
108+
instantiating `CustomGZipMiddleware` object. As you see, the `config` value was not provided but will be injected during runtime.
109+
98110
## **Starlette Middlewares**
99111
Let's explore other Starlette middlewares and other third party `ASGI` Middlewares
100112

docs/security/csrf.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
1-
# Coming Soon
1+
# **CSRF or XSRF**
2+
CSRF or XSRF is a security vulnerability and attack method in web applications. It involves tricking a user's browser
3+
into sending unauthorized requests to a website where the user is authenticated, allowing attackers to perform actions on behalf of the user.
4+
5+
## **Available ASGI CSRF Middlewares**
6+
7+
- [Piccolo CSRF Middleware](https://piccolo-api.readthedocs.io/en/latest/csrf/usage.html)
8+
- [Starlette CSRF](https://pypi.org/project/starlette-csrf/)
9+
10+
These middlewares can be configured as every other asgi middleware as shown in middleware [docs](../../overview/middleware/#applying-middleware) to work in Ellar
11+
12+
For example, using [Starlette CSRF](https://pypi.org/project/starlette-csrf/) Middleware
13+
```python
14+
# config.py
15+
import typing as t
16+
from ellar.core.middleware import Middleware
17+
from starlette_csrf import CSRFMiddleware
18+
19+
class Development(BaseConfig):
20+
DEBUG: bool = True
21+
# Application middlewares
22+
MIDDLEWARE: t.Sequence[Middleware] = [
23+
Middleware(
24+
CSRFMiddleware,
25+
secret="__CHANGE_ME__",
26+
cookie_name='csrftoken',
27+
safe_methods={"GET", "HEAD", "OPTIONS", "TRACE"}
28+
)
29+
]
30+
31+
```

0 commit comments

Comments
 (0)