• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » Add Content Security Policy (CSP) Header in Nginx With report-uri

By Abhishek Ghosh August 17, 2016 2:45 pm Updated on August 17, 2016

Add Content Security Policy (CSP) Header in Nginx With report-uri

Advertisement

In the previous guide, we talked about how to around HTTP Public Key Pinning (HPKP) Nginx With report-uri. Content Security Policy (CSP) header adds control over the resources to be loaded on modern browsers to increase security. Here is How to Add Content Security Policy Header in Nginx.

Table of Contents

  • 1 Introduction
  • 2 What is Content Security Policy (CSP) Header?
  • 3 How To Add Content Security Policy (CSP) Header in Nginx
  • 4 Content Security Policy Example

 

What is Content Security Policy (CSP) Header? Why We Need CSP?

 

As like the most current recommendations and Standards related to frontend security, Content Security Policy (CSP) is for HTTPS website. That should not be a concern anymore as Let’s Encrypt provides forever free SSL certificates and no longer inferior choice compared to the paid DV SSL certificates.

Advertisement

---

With the Content Security Policy (CSP) Header, web site administrators can use combination of directives that can be used to suit their web site’s needs. Content source is a string which indicates a possible source from which content or type allowed to be loaded. Source list is a string specifying Internet hosts by domain name (read FQDN) or IP address, URL scheme and/or port number. There is also optional report-uri used for CSP violation reports. The report-uri must be of different FQDN than that of the applied policy. Content Security Policy (CSP) specification is currently of version 2 (at the time of writing this guide) and W3C Candidate Recommendation, dated 21st July 2015.

Most of the blogging softwares like WordPress or content management system like Drupal, Joomla! needs Content Security Policy (CSP) Header as their backend is either LAMP server or LEMP server i.e. uses PHP as dynamic programming language and has a database. PHP and MySQL combination, unlike plain static HTML serving websites has vulnerabilities. It is not difficult to get access via PHP shell by the script kiddies. Technically risk increases when there is self hosted web form on a webpage. Ping, Trackback etc also increases risk. Obviously, the forum softwares also need Content Security Policy (CSP) Header. Content Security Policy (CSP) Header can decrease the chance of Javascript malware, XSS attacks to mention few of its original target.

However, Content Security Policy (CSP) Header is highly restrictive. Without proper understanding and testing, websites with Google AdSense Ads may end up getting all the Ads blocked. CORS header itself restrictive.

 

How To Add Content Security Policy (CSP) Header in Nginx With report-uri?

 

This is an example header output upon running curl -I followed by domain name :

Vim
1
Content-Security-Policy: default-src 'self' trusted.example.com;

to get the above response, we need to add the following to /etc/nginx/sites-available/default website configuration file or any modified named file in /etc/nginx/sites-available/ directory :

Vim
1
add_header Content-Security-Policy "default-src 'self' trusted.example.com;";

Note that ;"; ending. First semi-colon is for Content Security Policy (CSP), second is for Nginx. Also, website name is not enclosed inside ' '.

Reporting URI can be used with a free service like that report-uri.io as like described in our other similar topic – HTTP Public Key Pinning (HPKP) Nginx With report-uri.

Add-Content-Security-Policy-(CSP)-Header-in-Nginx-With-report-uri

 

Content Security Policy Example

 

The Content Security Policy header value is made up of one or more directives. Below are the authoritative source of Content Security Policy (CSP) directives :

Vim
1
2
3
4
5
6
7
8
# mozilla
https://developer.mozilla.org/en/docs/Web/Security/CSP/CSP_policy_directives
https://hacks.mozilla.org/2016/02/implementing-content-security-policy/
# w3c
http://www.w3.org/TR/2012/CR-CSP-20121115/
# others
http://caniuse.com/contentsecuritypolicy2
https://content-security-policy.com

The interesting directives which are holding promise for higher security on client side are –

upgrade-insecure-requests directive instructs user agents to automatically upgrade all insecure resource requests from their pages to secure variants. The URL will be rewritten before the request is made, meaning that no insecure requests will hit the network. Example :

Vim
1
Content-Security-Policy: upgrade-insecure-requests

This is a bigger topic :

Vim
1
https://w3c.github.io/webappsec/specs/upgrade/

report-uri directive instructs the user agent to report attempts to violate the Content Security Policy. These violation reports consist of JSON documents sent via an HTTP POST request to the specified URI.

‘unsafe-eval’ allows the use of eval() and similar methods for creating code from strings. This must be used after understanding the legit Javascripts. In other words we do not recommend blindly add it.

reflected-xss directive instructs the user agent to activate or deactivate any heuristics used to filter or block reflected cross-site scripting attacks with any of the values – allow, block, filter. Allow is expected for co-operation.

Vim
1
add_header Content-Security-Policy "upgrade-insecure-requests; ";

Tagged With content security policy header , nginx content security policy , Content-Security-Policy nginx , content-security-policy header , content security policy nginx , nginx csp , nginx add_header Content-Security-Policy , add_header Content-Security-Policy , csp examples drupal nginx ,  Content­Security­Policy header

This Article Has Been Shared 218 Times!

Facebook Twitter Pinterest
Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Add Content Security Policy (CSP) Header in Nginx With report-uri

  • HHVM WordPress (Nginx Ubuntu Server) Tweaks

    Here are some special HHVM Wordpress (Nginx Ubuntu Server) Tweaks for Page Speed optimization, Compatibility of WordPress Themes and Plugins.

  • Steps To Install Nginx Plus on Ubuntu Server (HP Cloud)

    Here Are the Steps To Install Nginx Plus on Ubuntu Server Running on HP Cloud. Nginx Plus is the Paid Version of Nginx with Extra Features.

  • Zend Opcache Optimization for Nginx Ubuntu (HP Cloud)

    Here is Full Guide to Zend Opcache Optimization for Nginx PHP5-FPM for Ubuntu Server Running on HP Cloud. We Suggest to Use Zend Opcache over APC.

  • Enable Nginx PHP-FPM Status Page (Ubuntu, HP Cloud)

    Here is How To Enable Nginx PHP-FPM Status Page on Ubuntu Server Instance Running on HP Cloud. Enabling Ping Has Difference With Enabling Status.

  • Nginx WWW to No WWW & HTTP to HTTPS

    Basic, Yet Many New Users Fight With Nginx WWW to No WWW & HTTP to HTTPS Rules on Cloud Server Instance Running Debian/Ubuntu GNU/Linux.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (22.1K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Why You Need A Big Data Consultant May 15, 2022
  • The Connection Between AI And Online Slots May 13, 2022
  • How To Choose Your Niche As An Instagram Influencer May 12, 2022
  • How to Choose a Powerful Domain Name for Your New Venture May 12, 2022
  • How To Leverage Big Data In Social Media May 11, 2022

About This Article

Cite this article as: Abhishek Ghosh, "Add Content Security Policy (CSP) Header in Nginx With report-uri," in The Customize Windows, August 17, 2016, May 16, 2022, https://thecustomizewindows.com/2016/08/add-content-security-policy-csp-header-nginx-report-uri/.

Source:The Customize Windows, JiMA.in

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Privacy Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2022 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy