Skip to content

Commit 2823d2f

Browse files
authored
Merge pull request #25 from nativeapptemplate/harden_security_headers_and_cleanup
Harden security headers and cleanup
2 parents a2c61d7 + 5ac0549 commit 2823d2f

9 files changed

Lines changed: 48 additions & 47 deletions

File tree

Gemfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ gem "image_processing", "~> 1.12"
4141
# Security update
4242
gem "nokogiri", ">= 1.12.5"
4343

44-
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
45-
gem "rack-cors"
4644
gem "devise_token_auth", "~> 1.2", ">= 1.2.2"
4745
gem "jsonapi-serializer"
4846
gem "pundit"

Gemfile.lock

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,6 @@ GEM
310310
rack (3.2.5)
311311
rack-attack (6.8.0)
312312
rack (>= 1.0, < 4)
313-
rack-cors (3.0.0)
314-
logger
315-
rack (>= 3.0.14)
316313
rack-session (2.1.1)
317314
base64 (>= 0.1.0)
318315
rack (>= 3.0.0)
@@ -513,7 +510,6 @@ DEPENDENCIES
513510
puma (~> 7.0)
514511
pundit
515512
rack-attack
516-
rack-cors
517513
rails (~> 8.1)
518514
resend
519515
rubocop-rails-omakase

app/views/layouts/display.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<%# Be sure to add your own custom favicons %>
1919
<%= render "shared/favicons" %>
2020

21-
<link rel="preload" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" as="style" onload="this.onload=null;this.rel='stylesheet'">
21+
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap">
2222

2323
<%= yield :head %>
2424
</head>

app/views/layouts/errors.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<%# Be sure to add your own custom favicons %>
1919
<%= render "shared/favicons" %>
2020

21-
<link rel="preload" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" as="style" onload="this.onload=null;this.rel='stylesheet'">
21+
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap">
2222

2323
<%= yield :head %>
2424
</head>

app/views/layouts/minimal.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<%# Be sure to add your own custom favicons %>
1919
<%= render "shared/favicons" %>
2020

21-
<link rel="preload" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" as="style" onload="this.onload=null;this.rel='stylesheet'">
21+
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap">
2222

2323
<%= yield :head %>
2424
</head>

config/environments/production.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@
7979
config.active_record.dump_schema_after_migration = false
8080

8181
# Enable DNS rebinding protection and other `Host` header attacks.
82-
# config.hosts = [
83-
# "example.com", # Allow requests from example.com
84-
# /.*\.example\.com/ # Allow requests from subdomains like `www.example.com`
85-
# ]
82+
# Render automatically sets RENDER_EXTERNAL_HOSTNAME to the service's hostname
83+
# (e.g. nativeapptemplateapi.onrender.com). No manual configuration needed.
84+
# APP_HOST is for the custom domain (set in Render env vars).
85+
config.hosts = [
86+
ENV["RENDER_EXTERNAL_HOSTNAME"],
87+
ENV["APP_HOST"]
88+
].compact
8689
# Skip DNS rebinding protection for the default health check endpoint.
87-
# config.host_authorization = { exclude: ->(request) { request.path == "/up" } }
90+
config.host_authorization = {exclude: ->(request) { request.path == "/up" }}
8891
end

config/initializers/content_security_policy.rb

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@
44
# See the Securing Rails Applications Guide for more information:
55
# https://guides.rubyonrails.org/security.html#content-security-policy-header
66

7-
# Rails.application.configure do
8-
# config.content_security_policy do |policy|
9-
# policy.default_src :self, :https
10-
# policy.font_src :self, :https, :data
11-
# policy.img_src :self, :https, :data
12-
# policy.object_src :none
13-
# policy.script_src :self, :https
14-
# policy.style_src :self, :https
15-
# # Specify URI for violation reports
16-
# # policy.report_uri "/csp-violation-report-endpoint"
17-
# end
18-
#
19-
# # Generate session nonces for permitted importmap, inline scripts, and inline styles.
20-
# config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s }
21-
# config.content_security_policy_nonce_directives = %w(script-src style-src)
22-
#
23-
# # Automatically add `nonce` to `javascript_tag`, `javascript_include_tag`, and `stylesheet_link_tag`
24-
# # if the corresponding directives are specified in `content_security_policy_nonce_directives`.
25-
# # config.content_security_policy_nonce_auto = true
26-
#
27-
# # Report violations without enforcing the policy.
28-
# # config.content_security_policy_report_only = true
29-
# end
7+
Rails.application.configure do
8+
config.content_security_policy do |policy|
9+
policy.default_src :self
10+
policy.font_src :self, "https://fonts.gstatic.com"
11+
policy.img_src :self, :data
12+
policy.object_src :none
13+
policy.script_src :self
14+
policy.style_src :self, :unsafe_inline, "https://fonts.googleapis.com"
15+
policy.connect_src :self
16+
policy.frame_src :self
17+
policy.base_uri :self
18+
policy.form_action :self
19+
end
20+
21+
# Generate session nonces for permitted importmap and inline scripts.
22+
config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s }
23+
config.content_security_policy_nonce_directives = %w[script-src]
24+
25+
# Report violations without enforcing the policy.
26+
# config.content_security_policy_report_only = true
27+
end

config/initializers/devise_token_auth.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# By default, users will need to re-authenticate after 2 weeks. This setting
1111
# determines how long tokens will remain valid after they are issued.
12-
config.token_lifespan = 90.days
12+
config.token_lifespan = 30.days
1313

1414
# Limiting the token_cost to just 4 in testing will increase the performance of
1515
# your test suite dramatically. The possible cost value is within range from 4
@@ -18,7 +18,7 @@
1818

1919
# Sets the max number of concurrent devices per user, which is 10 by default.
2020
# After this limit is reached, the oldest tokens will be removed.
21-
config.max_number_of_devices = 100
21+
config.max_number_of_devices = 10
2222

2323
# Sometimes it's necessary to make several requests to the API at the same
2424
# time. In this case, each request in the batch will need to share the same

config/initializers/permissions_policy.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
# Define an application-wide HTTP permissions policy. For further
44
# information see: https://developers.google.com/web/updates/2018/06/feature-policy
55

6-
# Rails.application.config.permissions_policy do |policy|
7-
# policy.camera :none
8-
# policy.gyroscope :none
9-
# policy.microphone :none
10-
# policy.usb :none
11-
# policy.fullscreen :self
12-
# policy.payment :self, "https://secure.example.com"
13-
# end
6+
Rails.application.config.permissions_policy do |policy|
7+
policy.accelerometer :none
8+
policy.ambient_light_sensor :none
9+
policy.autoplay :none
10+
policy.camera :none
11+
policy.geolocation :none
12+
policy.gyroscope :none
13+
policy.magnetometer :none
14+
policy.microphone :none
15+
policy.midi :none
16+
policy.payment :none
17+
policy.usb :none
18+
policy.fullscreen :self
19+
end

0 commit comments

Comments
 (0)