SlideShare a Scribd company logo
Defeating cross-site scripting
  with Content Security Policy




François Marier – @fmarier
what is a cross-site scripting
    (aka “XSS”) attack?
Defeating Cross-Site Scripting with Content Security Policy (updated)
Defeating Cross-Site Scripting with Content Security Policy (updated)
Defeating Cross-Site Scripting with Content Security Policy (updated)
Defeating Cross-Site Scripting with Content Security Policy (updated)
preventing XSS attacks
print <<<EOF
<html>

<h1>$title</h1>

</html>
EOF;
$title = escape($title);

print <<<EOF
<html>

<h1>$title</h1>

</html>
EOF;
templating system
page.tpl:

 <html>
 <h1>{title}</h1>
 </html>

page.php:

 render(“page.tpl”, $title);
auto-escaping turned ON
page.tpl:

 <html>
 <h1>{title|raw}</h1>
 </html>

page.php:

 render(“page.tpl”, $title);
auto-escaping turned ON




  escaping always ON
the real problem:

browser default = allow all
Defeating Cross-Site Scripting with Content Security Policy (updated)
a way to get the browser
to enforce the restrictions
   you want on your site
$ curl --head http://example.com/


Content-Security-Policy:
  default-src 'self' ;
  img-src     'self' data ;
$ curl --head https://example.com/login


Content-Security-Policy:
  default-src 'self' ;
  img-src     'self' data ;
  frame-src     'self'
     https://login.persona.org ;
  script-src 'self'
     https://login.persona.org
$ curl --head http://fmarier.org/


Content-Security-Policy:
  default-src 'none' ;
  img-src     'self' ;
  style-src 'self' ;
  font-src    'self'
<object>, <applet> & <embed>
                 <script>
             <style> & <link>
                  <img>
   <audio>, <video>, <source> & <track>
           <frame> & <iframe>
                @font-face

WebSocket, EventSource, & XMLHttpRequest
>= 10
what does a CSP-enabled
   website look like?
Defeating Cross-Site Scripting with Content Security Policy (updated)
Defeating Cross-Site Scripting with Content Security Policy (updated)
unless explicitly allowed by your policy

  inline scripts are not executed
Defeating Cross-Site Scripting with Content Security Policy (updated)
Defeating Cross-Site Scripting with Content Security Policy (updated)
unless explicitly allowed by your policy

external resources are not loaded
preparing your website for CSP
   (aka things you can do today)
eliminate inline scripts and styles
<script>
do_stuff();
</script>
<script src=”do_stuff.js”>

</script>
eliminate javascript: URIs
<a href=”javascript:go()”>
Go!
</a>
<a id=”go-button” href=”#”>
Go!
</a>
var button =
  document.getElementById('go-button');
button.onclick = go;
rolling out CSP
start with a loose policy
default-src 'self' *.example.com data;
default-src 'self' *.example.com data;

options unsafe-inline
work towards a stricter policy
default-src 'self';

img-src 'self' static.example.com data;

style-src static.example.com;

script-src static.example.com
use the reporting mode
Content-Security-Policy-Report-Only:
  default-src 'none' ;
  report-uri http://example.com/report.cgi
{

    "csp-report": {

        "document-uri": "http://example.com/page.html",

        "referrer": "http://evil.example.com/haxor.html",

        "blocked-uri": "http://evil.example.com/foo.png",

        "violated-directive": "default-src 'none'",

        "original-policy": "default-src 'none' ... "

    }

}
add headers in web server config
<Location /some/page>

 Header set Content-Security-Policy
   "default-src 'self' ;
    script-src 'self' http://example.org"

</Location>
not a
replacement
 for proper
XSS hygiene
great tool to
 increase the
   depth of
your defenses
Spec:
http://www.w3.org/TR/CSP/




HOWTO:
https://developer.mozilla.org/en/Security/CSP




@fmarier                              http://fmarier.org
100 %
              FREE!


  bonus
HTTP header
Defeating Cross-Site Scripting with Content Security Policy (updated)
Defeating Cross-Site Scripting with Content Security Policy (updated)
Defeating Cross-Site Scripting with Content Security Policy (updated)
wouldn't it be nice if the browser...
...blocked all HTTP requests there?
HTTP Strict
Transport Security
$ curl --head https://login.persona.org

HTTP/1.1 200 OK
Vary: Accept-Encoding,Accept-Language
Cache-Control: public, max-age=0
Content-Type: text/html; charset=utf8
Strict-Transport-Security: max-age= 2592000
Date: Thu, 16 Aug 2012 03:29:19 GMT
ETag: "2943768d6a45793897e83bf8804cd711"
Connection: keep-alive
X-Frame-Options: DENY
Content-Length: 5374
Defeating Cross-Site Scripting with Content Security Policy (updated)
HTTPS only site   turn HSTS on
Spec:
http://www.w3.org/TR/CSP/
https://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec



HOWTO:
https://developer.mozilla.org/en/Security/CSP
https://developer.mozilla.org/en/Security/HTTP_Strict_Transport_Security




@fmarier                                http://fmarier.org
Photo credits:
Biohazard wallpaper: http://www.flickr.com/photos/rockyx/4273385120/

Under Construction: https://secure.flickr.com/photos/aguichard/6864586905/

Castle walls: https://secure.flickr.com/photos/rdale/585105348/

Wash hands: https://secure.flickr.com/photos/hygienematters/4504612019/




                       Copyright © 2012 François Marier
                       Released under the terms of the Creative Commons
                       Attribution Share Alike 3.0 Unported Licence

More Related Content

PDF
Content Security Policy
PDF
Integrity protection for third-party JavaScript
PDF
Integrity protection for third-party JavaScript
PDF
Security and Privacy on the Web in 2016
PDF
Preventing XSS with Content Security Policy
PDF
Securing your AngularJS Application
PDF
Why Traditional Web Security Technologies no Longer Suffice to Keep You Safe
PPTX
Java script, security and you - Tri-Cities Javascript Developers Group
Content Security Policy
Integrity protection for third-party JavaScript
Integrity protection for third-party JavaScript
Security and Privacy on the Web in 2016
Preventing XSS with Content Security Policy
Securing your AngularJS Application
Why Traditional Web Security Technologies no Longer Suffice to Keep You Safe
Java script, security and you - Tri-Cities Javascript Developers Group

What's hot (20)

PDF
Http security response headers
PDF
JavaScript Security
PPTX
Javascript Security
PDF
URL to HTML
PPTX
Phu appsec13
PDF
Defeating xss-and-xsrf-with-my faces-frameworks-steve-wolf
PDF
Modern Web Application Defense
PDF
Protecting Java EE Web Apps with Secure HTTP Headers
PDF
Web vulnerabilities
PDF
Java EE 6 Security in practice with GlassFish
PDF
HTTP Security Headers Every Java Developer Must Know
PPT
Web Apps Security
PPTX
Client-side JavaScript Vulnerabilities
PDF
HTTP Security Headers
PDF
CONFidence 2018: Defense-in-depth techniques for modern web applications and ...
ODP
Csrf not-all-defenses-are-created-equal
PDF
Abusing & Securing XPC in macOS apps
PDF
Developer's Guide to JavaScript and Web Cryptography
PPTX
Web Application Security in front end
PPT
Same Origin Policy Weaknesses
Http security response headers
JavaScript Security
Javascript Security
URL to HTML
Phu appsec13
Defeating xss-and-xsrf-with-my faces-frameworks-steve-wolf
Modern Web Application Defense
Protecting Java EE Web Apps with Secure HTTP Headers
Web vulnerabilities
Java EE 6 Security in practice with GlassFish
HTTP Security Headers Every Java Developer Must Know
Web Apps Security
Client-side JavaScript Vulnerabilities
HTTP Security Headers
CONFidence 2018: Defense-in-depth techniques for modern web applications and ...
Csrf not-all-defenses-are-created-equal
Abusing & Securing XPC in macOS apps
Developer's Guide to JavaScript and Web Cryptography
Web Application Security in front end
Same Origin Policy Weaknesses
Ad

Viewers also liked (20)

PPT
Reign Seven Nine; Web Designing India Company Profile
PPTX
Megha india ppt
PPTX
Global Software Solutions
PPS
Hacking Client Side Insecurities
PDF
Cisco IOS Attack & Defense - The State of the Art
PPS
Workshop on Wireless Security
PDF
Hack IBM Connections - Advance Use - Iframes & more...
PDF
Implications of Growing Differences in Life Expectancy Across Socioeconomic G...
PPTX
Building A Modern Security Policy For Social Media and Government
PDF
Top Ten Web Hacking Techniques (2010)
PDF
Implications of Differential Mortality for Analyses of Social Security Policy...
PPTX
Reflective and Stored XSS- Cross Site Scripting
PDF
Gartner Magic Quadrant for Secure Email Gateways 2014
PPT
Network security chapter 6 and 7 internet architecture
PPT
Network Security Primer
PDF
Saudi arabia's food security policy and nutrition
PDF
Security Policy Checklist
PPT
develop security policy
PPTX
A business driven approach to security policy management a technical perspec...
PDF
National Cyber Security Policy 2013 (NCSP)
Reign Seven Nine; Web Designing India Company Profile
Megha india ppt
Global Software Solutions
Hacking Client Side Insecurities
Cisco IOS Attack & Defense - The State of the Art
Workshop on Wireless Security
Hack IBM Connections - Advance Use - Iframes & more...
Implications of Growing Differences in Life Expectancy Across Socioeconomic G...
Building A Modern Security Policy For Social Media and Government
Top Ten Web Hacking Techniques (2010)
Implications of Differential Mortality for Analyses of Social Security Policy...
Reflective and Stored XSS- Cross Site Scripting
Gartner Magic Quadrant for Secure Email Gateways 2014
Network security chapter 6 and 7 internet architecture
Network Security Primer
Saudi arabia's food security policy and nutrition
Security Policy Checklist
develop security policy
A business driven approach to security policy management a technical perspec...
National Cyber Security Policy 2013 (NCSP)
Ad

Similar to Defeating Cross-Site Scripting with Content Security Policy (updated) (20)

PDF
Defeating Cross-Site Scripting with Content Security Policy
PPTX
Analysis of HTTP Security Headers in Turkey
PDF
Csp and http headers
PDF
Csp and http headers
PDF
DefCamp 2013 - Http header analysis
PDF
HTTP_Header_Security.pdf
PDF
Tsc summit #2 - HTTP Header Security
PDF
http security response headers for web security
PDF
Rails security: above and beyond the defaults
PDF
Content Security Policy - Lessons learned at Yahoo
PPTX
Lec4-WebClientSideExploitation.pptxdslkjhgfkjdshgfkjfhdkjg
PDF
Securing the client side web
PDF
Content Security Policy
PDF
Web Security - CSP & Web Cryptography
PDF
Content Security Policy (CSP)
PDF
Future of Web Security Opened up by CSP
PDF
W3C Content Security Policy
PDF
2015-04-25-content-security-policy
PPTX
CollabSphere SC 103 : Domino on the Web : Yes, It's (Probably) Hackable
PDF
Browser Wars 2019 - Implementing a Content Security Policy
Defeating Cross-Site Scripting with Content Security Policy
Analysis of HTTP Security Headers in Turkey
Csp and http headers
Csp and http headers
DefCamp 2013 - Http header analysis
HTTP_Header_Security.pdf
Tsc summit #2 - HTTP Header Security
http security response headers for web security
Rails security: above and beyond the defaults
Content Security Policy - Lessons learned at Yahoo
Lec4-WebClientSideExploitation.pptxdslkjhgfkjdshgfkjfhdkjg
Securing the client side web
Content Security Policy
Web Security - CSP & Web Cryptography
Content Security Policy (CSP)
Future of Web Security Opened up by CSP
W3C Content Security Policy
2015-04-25-content-security-policy
CollabSphere SC 103 : Domino on the Web : Yes, It's (Probably) Hackable
Browser Wars 2019 - Implementing a Content Security Policy

More from Francois Marier (20)

PDF
Security and Privacy settings for Firefox Power Users
PDF
Getting Browsers to Improve the Security of Your Webapp
PDF
Hardening Firefox for Security and Privacy
PDF
Privacy and Tracking Protection in Firefox
PDF
Security and Privacy on the Web in 2015
PDF
URL to HTML
PDF
Supporting Debian machines for friends and family
PDF
Outsourcing your webapp maintenance to Debian
PDF
Easy logins for Ruby web applications
PDF
Easy logins for JavaScript web applications
PDF
You're still using passwords on your site?
PDF
Killing Passwords with JavaScript
PDF
Securing the Web without site-specific passwords
PDF
Easy logins for PHP web applications
PDF
Persona: a federated and privacy-protecting login system for the whole Web
PDF
Taking the pain out of signing users in
PDF
Mozilla Persona for your domain
PDF
Passwords and freedom: can we lose the former and retain the latter?
PDF
Login de usuários: podemos fazer algo melhor que usar senhas ou serviços cent...
PDF
The problem with passwords on the web and what to do about it
Security and Privacy settings for Firefox Power Users
Getting Browsers to Improve the Security of Your Webapp
Hardening Firefox for Security and Privacy
Privacy and Tracking Protection in Firefox
Security and Privacy on the Web in 2015
URL to HTML
Supporting Debian machines for friends and family
Outsourcing your webapp maintenance to Debian
Easy logins for Ruby web applications
Easy logins for JavaScript web applications
You're still using passwords on your site?
Killing Passwords with JavaScript
Securing the Web without site-specific passwords
Easy logins for PHP web applications
Persona: a federated and privacy-protecting login system for the whole Web
Taking the pain out of signing users in
Mozilla Persona for your domain
Passwords and freedom: can we lose the former and retain the latter?
Login de usuários: podemos fazer algo melhor que usar senhas ou serviços cent...
The problem with passwords on the web and what to do about it

Recently uploaded (20)

PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Architecture types and enterprise applications.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Getting Started with Data Integration: FME Form 101
PDF
Hindi spoken digit analysis for native and non-native speakers
PPTX
Tartificialntelligence_presentation.pptx
PPTX
The various Industrial Revolutions .pptx
PDF
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
TLE Review Electricity (Electricity).pptx
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PPTX
1. Introduction to Computer Programming.pptx
PPTX
O2C Customer Invoices to Receipt V15A.pptx
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Architecture types and enterprise applications.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
observCloud-Native Containerability and monitoring.pptx
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Getting Started with Data Integration: FME Form 101
Hindi spoken digit analysis for native and non-native speakers
Tartificialntelligence_presentation.pptx
The various Industrial Revolutions .pptx
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
Final SEM Unit 1 for mit wpu at pune .pptx
1 - Historical Antecedents, Social Consideration.pdf
Zenith AI: Advanced Artificial Intelligence
TLE Review Electricity (Electricity).pptx
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
1. Introduction to Computer Programming.pptx
O2C Customer Invoices to Receipt V15A.pptx

Defeating Cross-Site Scripting with Content Security Policy (updated)