I want to modify the response header using transform rules so that CORs would allow certain domains and block others.
If I create a rule like this:
If hostname equals sub.domain.com
Set static access-control-allow-origin = *
This works.
But I don’t want to use a wildcard but rather instead only allow sub1.domain.com and sub2.domain.com. However, my problem is that if I use sub1.domain.com instead of * it doesn’t work. It seems that my “syntax” is incorrect. How can I add two subdomains to the “access-control-allow-origin”.
So the issue I have is not directly related to Cloudflare I guess as the header value is indeed set correctly. by Cloudflare.
The error in console I get is this:
The ‘Access-Control-Allow-Origin’ header contains multiple values ‘sub1.domain.com, sub2.domain.com’, but only one is allowed.
I guess this is outside of scope of support of Cloudflare community.
This can be achieved with Cloudflare. You can use Transform Rules to dynamically set the Access-Control-Allow-Origin header to the domain that sent the CORS request.
If (http.host eq "cors-target.example.com" and http.request.headers["origin"][0] in {"https://allowed-origin-1.example.com" "https://allowed-origin-2.example.com"}) then set dynamic Access-Control-Allow-Origin to http.request.headers["origin"][0].