Skip to content

Commit b3f543a

Browse files
authored
Merge pull request #1803 from alphagov/configure-config-hosts
Add config.hosts configuration to production
2 parents 7fafb07 + 70963d1 commit b3f543a

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

config/environments/production.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "active_support/core_ext/integer/time"
2+
require_relative "../../lib/host_patterns"
23

34
Rails.application.configure do
45
# Settings specified here will take precedence over those in config/application.rb.
@@ -72,13 +73,10 @@
7273
config.active_support.report_deprecations = false
7374

7475
# Enable DNS rebinding protection and other `Host` header attacks.
75-
# config.hosts = [
76-
# "example.com", # Allow requests from example.com
77-
# /.*\.example\.com/ # Allow requests from subdomains like `www.example.com`
78-
# ]
79-
#
76+
config.hosts = HostPatterns.allowed_host_patterns
77+
8078
# Skip DNS rebinding protection for the default health check endpoint.
81-
# config.host_authorization = { exclude: ->(request) { request.path == "/up" } }
79+
config.host_authorization = { exclude: ->(request) { request.path == "/up" } }
8280

8381
# Set ActiveRecord Encryption keys
8482
if ENV.key? "KMS_KEY_ID"

lib/host_patterns.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module HostPatterns
2+
DEFAULT_HOST_PATTERNS = [
3+
/submit\.forms\.service\.gov\.uk/,
4+
/submit\.[^.]*\.forms\.service\.gov\.uk/,
5+
/submit\.internal.[^.]*\.forms\.service\.gov\.uk/,
6+
/pr-[^.]*\.submit\.review\.forms\.service\.gov\.uk/,
7+
].freeze
8+
9+
def self.allowed_host_patterns
10+
additional_patterns = ENV.fetch("ALLOWED_HOST_PATTERNS", "").split(",").map { |pattern| Regexp.new(pattern.strip) }
11+
12+
[*DEFAULT_HOST_PATTERNS, *additional_patterns]
13+
end
14+
end

0 commit comments

Comments
 (0)