module

Kemal::Shield

Kemal::Shield is a module that contains a collection of Kemal handlers. These handlers sets/unsets different HTTP response headers adding an extra layer of protection.

Kemal::Shield.activate # => Adds all the handlers

It is also possible to add just the handlers that you are interested in.

add_handler Kemal::Shield::XPoweredBy.new # => Removes the X-Powered-By header
add_handler Kemal::Shield::XXSSProtection.new # => Sets X-XSS-Protection to "0"

The different headers can be configured in the same way as Kemal:

Kemal::Shield.config do |config|
  config.csp_on = true
  config.hide_powered_by = true
  config.no_sniff = true
  config.referrer_policy = ["no-referrer"]
  config.x_xss_protection = false
end

Constants

HANDLERS = [] of Shield::Handler.class
VERSION = "0.4.0"

Class methods

activate

Adds a collection of Kemal::Shield::Handler.

Kemal::Shield.activate
Source
add_handler(handler : Shield::Handler)

Adds a Kemal::Shield::Handler.

class CustomHandler < Kemal::Shield::Handler
  def call(context)
    # code ...
    call_next context
  end
end

Kemal::Shield.add_handler CustomHandler.new

A Kemal::Shield::DublicateHandlerError is raised if dublicate handlers are added.

Kemal::Shield.add_handler CustomHandler.new # => okay
Kemal::Shield.add_handler CustomHandler.new # => raises DublicateHandlerError
Source
config
Source
config
Source
deactivate

Removes all Kemal::Shield::Handler.

Kemal::Shield.deactivate
Source
remove_handler(handler : Shield::Handler.class)

Removes a Kemal::Shield::Handler.

Returns the removed handler if found, otherwise nil.

Kemal::Shield.activate

Kemal::Shield.remove_handler Kemal::Shield::ExpectCT # => Kemal::Shield::ExpectCT object
Kemal::Shield.remove_handler Kemal::Shield::ExpectCT # => nil
Source

Nested types