Securityheaders

Securityheaders adds common security-related HTTP headers to responses with a single declaration, instead of hand-rolling output filter rules.

What it adds

By default, to every response that does not already set them:

Two further headers are opt-in (added only when you configure them):

Any header already set by the application is left untouched, so you can override a default per response.

Ordering

This is a response filter: it decorates responses produced by earlier extensions. Place it after the extension whose responses it should cover (Staticmod, Eliom, ...). If placed before, it sees no response and silently adds nothing.

Using as a library

Load OCamlfind package ocsigenserver.ext.securityheaders from your Dune file, and see the API documentation:

let _ =
   Ocsigen.Server.start
     [ Ocsigen.Server.host
         [ Staticmod.run ~dir:"static" ()
         ; Securityheaders.run () ]]

Configuration file

Load the extension and add the <securityheaders/> element to a host, after the elements producing the responses to decorate:

<extension findlib-package="ocsigenserver.ext.securityheaders"/>
...
<host hostfilter="*">
  <static dir="/path/to/the/local/directory" />
  <securityheaders/>
</host>

Each header can be customised or disabled with an attribute. The values "no", "none" and the empty string disable a header. HSTS and CSP are added only when their attribute is present:

<securityheaders
   frame-options="DENY"
   referrer-policy="no-referrer"
   nosniff="true"
   hsts="max-age=63072000; includeSubDomains"
   content-security-policy="default-src 'self'" />