Raise an exception when accessing a domain as subdomain

We were returning `nil` in this case, meaning we would be accessing the
main tenant in this situation instead of returning "Not Found".
This commit is contained in:
Javi Martín
2022-12-19 15:12:08 +01:00
parent 0d18e25e99
commit 0dac6ead77
2 changed files with 7 additions and 3 deletions

View File

@@ -30,7 +30,11 @@ class Tenant < ApplicationRecord
host_domain = allowed_domains.find { |domain| host == domain || host.ends_with?(".#{domain}") } host_domain = allowed_domains.find { |domain| host == domain || host.ends_with?(".#{domain}") }
schema = host_without_www.sub(/\.?#{host_domain}\Z/, "").presence schema = host_without_www.sub(/\.?#{host_domain}\Z/, "").presence
schema unless find_by_domain(schema) if find_by_domain(schema)
raise Apartment::TenantNotFound
else
schema
end
end end
end end

View File

@@ -86,10 +86,10 @@ describe Tenant do
expect(Tenant.resolve_host("saturn.consul.dev")).to eq "saturn" expect(Tenant.resolve_host("saturn.consul.dev")).to eq "saturn"
end end
it "returns nil when a domain is accessed as a subdomain" do it "raises an exception when a domain is accessed as a subdomain" do
insert(:tenant, :domain, schema: "saturn.dev") insert(:tenant, :domain, schema: "saturn.dev")
expect(Tenant.resolve_host("saturn.dev.consul.dev")).to be nil expect { Tenant.resolve_host("saturn.dev.consul.dev") }.to raise_exception(Apartment::TenantNotFound)
end end
it "returns nested subdomains when there's a subdomain-type tenant with nested subdomains" do it "returns nested subdomains when there's a subdomain-type tenant with nested subdomains" do