Repository navigation
CSRF Protection
Laravex protects all state-changing requests (anything that is not GET, HEAD, or OPTIONS) against Cross-Site Request Forgery attacks using the VerifyCsrfToken middleware, which is included in the "web" Middleware group by default.
- When a
GETorHEADrequest arrives,VerifyCsrfTokenensures a random token is stored in the session under the key"_token". If none exists it generates one withstr_random(40). - For
POST,PUT,PATCH, andDELETErequests the middleware checks that a token submitted with the request matches the session token using a constant-time comparison (crypto.hash_equals). - If the token is missing or does not match the response is a
419 Page Expired.
Use the @csrf Blade directive inside any HTML form to emit a hidden input field:
<form method="POST" action="/posts">
@csrf
<!-- renders: <input type="hidden" name="_token" value="<token>"> -->
...
</form>You can also output the raw token value with {{ csrf_token() }} in a Blade template, which is useful for embedding the token in <meta> tags for JavaScript clients:
<meta name="csrf-token" content="{{ csrf_token() }}">Axios and other HTTP clients can read the token from the X-CSRF-TOKEN header or the X-XSRF-TOKEN cookie. VerifyCsrfToken accepts the token from any of the following locations (checked in order):
-
request.params["_token"](form field) -
X-CSRF-TOKENrequest header -
X-XSRF-TOKENrequest header
Certain paths — such as webhook receivers — need to be exempt from CSRF checks. Configure exclusions in your application config or via Application.get_env:
# config/config.exs (or any environment config)
config :laravex, csrf_except: [
"/stripe/webhook",
"/github/webhook"
]The paths "/" and "/up" (the health-check endpoint) are always excluded automatically.
HTML forms only support GET and POST. To submit a PUT, PATCH, or DELETE request from a form, use the @method directive alongside @csrf:
<form method="POST" action="/posts/1">
@csrf
@method('DELETE')
<button type="submit">Delete Post</button>
</form>The MethodOverride middleware (part of the "web" group) reads the _method field and updates request.method accordingly before the route is dispatched.
Laravex — Laravel-faithful application skeleton & runtime for Elixir.
GitHub
·
Docs
·
Limitations
·
Laravel Docs
Not affiliated with Laravel LLC or the Elixir core team. Built to feel like Laravel on the BEAM.
- Home
- Installation
- Configuration
- Directory-Structure
- Request-Lifecycle
- Service-Container
- Service-Providers
- Deployment
- Routing
- Middleware
- CSRF-Protection
- Controllers
- Requests
- Responses
- Views
- Blade-Templates
- Asset-Bundling
- Session
- Validation
- Error-Handling
- Logging
- Artisan-Console
- Broadcasting
- Cache
- Collections
- Events
- File-Storage
- Helpers
- HTTP-Client
- Localization
- Notifications
- Pipeline
- Queues
- Rate-Limiting
- Strings
- Task-Scheduling