Modern web development has many challenges, and of those security is both very important and often under-emphasized.

Hanami provides ways to secure from most common vulnerabilities. Security options can be configured in application.rb.

X-Frame-Options

X-Frame-Options is a HTTP header supported by modern browsers. It determines if a web page can or cannot be included via <frame> and <iframe> tags by untrusted domains.

Web applications can send this header to prevent Clickjacking attacks:

  1. # Denies all untrusted domains (default)
  2. security.x_frame_options 'DENY'
  1. # Allows iframes on example.com
  2. security.x_frame_options 'ALLOW-FROM https://example.com/'

X-Content-Type-Options

X-Content-Type-Options prevents browsers from interpreting files as something else than declared by the content type in the HTTP headers.

  1. # Will prevent the browser from MIME-sniffing a response away from the declared content-type (default)
  2. security.x_content_type_options 'nosniff'

X-XSS-Protection

X-XSS-Protection is a HTTP header to determine the behavior of the browser in case an XSS attack is detected.

  1. # Filter enabled. Rather than sanitize the page, when a XSS attack is detected,
  2. # the browser will prevent rendering of the page (default)
  3. security.x_xss_protection '1; mode=block'
  1. # Filter disabled
  2. security.x_xss_protection '0'
  1. # Filter enabled. If a cross-site scripting attack is detected, in order to stop the attack,
  2. # the browser will sanitize the page
  3. security.x_xss_protection '1'
  1. # The browser will sanitize the page and report the violation.
  2. # This is a Chromium function utilizing CSP violation reports to send details to a URI of your choice
  3. security.x_xss_protection '1; report=http://[YOURDOMAIN]/your_report_URI'

Content-Security-Policy

Content-Security-Policy (CSP) is a HTTP header supported by modern browsers. It determines trusted sources of execution for dynamic contents (JavaScript) or other web related assets: stylesheets, images, fonts, plugins, etc.

Web applications can send this header to mitigate Cross Site Scripting (XSS) attacks.

The default value allows images, scripts, AJAX, fonts and CSS from the same origin, and does not allow any other resources to load (eg object, frame, media, etc).

Inline JavaScript is NOT allowed. To enable it, please use: script-src ‘unsafe-inline’.

Default value is:

  1. security.content_security_policy %{
  2. form-action 'self';
  3. frame-ancestors 'self';
  4. base-uri 'self';
  5. default-src 'none';
  6. script-src 'self';
  7. connect-src 'self';
  8. img-src 'self' https: data:;
  9. style-src 'self' 'unsafe-inline' https:;
  10. font-src 'self';
  11. object-src 'none';
  12. plugin-types application/pdf;
  13. child-src 'self';
  14. frame-src 'self';
  15. media-src 'self'
  16. }