forked from soveran/cuba
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hsts plugin for setting Strict-Transport-Security header
- Loading branch information
1 parent
3017a35
commit 102926a
Showing
5 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen-string-literal: true | ||
|
||
# | ||
class Roda | ||
module RodaPlugins | ||
# The hsts plugin allows for easily configuring an appropriate | ||
# Strict-Transport-Security response header for the application: | ||
# | ||
# plugin :hsts | ||
# # Strict-Transport-Security: max-age=63072000; includeSubDomains | ||
# | ||
# plugin :hsts, preload: true | ||
# # Strict-Transport-Security: max-age=63072000; includeSubDomains; preload | ||
# | ||
# plugin :hsts, max_age: 31536000, subdomains: false | ||
# # Strict-Transport-Security: max-age=31536000 | ||
module Hsts | ||
# Ensure default_headers plugin is loaded first | ||
def self.load_dependencies(app, opts=OPTS) | ||
app.plugin :default_headers | ||
end | ||
|
||
# Configure the Strict-Transport-Security header. Options: | ||
# :max_age :: Set max-age in seconds (default is 63072000, two years) | ||
# :preload :: Set preload, so the domain can be included in HSTS preload lists | ||
# :subdomains :: Set to false to not set includeSubDomains. By default, | ||
# includeSubDomains is set to enforce HTTPS for subdomains. | ||
def self.configure(app, opts=OPTS) | ||
app.plugin :default_headers, RodaResponseHeaders::STRICT_TRANSPORT_SECURITY => "max-age=#{opts[:max_age]||63072000}#{'; includeSubDomains' unless opts[:subdomains] == false}#{'; preload' if opts[:preload]}".freeze | ||
end | ||
end | ||
|
||
register_plugin(:hsts, Hsts) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require_relative "../spec_helper" | ||
|
||
describe "default_headers plugin" do | ||
def app(opts={}) | ||
super(:bare) do | ||
plugin :hsts, opts | ||
route do |r| | ||
'' | ||
end | ||
end | ||
end | ||
|
||
it "sets appropriate headers for the response" do | ||
app | ||
req[1][RodaResponseHeaders::STRICT_TRANSPORT_SECURITY].must_equal "max-age=63072000; includeSubDomains" | ||
end | ||
|
||
it "supports :preload option" do | ||
app(preload: true) | ||
req[1][RodaResponseHeaders::STRICT_TRANSPORT_SECURITY].must_equal "max-age=63072000; includeSubDomains; preload" | ||
end | ||
|
||
it "supports subdomains: false option" do | ||
app(subdomains: false) | ||
req[1][RodaResponseHeaders::STRICT_TRANSPORT_SECURITY].must_equal "max-age=63072000" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters