Skip to content

Commit

Permalink
Only allowed hostnames can access the site
Browse files Browse the repository at this point in the history
This is a common thing that comes out of pentests, so is worth having across all of
our projects.
  • Loading branch information
pezholio committed Feb 19, 2021
1 parent 9c31c40 commit 4000637
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ ROLLBAR_ENV=development

# TODO: Replace `rails-template` with the name of the app.
DATABASE_URL=postgres://postgres@localhost:5432/rails-template-development

# TODO: Replace `example.com` with the canonical hostname of the app
CANONICAL_HOSTNAME=example.com

# TODO: Add a comma seperated list of any other hostnames you want to
# app to respond to and redirect to the canonical hostname, or delete
# this line completely
ADDITIONAL_HOSTNAMES=
9 changes: 9 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

# See https://github.com/rails/rails/issues/29893
# This only allows hosts the application can trust when using `url_for` and related helpers
if ENV["CANONICAL_HOSTNAME"].present?
hosts = []
hosts << ENV["CANONICAL_HOSTNAME"]
hosts += ENV["ADDITIONAL_HOSTNAMES"].split(",").map { |domain| domain } if ENV["ADDITIONAL_HOSTNAMES"].present?
config.hosts = hosts.compact
end

# Inserts middleware to perform automatic connection switching.
# The `database_selector` hash is used to pass options to the DatabaseSelector
# middleware. The `delay` is used to determine how long to wait after a write
Expand Down

0 comments on commit 4000637

Please sign in to comment.