You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description=> <<"Add X-Correlation-ID headers to response">>}.
41
-
```
42
-
43
-
The `pre_request` callback picks up or generates a correlation ID and adds it to both the response headers and the request map. `post_request` is a no-op here. The `State` argument is global plugin state — see [Custom Plugins](../going-further/plugins-cors.md) for details on managing it with `init/0` and `stop/1`.
15
+
Each callback receives `(Req, Env, Options, State)` and returns `{ok, Req, State}` to pass control to the next plugin. Plugins can also enrich the request map — adding keys like `json`, `params`, or `correlation_id` — so that later plugins and controllers can use them.
44
16
45
17
## Configuring plugins
46
18
@@ -56,7 +28,168 @@ Plugins are configured in `sys.config` under the `nova` application key:
56
28
57
29
Each plugin entry is a tuple: `{Phase, Module, Options}` where Phase is `pre_request` or `post_request`.
58
30
59
-
`nova_request_plugin` is a built-in plugin that handles request body decoding. The options map controls what it decodes.
31
+
## nova_request_plugin
32
+
33
+
This built-in plugin handles request body decoding and query string parsing. It supports three options:
|`request_correlation_header`|*(none — always generates)*| Header to read an existing correlation ID from. Cowboy lowercases all header names. |
116
+
|`logger_metadata_key`|`<<"correlation-id">>`| Key set in OTP logger process metadata |
117
+
118
+
The plugin:
119
+
120
+
1. Reads the correlation ID from the configured header, or generates a v4 UUID if missing
121
+
2. Sets the ID in OTP logger metadata (so all log messages for this request include it)
122
+
3. Adds an `x-correlation-id` response header
123
+
4. Stores the ID in the request map as `correlation_id`
124
+
125
+
Access it in your controller:
126
+
127
+
```erlang
128
+
show(#{correlation_id :=CorrId} =_Req) ->
129
+
logger:info("Handling request ~s", [CorrId]),
130
+
...
131
+
```
132
+
133
+
## nova_csrf_plugin
134
+
135
+
This plugin provides CSRF protection using the synchronizer token pattern. It generates a random token per session and validates it on state-changing requests.
136
+
137
+
```erlang
138
+
{pre_request, nova_csrf_plugin, #{}}
139
+
```
140
+
141
+
| Option | Default | Description |
142
+
|---|---|---|
143
+
|`field_name`|`<<"_csrf_token">>`| Form field name to check |
144
+
|`header_name`|`<<"x-csrf-token">>`| Header name to check (for AJAX) |
145
+
|`session_key`|`<<"_csrf_token">>`| Key used to store the token in the session |
146
+
|`excluded_paths`|`[]`| List of path prefixes to skip protection for |
147
+
148
+
### How it works
149
+
150
+
-**Safe methods** (GET, HEAD, OPTIONS): The plugin ensures a CSRF token exists in the session and injects it into the request map as `csrf_token`.
151
+
-**Unsafe methods** (POST, PUT, PATCH, DELETE): The plugin reads the submitted token from the form field or header and validates it against the session token. If the token is missing or doesn't match, the request is rejected with a 403 response.
152
+
153
+
### Template integration
154
+
155
+
In your ErlyDTL templates, include the token in forms as a hidden field:
The `csrf_token` variable is available because the plugin adds it to the request map, and Nova passes request map values to templates as template variables.
166
+
167
+
For AJAX requests, send the token in a header instead:
168
+
169
+
```javascript
170
+
fetch('/api/resource', {
171
+
method:'POST',
172
+
headers: {
173
+
'X-CSRF-Token': csrfToken,
174
+
'Content-Type':'application/json'
175
+
},
176
+
body:JSON.stringify(data)
177
+
});
178
+
```
179
+
180
+
### Excluding API paths
181
+
182
+
If you have API routes that use a different authentication scheme (e.g. bearer tokens), exclude them from CSRF checks:
183
+
184
+
```erlang
185
+
{pre_request, nova_csrf_plugin, #{
186
+
excluded_paths=> [<<"/api/">>]
187
+
}}
188
+
```
189
+
190
+
```admonish warning
191
+
`nova_request_plugin` must run **before** `nova_csrf_plugin` so that form params are parsed into the `params` key. Plugin order matters — list `nova_request_plugin` first.
192
+
```
60
193
61
194
## Setting up for our login form
62
195
@@ -74,11 +207,67 @@ With this setting, form POST data is decoded and placed in the `params` key of t
74
207
You can enable multiple decoders at once. We will add `decode_json_body => true` later when we build our [JSON API](../building-api/json-api.md).
75
208
```
76
209
77
-
## Built-in plugins
210
+
## Per-route plugins
211
+
212
+
So far we've configured plugins globally in `sys.config`. You can also set plugins per route group by adding a `plugins` key to the group map in your router:
Nova ships with several plugins. See the [Nova documentation](https://hexdocs.pm/nova/plugins.html) for the full list.
236
+
When `plugins` is set on a route group, it **overrides** the global plugin configuration for those routes. This lets you use JSON decoding for API routes and form decoding for HTML routes without conflict.
237
+
238
+
See [Custom Plugins and CORS](../going-further/plugins-cors.md) for more examples, including per-route CORS.
For now, the key one is `nova_request_plugin`— it handles JSON body decoding, URL-encoded body decoding, and multipart uploads.
270
+
Ordering matters: `nova_correlation_plugin` runs first so all subsequent log messages include the correlation ID. `nova_request_plugin`runs before `nova_csrf_plugin` so form params are available for token validation.
0 commit comments