I've been trying to configure CORS headers on my hosting company's LiteSpeed Web Server via .htaccess, but I've run into what seems like a bug with LiteSpeed's handling of environment variables. The hosting company upgraded my instance to the latest LSWS 5.2.7 to see if that would help, but the problem still exists.
The troublesome stanza from the .htaccess file is the following:
What this code is supposed to do is add appropriate CORS headers to the server's response when the request is for any file with a font file extension and the client has set an "Origin" header representing any subdomain of either domain1.com or domain2.com. There is more content in the .htaccess file than this, but everything else is working normally. This is the only section that does not seem to work properly.
For troubleshooting purposes, I have deleted everything else from the .htaccess file to eliminate the possibility of conflicting directives. I also set up a quick and dirty Apache 2.2 installation on a test box in our company's lab environment to compare the behavior of the LiteSpeed server against this web server.
I tested the server's response using curl on a separate Ubuntu 16.04 utility box, and found that the exact same .htaccess config works on Apache 2.2, but fails on LSWS 5.2.7.
In trying to debug this, I've noticed that the issue seems to have something to do with the "THE_ORIGIN" environment variable not getting assigned a value properly on the LiteSpeed server when the "SetEnvIf" statement is inside of the "FilesMatch" block.
I also tried simplifying my configuration directives to help with further troubleshooting. Here's a rather pedantic .htaccess config I threw into my LiteSpeed instance as well as my lab Apache server:
Here's the result from the lab Apache server:
And here's the result from my web host's LiteSpeed server:
Notice that LiteSpeed failed to assign a value to the environment variable inside of the "FilesMatch" block, but the same assignment worked outside of the block. That seems kind of buggy to me. Apache assigned both variables properly.
Can anyone shed some light onto what might be happening here? Thanks.
The troublesome stanza from the .htaccess file is the following:
Code:
# CORS allowances for domain1 & domain2 subdomains to access my server's custom web fonts
<FilesMatch "\.(otf|ttf|eot|woff)$">
SetEnvIf Origin "^http(s)?://(.+\.)?(domain1.com|domain2.com)$" THE_ORIGIN=$0
Header set Access-Control-Allow-Origin %{THE_ORIGIN}e env=THE_ORIGIN
</FilesMatch>
For troubleshooting purposes, I have deleted everything else from the .htaccess file to eliminate the possibility of conflicting directives. I also set up a quick and dirty Apache 2.2 installation on a test box in our company's lab environment to compare the behavior of the LiteSpeed server against this web server.
I tested the server's response using curl on a separate Ubuntu 16.04 utility box, and found that the exact same .htaccess config works on Apache 2.2, but fails on LSWS 5.2.7.
In trying to debug this, I've noticed that the issue seems to have something to do with the "THE_ORIGIN" environment variable not getting assigned a value properly on the LiteSpeed server when the "SetEnvIf" statement is inside of the "FilesMatch" block.
I also tried simplifying my configuration directives to help with further troubleshooting. Here's a rather pedantic .htaccess config I threw into my LiteSpeed instance as well as my lab Apache server:
Code:
# Test environment variable handling
SetEnvIf Host ".*" MY_OUTSIDE_ENV_VAR=Outside
<FilesMatch "\.otf$">
SetEnvIf Host ".*" MY_INSIDE_ENV_VAR=Inside
Header set X-My-Outside-Env-Var %{MY_OUTSIDE_ENV_VAR}e
Header set X-My-Inside-Env-Var %{MY_INSIDE_ENV_VAR}e
</FilesMatch>
Code:
ven42@git-build-test:~$ curl -I 'http://real-apache-test.mylabdomain.com/assets/fonts/ProximaNova-Regular.otf'
HTTP/1.1 200 OK
Date: Thu, 24 May 2018 01:50:00 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Tue, 22 May 2018 00:41:15 GMT
ETag: "40a4f-171cc-56cc0ad014724"
Accept-Ranges: bytes
Content-Length: 94668
X-My-Outside-Env-Var: Outside
X-My-Inside-Env-Var: Inside
Connection: close
Content-Type: application/vnd.oasis.opendocument.formula-template
Code:
ven42@git-build-test:~$ curl -I 'http://litespeed-test.mywebhost.com/assets/fonts/ProximaNova-Regular.otf'
HTTP/1.1 200 OK
Last-Modified: Tue, 22 May 2018 00:00:15 GMT
Content-Type: application/x-font-otf
Content-Length: 94668
Date: Thu, 24 May 2018 01:49:54 GMT
Accept-Ranges: bytes
X-My-Outside-Env-Var: Outside
X-My-Inside-Env-Var:
Connection: Keep-Alive
Notice that LiteSpeed failed to assign a value to the environment variable inside of the "FilesMatch" block, but the same assignment worked outside of the block. That seems kind of buggy to me. Apache assigned both variables properly.
Can anyone shed some light onto what might be happening here? Thanks.