kemal-csrf
Version, currently 0.5.05 versions
github.com/fridgerator/kemal-csrf
Add CSRF protection to your Kemal application
0 stars
0 dependents
License: MIT
Installation
# Add this to your shard.yml
dependencies:
kemal-csrf:
github: fridgerator/kemal-csrf
version: ~> 0.5.0Then run:
shards installshard.yml
- Crystal
0.25.0- License
- MIT
- Author
- Serdar Dogruyol
Dependencies
Runtime Dependencies
- kemal-session0.10.0github: kemalcr/kemal-session
Development Dependencies
- kemal0.23.0github: kemalcr/kemaldev
README
kemal-csrf
Adds CSRF protection to your Kemal application.
Requires a session middleware to be initialized first.
Installation
Add this to your application's shard.yml:
dependencies:
kemal-csrf:
github: kemalcr/kemal-csrf
Usage
Basic Use
require "kemal-csrf"
add_handler CSRF.new
You can also change the name of the form field, header name, the methods which don't need csrf,error message and routes which you don't want csrf to apply. All of these are optional
require "kemal-csrf"
add_handler CSRF.new(
header: "X_CSRF_TOKEN",
allowed_methods: ["GET", "HEAD", "OPTIONS", "TRACE"],
allowed_routes: ["/api/somecallback"],
parameter_name: "_csrf",
error: "CSRF Error"
)
If you need to have some logic within your error response, you can also pass it a proc (a pointer to a function)
require "kemal-csrf"
add_handler CSRF.new(
header: "X_CSRF_TOKEN",
allowed_methods: ["GET", "HEAD", "OPTIONS", "TRACE"],
allowed_routes: ["/api/somecallback"],
parameter_name: "_csrf",
error: ->myerrorhandler(HTTP::Server::Context)
)
def myerrorhandler(env)
if env.request.headers["Content-Type"]? == "application/json"
{"error" => "csrf error"}.to_json
else
"<html><head><title>Error</title><body><h1>You cannot post to this route without a valid csrf token</h1></body></html>"
end
end
Contributing
- Fork it ( https://github.com/kemalcr/kemal-csrf/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- sdogruyol Serdar Dogruyol - creator, maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.5.0- Tagged
- Jun 18, 2018
- Commit
a1d90ff1667d- Crystal
0.25.0- Indexed
- yes
Dependents
No indexed shard depends on this one yet.
Repository
github.com/fridgerator/kemal-csrf
Metadata
- Created
- Aug 16, 2026
- Updated
- Aug 16, 2026
- Synced
- Aug 16, 2026
- Versions
- 5