Commit e4b9478
committed
fix(security): render the CSRF 403 through the error renderer, not a hardcoded page
CsrfMiddleware emitted a hardcoded <!DOCTYPE html> 403 (and a bespoke JSON body)
that bypassed the application's configured error renderer, so the CSRF rejection
could not be themed or localized like every other 4xx.
CsrfMiddleware now content-negotiates its 403:
- JSON for API clients (Accept: application/json, or X-Requested-With:
XMLHttpRequest), preserving the prior XHR-as-JSON behaviour.
- HTML rendered via the application's configured ExceptionRendererInterface —
themed and localized like every other error page — when one is wired, falling
back to a minimal inline document otherwise.
The response is returned, not thrown. Throwing would let the Kernel's outer
catch build the error response after the middleware stack has unwound, stripping
the SecurityHeadersMiddleware headers off the 403; returning keeps the response
flowing back out through the stack so security headers still apply on rejection
(covered by SecurityPipelineTest::securityHeadersAreAppliedEvenOnCsrfRejection).
The renderer is wired by ExceptionHandlerWiring, which runs after SecurityWiring,
so SecurityWiring injects a lazy resolver closure (mirroring its own
templateEngineResolver pattern) that the middleware invokes at request time. The
optional constructor parameter is additive: callers that omit it get the minimal
fallback page, exactly as before.1 parent a83fc6c commit e4b9478
4 files changed
Lines changed: 116 additions & 9 deletions
File tree
- src
- Core/Wiring
- Security/Csrf
- tests/Unit/Security/Csrf
- tools/api
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
335 | 336 | | |
336 | 337 | | |
337 | 338 | | |
338 | | - | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
339 | 355 | | |
340 | 356 | | |
341 | 357 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| 15 | + | |
| 16 | + | |
14 | 17 | | |
15 | 18 | | |
16 | 19 | | |
| |||
42 | 45 | | |
43 | 46 | | |
44 | 47 | | |
45 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
46 | 54 | | |
47 | 55 | | |
48 | 56 | | |
49 | 57 | | |
50 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
51 | 65 | | |
52 | 66 | | |
53 | 67 | | |
| 68 | + | |
54 | 69 | | |
55 | 70 | | |
56 | 71 | | |
| |||
243 | 258 | | |
244 | 259 | | |
245 | 260 | | |
246 | | - | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
247 | 268 | | |
248 | 269 | | |
249 | 270 | | |
250 | | - | |
251 | | - | |
252 | | - | |
| 271 | + | |
253 | 272 | | |
254 | 273 | | |
255 | 274 | | |
| |||
258 | 277 | | |
259 | 278 | | |
260 | 279 | | |
261 | | - | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
262 | 288 | | |
263 | 289 | | |
264 | 290 | | |
265 | 291 | | |
266 | 292 | | |
267 | | - | |
| 293 | + | |
268 | 294 | | |
269 | 295 | | |
270 | 296 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| 20 | + | |
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
| |||
512 | 515 | | |
513 | 516 | | |
514 | 517 | | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
515 | 552 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9133 | 9133 | | |
9134 | 9134 | | |
9135 | 9135 | | |
| 9136 | + | |
| 9137 | + | |
| 9138 | + | |
| 9139 | + | |
| 9140 | + | |
| 9141 | + | |
| 9142 | + | |
| 9143 | + | |
| 9144 | + | |
| 9145 | + | |
| 9146 | + | |
| 9147 | + | |
9136 | 9148 | | |
9137 | 9149 | | |
9138 | 9150 | | |
| |||
9169 | 9181 | | |
9170 | 9182 | | |
9171 | 9183 | | |
| 9184 | + | |
| 9185 | + | |
| 9186 | + | |
| 9187 | + | |
| 9188 | + | |
| 9189 | + | |
| 9190 | + | |
| 9191 | + | |
9172 | 9192 | | |
9173 | 9193 | | |
9174 | 9194 | | |
9175 | 9195 | | |
9176 | 9196 | | |
9177 | 9197 | | |
9178 | 9198 | | |
| 9199 | + | |
| 9200 | + | |
| 9201 | + | |
| 9202 | + | |
| 9203 | + | |
9179 | 9204 | | |
9180 | 9205 | | |
9181 | 9206 | | |
| |||
43574 | 43599 | | |
43575 | 43600 | | |
43576 | 43601 | | |
43577 | | - | |
| 43602 | + | |
| 43603 | + | |
43578 | 43604 | | |
43579 | 43605 | | |
43580 | 43606 | | |
| |||
56172 | 56198 | | |
56173 | 56199 | | |
56174 | 56200 | | |
| 56201 | + | |
56175 | 56202 | | |
56176 | 56203 | | |
56177 | 56204 | | |
56178 | 56205 | | |
56179 | 56206 | | |
| 56207 | + | |
56180 | 56208 | | |
56181 | 56209 | | |
56182 | 56210 | | |
| |||
0 commit comments