Click here to Skip to main content
15,886,740 members
Articles / XSS
Tip/Trick

Prevent XSS Attacks using Content Security Policy

Rate me:
Please Sign up or sign in to vote.
4.89/5 (4 votes)
21 Aug 2017CPOL2 min read 9.6K   3   3
The Content-Security-Policy meta-tag allows you to reduce the risk of XSS attacks by allowing you to define where resources can be loaded from, preventing browsers from loading data from any other locations.

Today, I am working my project and make a script that downloads the other scripts. To test script working fine, I'm trying to run this script in the console of opened website. I tried it on my own website, it works fine but when I try to run the script in Gmail website console, I got an error:

JavaScript
Refused to load the script 'http://abc.com/abc.js' 
because it violates the following Content Security Policy
?directive: "script-src https://clients4.google.com/insights/consumersurveys/
?https://www.google.com/js/bg/ 'self' 'unsafe-inline' 'unsafe-eval' 
?https://mail.google.com/_/scs/mail-static/ 
?https://hangouts.google.com/ https://talkgadget.google.com/
?https://*.talkgadget.google.com/ 
?https://www.googleapis.com/appsmarket/v2/installedApps/
?https://www-gm-opensocial.googleusercontent.com/gadgets/js/ 
?https://docs.google.com/static/doclist/client/js/ 
?https://www.google.com/tools/feedback/ 
?https://s.ytimg.com/yts/jsbin/ 
?https://www.youtube.com/iframe_api 
?https://ssl.google-analytics.com/ 
?https://apis.google.com/_/scs/abc-static/ 
?https://apis.google.com/js/ 
?https://clients1.google.com/complete/....".

I saw this error in console screen, so I tried to find out why Gmail doesn't allow me. In error, I found the Content security policy. This is the property who tells the browser which content needs to be download on a page.

The Content-Security-Policy meta-tag allows you to reduce the risk of XSS attacks by allowing you to define where resources can be loaded from, preventing browsers from loading data from any other locations. This makes it harder for an attacker to inject malicious code into your site.

To add content security policy on your site, what you have to do is to add a meta tag in a head for pages.

HTML
<meta http-equiv="Content-Security-Policy" 
content="default-src 'self'">

or in a server response (in web.config):

XML
<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Content-Security-Policy" value="default-src 'self';" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

The most common directives are:

  • default-src the default policy for loading JavaScript, images, CSS, fonts, AJAX requests, etc.
  • script-src defines valid sources for JavaScript files
  • style-src defines valid sources for CSS files
  • img-src defines valid sources for images
  • connect-src defines valid targets for to XMLHttpRequest (AJAX), WebSockets or EventSource. If a connection attempt is made to a host that's not allowed here, the browser will emulate a 400error.

There are others, but these are the ones you're most likely to need. You can control various operations in your application using content security policy like the sources from where your contents come to your website.

I know this one way to protect our website from XSS attack. if you have any XSS prevention technique, please share it on comments.

Thanks for reading! :)

Further Reading

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMy vote of 5 Pin
Bohdan Stupak11-Sep-17 0:32
professionalBohdan Stupak11-Sep-17 0:32 
QuestionWill it stop image/js/css to load? Pin
Debashis 1043365623-Aug-17 2:31
Debashis 1043365623-Aug-17 2:31 
AnswerRe: Will it stop image/js/css to load? Pin
Code Atom27-Aug-17 20:35
professionalCode Atom27-Aug-17 20:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.