I want to apply a default CSP header in nginx - basically a catch all. However, I also want to be able to override the CSP header via PHP in certain scripts. So far I've been unable to find a way to do this and the result is I get 2 headers - the first from PHP and the second from Nginx, with the last header taking preference over the first.
In nginx I have:
add_header Content-Security-Policy "default-src 'self';";
Then there's a catch for the PHP files:
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 3600;
fastcgi_read_timeout 3600;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
I've tried playing around with fastcgi_param
to see if I can get the header back from PHP and only add_header if not set, as well as trying:
if ($http_content_security_policy = "") {
add_header Content-Security-Policy "default-src 'self';";
}
But so far not winning. I assume it's a timing or access issue in Nginx in terms of the PHP header values? Any help is appreciated.
Note: I'm using Azure web apps, so I don't think I can add any modules that aren't default for Nginx