Skip to content

Commit 0a303fb

Browse files
authored
Merge pull request #2666 from alphagov/fix-redirect-validation
Allow external redirects without a subdomain
2 parents a3720a6 + cb5b205 commit 0a303fb

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

app/validators/routes_and_redirects_validator.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class RoutesAndRedirectsValidator < ActiveModel::Validator
22
EXTERNAL_HOST_ALLOW_LIST = %w[
3-
.caa.co.uk
4-
.gov.uk
5-
.judiciary.uk
6-
.moneyhelper.org.uk
7-
.nationalhighways.co.uk
8-
.nhs.uk
9-
.police.uk
10-
.ukri.org
3+
caa.co.uk
4+
gov.uk
5+
judiciary.uk
6+
moneyhelper.org.uk
7+
nationalhighways.co.uk
8+
nhs.uk
9+
police.uk
10+
ukri.org
1111
].freeze
1212

1313
def validate(record, base_path: nil)
@@ -176,7 +176,10 @@ def internal?(destination)
176176
end
177177

178178
def government_domain?(host)
179-
host.end_with?(*EXTERNAL_HOST_ALLOW_LIST)
179+
return true if EXTERNAL_HOST_ALLOW_LIST.include?(host)
180+
181+
host_allow_list_for_subdomains = EXTERNAL_HOST_ALLOW_LIST.map { |allowed_host| ".#{allowed_host}" }
182+
host.end_with?(*host_allow_list_for_subdomains)
180183
end
181184

182185
def invalid_destination?(destination)

spec/support/routes_and_redirects_validator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@
226226
http://new-vat-rates.campaignjservicepgov.uk/path/to/your/new/vat-rates
227227
https://fakesite.net/.new-vat-rates.campaign.gov.uk/path/to/your/new/vat-rates
228228
ftp://new-vat-rates.campaign.gov.uk/
229+
https://evilgov.uk/
229230
].each do |destination|
230231
edition.redirects = [{ path: "#{subject.base_path}/foo", type: "exact", destination: }]
231232

@@ -244,6 +245,7 @@
244245
https://etl.beis.gov.uk/
245246
https://www.nhs.uk/
246247
https://www.ukri.org/
248+
https://nationalhighways.co.uk/
247249
https://www.nationalhighways.co.uk/
248250
https://www.police.uk/
249251
].each do |destination|

0 commit comments

Comments
 (0)