Skip to content

Commit

Permalink
Add strict transport security (#2725)
Browse files Browse the repository at this point in the history
* enables strict transport security

* adds specs

---------

Co-authored-by: Sri Harsha <sriharsha.poosa@gmail.com>
  • Loading branch information
utkarsh7989 and sri49 authored Jul 24, 2024
1 parent 674053d commit 95679bc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
8 changes: 8 additions & 0 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class WelcomeController < ApplicationController
skip_before_action :require_login
before_action :set_cookie_attributes, only: [:index]

def show_hints
current_user.hints = !current_user.hints
Expand All @@ -12,4 +13,11 @@ def index; end
def form_template
# created for generic form template access at '/templates/form-template'
end

private

def set_cookie_attributes
response.headers['Set-Cookie'] = "_session_id=#{session.id}; SameSite=Strict; Secure=true; HttpOnly"
response.headers['Strict-Transport-Security'] = "max-age=31536000; includeSubDomains; preload"
end
end
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
config.force_ssl = true

# Use the lowest log level to ensure availability of diagnostic information
# when problems arise.
Expand Down
8 changes: 7 additions & 1 deletion spec/controllers/welcome_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
it "renders welcome index" do
expect(response).to render_template("index")
end

it "has Cookie attributes" do
expect(response.headers["Set-Cookie"]).to match(/SameSite=Strict/)
expect(response.headers["Set-Cookie"]).to match(/HttpOnly/)
expect(response.headers["Strict-Transport-Security"]).to match(/max-age=31536000; includeSubDomains; preload/)
end
end

context "when not signed in" do
Expand Down Expand Up @@ -59,4 +65,4 @@
get :index
expect(response).to have_http_status(:success)
end
end
end

0 comments on commit 95679bc

Please sign in to comment.