Host-Header Injection Protection
gem'rack-allowed_hosts'In config/application.rb, (if using Rails):
classMyApplication < Rails::Application
...
ifRails.env == 'production'require'rack/allowed_hosts'config.middleware.useRack::AllowedHostsdo# Allow root domain:allow'myapp.com'# Allow our subdomainsallow'www.myapp.com','app.myapp.com'# Allow any subdomain with a wildcardallow'*.myapp.com'# Include subdomain from a configuration variable:# ENV['ALLOWED_HOSTS'] can be a string or an array of strings.allowENV['ALLOWED_HOSTS']endendWildcards (*) can be placed anywhere in the host pattern, and used to match any string, even including ..
So *.mydomain.com would match the following hosts:
platform.mydomain.comwww.mydomain.comclient.app.mydomain.com
This pattern would not match the following hosts:
mydomain.com- this pattern should be included separately if neededmydomain.com.aumydomain.com.otherwebsite.com
Do not simply allow all hosts. This would defeat the purpose of using the middleware
allow'*'...or this...
allow'*.com'...or any of these... (will enable anyone to spoof with mydomain.com.maliciousdomain.com)
allow'mydomain.*'allow'mycomain.com.*'...or this (will not match anything)
allow'*-something.mydomain.com'