Skip to content

Instantly share code, notes, and snippets.

Last active July 18, 2024 01:44
Show Gist options
  • Save ambroisemaupate/bce4b760405558f358ae to your computer and use it in GitHub Desktop.
Save ambroisemaupate/bce4b760405558f358ae to your computer and use it in GitHub Desktop.
Nginx CSP example
# config to don't allow the browser to render the page inside an frame or iframe
# and avoid clickjacking
# if you need to allow [i]frames, you can use SAMEORIGIN or even set an uri with ALLOW-FROM uri
add_header X-Frame-Options SAMEORIGIN;
# when serving user-supplied content, include a X-Content-Type-Options: nosniff header along with the Content-Type: header,
# to disable content-type sniffing on some browsers.
# currently suppoorted in IE > 8
# 'soon' on Firefox
add_header X-Content-Type-Options nosniff;
# This header enables the Cross-site scripting (XSS) filter built into most recent web browsers.
# It's usually enabled by default anyway, so the role of this header is to re-enable the filter for
# this particular website if it was disabled by the user.
add_header X-XSS-Protection "1; mode=block";
# with Content Security Policy (CSP) enabled(and a browser that supports it(,
# you can tell the browser that it can only download content from the domains you explicitly allow
# I need to change our application code so we can increase security by disabling 'unsafe-inline' 'unsafe-eval'
# directives for css and js(if you have inline css or js, you will need to keep it too).
# more:
add_header Content-Security-Policy "script-src 'self' 'unsafe-inline' 'unsafe-eval' * * *; frame-src 'self' * *; object-src 'self'";
Copy link

Thanks for this resource. Regarding the use of 'unsafe-inline', unfortunately some php web apps don't work properly without it. :-( Limesurvey, Moodle to name the two that I have had to use it on.

Copy link

How to write multiple CSP rules in http block?

Copy link

zar3bski commented Sep 9, 2021

How to write multiple CSP rules in http block?

You can can specify multiple CSP rules after globally disable permissive default-src with ;. For instance:

    add_header Content-Security-Policy "default-src 'none'; 
                                        script-src 'self';
                                        style-src 'self' ; 
                                        connect-src 'self'; 
                                        font-src 'self'; 
                                        object-src 'self'; 
                                        media-src 'self'; 
                                        frame-src 'self';";

Line breaks are tolerated by nginx conf parser, as long as it starts and ends with ". Machines don't mind but humans usually prefer readable configuration files

Copy link

oDinZu commented Dec 2, 2022

@zar3bski 😎 Thanks for sharing!

Copy link

oDinZu commented Dec 3, 2022

Since you have shared, I will do the same ❤️

These CSP config for nginx is more for securing public sites than internal use.

"We can also add “always” at the end of the nginx config to confirm nginx sends the header regardless of the response code. "

I still ain't super happy about the unsafe-inline and unsafe-eval, but at least you know what links are allowed as unsafe, plus if you are hosting JAMstack sites, the site(s) should be static.

Note: The following snippet of code is the same as @zar3bski code, but the default-src isn't needed according to Mozilla.

Content-Security-Policy: "default-src 'self'; font-src; frame-src 'self';"

This is what I have concluded with for the Nginx Content-Security-Policty; also, has some useful tid bits of information for learning and securing ones site:

add_header Content-Security-Policy      "connect-src 'self' *;
                                        font-src 'self' * *;
                                        frame-src 'self' * * * * *;
                                        script-src 'self' 'unsafe-inline' * 'unsafe-eval';
                                        style-src 'self' *;
                                        frame-ancestors 'self';
                                        img-src 'self' data:;
                                        manifest-src 'self';
                                        media-src 'self';
                                        object-src 'self';
                                        worker-src 'self';" always;

Copy link

oDinZu commented Dec 4, 2022

Also, it is important to note that all mobile web browsers besides Google Chrome didn't work because of line breaks in the CSP config.

After adding the configuration to a single line solved the problem. 😅

Copy link

hkn06tr commented Apr 5, 2024

First set src variable then use it:

set $src "bla bla very long bla bla";
add_header Content-Security-Policy $src;
add_header X-Content-Security-Policy $src;
add_header X-WebKit-CSP $src;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment