I'm looking at the accepted answer to this question. It recommends the following for making a CORS policy with whitelisted domains:
var whitelist = ['http://example1.com', 'http://example2.com']
var corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
}
}
It looks good, but I'm worried about a url like http://example1.com.evil.com
. As far as I know, this would be a valid url, and it contains a whitelist string, but it's not a whitelisted domain. Is this method of checking for a domain unsafe, or is there something that prevents a sneaky subdomain from circumventing the CORS policy?