SlideShare a Scribd company logo
Same Origin Policy
Cross-Origin Resource Sharing
Content Security Policy
subbul@gmail.com
Agenda
•
•
•
•

Need for SOP
How CORS help SOP
What is XSS?
How CSP helps preventing XSS
Why Same Origin Policy ?
• What if your personal data you are entering in a “Bank” page
in Browser is accessible to another Page in the browser
Instance
What is Same Origin Policy
• This is a Browser Mechanism to allow trusted pages/scripts
• To Prevent HTML/JS Application from different window, domain accessing the
DOM, data of Application current domain or “Origin”
• Thanks to Same Origin Policy (SOP), Browser prevents loading or blocks request
for DOM access, execution of script from “Origin/Domain” other than “Self”

• More Details
What are allowed in SOP?
• SOP cannot prevent cross site content inclusions (like images, scripts, css
from different domain
• http://www.google.com/page1 can access http://www.google.com/page2
• http://www.google.com/page1 cannot access http://www.yahoo.com as
the two pages belong to different domain
• <script> is allowed by SOP [file:// ??]
• In a http://www.mypage.com page, you can include<script src=
http://api.google.com/googleplus >.
• Google API page scripts are executed in “Mypage” domain, HTML
Application, it will still have access to “Mypage” DOM elements. So, if the
“Google API scripts” are compromised, it will have bad effect on the
“MyPage” (Will take it to XSS- Cross Site Scripting)
What is not allowed in SOP?
• AJAX (XHR) from One domain to another
• XHR request from “MyPage.com” to “Google.com”
• Why it is not allowed?
– Using AJAX you can download a malicious JS code and could spoil the
current page information or could derive information from current
page and send it over maliciously to remote pages
How to circumvent SOP
•
•
•
•

Simple suggestion DO NOT USE ( unless it’s the End of the World)
Document.domain
PostMessage
JSONP

• Right Way
– CORS (Cross-Origin Resource Sharing)
Cross-Origin Resource Sharing
• CORS is to overcome SOP for XHR
• Allowing Cross Origin Request from Domain A to Domain B using XHR
• Introduction of new HTTP Headers (Origin) from Server to make Browser
decide to Allow Cross-Origin request or not
• Use Pre-flight (handshake) OPTIONS request for methods other than
POST/GET to know if the server supports, allow-origin for your request

More Detail
How CORS works?
CORS HTTP Request/Response Headers
HTTP Request/
ResponseHeader

Parameter

Description

Example

Access-Control-AllowOrigin:

<origin> | *

Specifying a particular
“domain” is allowed
or “*” all

Access-Control-AllowOrigin:
http://mozilla.com

Access-Control-AllowCredentials

True| false

Request for cookie
along with request

Access-ControlRequest-Method

GET,POST

Request for supported
HTTP methods

Access-Control-AllowHeaders

Content-Type|
Custom-Header

Preflight-request
headers
CORS Server/Browser Request /Response Flow

http://www.html5rocks.com/static/images/cors_server_flowchart.png
XSS (Cross Site Scripting)
• Finding Vulnerability of Web Pages and
injecting and injecting malicious client
side- script .
• Types
– Non-Persistent (server Echo’s back your
request)
– Adding malicious scripts in HTML Forms,
HTTP Query from web browser during a
search request. If the “String” is not
formatted/escaped, the injected script
will be executed back in client browser.
– E.g.,
• Phishing Attacks,
• URL Shortens (bit.ly ) taking to
legitimate page and injecting their
“script” along with it
XSS (Cross Site Scripting)
– Persistent (Server stores the data
and script)
– Storing user provided “string” as is
without escaping the HTML, JS code
in Webserver and serving later to all
users will cause the malicious script
to execute on client browser
– Message Boards, which include Plain
Text and Scripts, later when another
user reloads the Message Board, the
malicious code executes and steals
user data
– Defacing web
servers, cookie/session stealing
Examples
• http://www.insecurelabs.org/Task/Rule1
• http://www.insecurelabs.org
• https://www.owasp.org/index.php/Testing_for_Reflected_Cros
s_site_scripting_(OWASP-DV-001)
How to Prevent XSS
•
•
•
•
•

Validation/Sanitization of ALL user inputs in a page
No inline please, keep it safe in a dedicated JS
Secure all input path, query string, file path etc
Don’t keep untrusted data in your HTML, JS
This is one of the reason, you find forms in organization preventing
<, > etc 
• https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Pre
vention_Cheat_Sheet
• And of course CSP (Content Security Policy)
Content Security Policy (CSP)
• It’s a policy how Browser/UserAgent adhere to as a directive from
HTTP Server in order to display, execute scripts
• New HTTP Headers introduced to enable CSP
• Content-Security-Policy: script-src 'self'
Trusted Source
https://abc.MyWebpage.com
Resource

Trusted Source
Content Security Policy
• If a malicious code is injected through XSS (added <script src=“hackedsite.com”>), browser
will detect and prevent
• More XSS prevention by
• 'unsafe-inline' prevents inline JavaScript and CSS
• 'unsafe-eval' prevents text-to-JavaScript mechanisms like eval
• Default-src “none” (Shut down any other script, img, media load beyond my own)
• Other resources which can be controlled by CSP are font-src,img-src etc
–
–
–
–

http://www.html5rocks.com/en/tutorials/security/content-security-policy/
http://erlend.oftedal.no/blog/csp/readiness/
http://people.mozilla.org/~bsterne/content-security-policy/details.html
https://wiki.mozilla.org/index.php?title=Security/CSP/Spec&oldid=133465
Thank You

More Related Content

PPT
Same origin policy
PDF
Javascript cross domain communication
PDF
Cross site calls with javascript - the right way with CORS
PPTX
Web Security - Cookies, Domains and CORS
PPTX
CORS - Enable Alfresco for CORS
PPTX
Misconfigured CORS, Why being secure isn't getting easier. AppSec USA 2016
PDF
Cross-domain requests with CORS
PDF
CORS and (in)security
Same origin policy
Javascript cross domain communication
Cross site calls with javascript - the right way with CORS
Web Security - Cookies, Domains and CORS
CORS - Enable Alfresco for CORS
Misconfigured CORS, Why being secure isn't getting easier. AppSec USA 2016
Cross-domain requests with CORS
CORS and (in)security

What's hot (20)

PPT
Breaking The Cross Domain Barrier
PDF
RESTful Web Services
PDF
Basic web architecture
PPTX
Evolution Of The Web Platform & Browser Security
PPTX
01. http basics v27
KEY
Modernizr, Yepnope, and Polyfills
PDF
Your rest api using laravel
PDF
Building Awesome APIs with Lumen
PDF
Cors kung fu
PDF
Cors michael
PPTX
Web Architecture
PDF
Basic Introduction About API Web Service
PDF
Design Web Service API by HungerStation
ODP
PHP BASIC PRESENTATION
PDF
REST in peace @ IPC 2012 in Mainz
KEY
REST Easy - Building RESTful Services in Zend Framework
PDF
Data normalization weaknesses
PDF
SOAP-based Web Services
PDF
Your first sitemap.xml and robots.txt implementation
PPTX
Robots.txt and Sitemap.xml Creation
Breaking The Cross Domain Barrier
RESTful Web Services
Basic web architecture
Evolution Of The Web Platform & Browser Security
01. http basics v27
Modernizr, Yepnope, and Polyfills
Your rest api using laravel
Building Awesome APIs with Lumen
Cors kung fu
Cors michael
Web Architecture
Basic Introduction About API Web Service
Design Web Service API by HungerStation
PHP BASIC PRESENTATION
REST in peace @ IPC 2012 in Mainz
REST Easy - Building RESTful Services in Zend Framework
Data normalization weaknesses
SOAP-based Web Services
Your first sitemap.xml and robots.txt implementation
Robots.txt and Sitemap.xml Creation
Ad

Similar to Browsers_SameOriginPolicy_CORS_ContentSecurityPolicy (20)

PDF
Going Beyond Cross Domain Boundaries (jQuery Bulgaria)
PPTX
Lesson 6 web based attacks
PPTX
Browser Security 101
PDF
Krzysztof Kotowicz - Hacking HTML5
PPTX
Browser Internals-Same Origin Policy
PPTX
Building Secure User Interfaces With JWTs (JSON Web Tokens)
PPTX
Browser Hacking For Fun and Profit | Null Bangalore Meetup 2019 | Divyanshu S...
PPTX
Building Secure User Interfaces With JWTs
PDF
Hacking HTML5 offensive course (Zeronights edition)
PPTX
JWT Authentication with AngularJS
PPTX
Web security for app developers
PPTX
Security vulnerabilities - 2018
PPTX
Conquering CORS. Taming Cross-Origin Resource Sharing.
PPTX
Lec4-WebClientSideExploitation.pptxdslkjhgfkjdshgfkjfhdkjg
PDF
CNIT 129S: 13: Attacking Users: Other Techniques (Part 1 of 2)
PPT
A privacy-preserving defense mechanism against attacks
PDF
Chrome extensions threat analysis and countermeasures
PDF
Building Client-Side Attacks with HTML5 Features
PDF
CNIT 129S: Ch 3: Web Application Technologies
Going Beyond Cross Domain Boundaries (jQuery Bulgaria)
Lesson 6 web based attacks
Browser Security 101
Krzysztof Kotowicz - Hacking HTML5
Browser Internals-Same Origin Policy
Building Secure User Interfaces With JWTs (JSON Web Tokens)
Browser Hacking For Fun and Profit | Null Bangalore Meetup 2019 | Divyanshu S...
Building Secure User Interfaces With JWTs
Hacking HTML5 offensive course (Zeronights edition)
JWT Authentication with AngularJS
Web security for app developers
Security vulnerabilities - 2018
Conquering CORS. Taming Cross-Origin Resource Sharing.
Lec4-WebClientSideExploitation.pptxdslkjhgfkjdshgfkjfhdkjg
CNIT 129S: 13: Attacking Users: Other Techniques (Part 1 of 2)
A privacy-preserving defense mechanism against attacks
Chrome extensions threat analysis and countermeasures
Building Client-Side Attacks with HTML5 Features
CNIT 129S: Ch 3: Web Application Technologies
Ad

Recently uploaded (20)

PPTX
Machine Learning_overview_presentation.pptx
PDF
Approach and Philosophy of On baking technology
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Encapsulation theory and applications.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Electronic commerce courselecture one. Pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Empathic Computing: Creating Shared Understanding
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Machine Learning_overview_presentation.pptx
Approach and Philosophy of On baking technology
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Machine learning based COVID-19 study performance prediction
Network Security Unit 5.pdf for BCA BBA.
“AI and Expert System Decision Support & Business Intelligence Systems”
20250228 LYD VKU AI Blended-Learning.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Encapsulation theory and applications.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Programs and apps: productivity, graphics, security and other tools
Electronic commerce courselecture one. Pdf
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25-Week II
Empathic Computing: Creating Shared Understanding
SOPHOS-XG Firewall Administrator PPT.pptx
cuic standard and advanced reporting.pdf
Group 1 Presentation -Planning and Decision Making .pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...

Browsers_SameOriginPolicy_CORS_ContentSecurityPolicy

  • 1. Same Origin Policy Cross-Origin Resource Sharing Content Security Policy subbul@gmail.com
  • 2. Agenda • • • • Need for SOP How CORS help SOP What is XSS? How CSP helps preventing XSS
  • 3. Why Same Origin Policy ? • What if your personal data you are entering in a “Bank” page in Browser is accessible to another Page in the browser Instance
  • 4. What is Same Origin Policy • This is a Browser Mechanism to allow trusted pages/scripts • To Prevent HTML/JS Application from different window, domain accessing the DOM, data of Application current domain or “Origin” • Thanks to Same Origin Policy (SOP), Browser prevents loading or blocks request for DOM access, execution of script from “Origin/Domain” other than “Self” • More Details
  • 5. What are allowed in SOP? • SOP cannot prevent cross site content inclusions (like images, scripts, css from different domain • http://www.google.com/page1 can access http://www.google.com/page2 • http://www.google.com/page1 cannot access http://www.yahoo.com as the two pages belong to different domain • <script> is allowed by SOP [file:// ??] • In a http://www.mypage.com page, you can include<script src= http://api.google.com/googleplus >. • Google API page scripts are executed in “Mypage” domain, HTML Application, it will still have access to “Mypage” DOM elements. So, if the “Google API scripts” are compromised, it will have bad effect on the “MyPage” (Will take it to XSS- Cross Site Scripting)
  • 6. What is not allowed in SOP? • AJAX (XHR) from One domain to another • XHR request from “MyPage.com” to “Google.com” • Why it is not allowed? – Using AJAX you can download a malicious JS code and could spoil the current page information or could derive information from current page and send it over maliciously to remote pages
  • 7. How to circumvent SOP • • • • Simple suggestion DO NOT USE ( unless it’s the End of the World) Document.domain PostMessage JSONP • Right Way – CORS (Cross-Origin Resource Sharing)
  • 8. Cross-Origin Resource Sharing • CORS is to overcome SOP for XHR • Allowing Cross Origin Request from Domain A to Domain B using XHR • Introduction of new HTTP Headers (Origin) from Server to make Browser decide to Allow Cross-Origin request or not • Use Pre-flight (handshake) OPTIONS request for methods other than POST/GET to know if the server supports, allow-origin for your request More Detail
  • 10. CORS HTTP Request/Response Headers HTTP Request/ ResponseHeader Parameter Description Example Access-Control-AllowOrigin: <origin> | * Specifying a particular “domain” is allowed or “*” all Access-Control-AllowOrigin: http://mozilla.com Access-Control-AllowCredentials True| false Request for cookie along with request Access-ControlRequest-Method GET,POST Request for supported HTTP methods Access-Control-AllowHeaders Content-Type| Custom-Header Preflight-request headers
  • 11. CORS Server/Browser Request /Response Flow http://www.html5rocks.com/static/images/cors_server_flowchart.png
  • 12. XSS (Cross Site Scripting) • Finding Vulnerability of Web Pages and injecting and injecting malicious client side- script . • Types – Non-Persistent (server Echo’s back your request) – Adding malicious scripts in HTML Forms, HTTP Query from web browser during a search request. If the “String” is not formatted/escaped, the injected script will be executed back in client browser. – E.g., • Phishing Attacks, • URL Shortens (bit.ly ) taking to legitimate page and injecting their “script” along with it
  • 13. XSS (Cross Site Scripting) – Persistent (Server stores the data and script) – Storing user provided “string” as is without escaping the HTML, JS code in Webserver and serving later to all users will cause the malicious script to execute on client browser – Message Boards, which include Plain Text and Scripts, later when another user reloads the Message Board, the malicious code executes and steals user data – Defacing web servers, cookie/session stealing
  • 14. Examples • http://www.insecurelabs.org/Task/Rule1 • http://www.insecurelabs.org • https://www.owasp.org/index.php/Testing_for_Reflected_Cros s_site_scripting_(OWASP-DV-001)
  • 15. How to Prevent XSS • • • • • Validation/Sanitization of ALL user inputs in a page No inline please, keep it safe in a dedicated JS Secure all input path, query string, file path etc Don’t keep untrusted data in your HTML, JS This is one of the reason, you find forms in organization preventing <, > etc  • https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Pre vention_Cheat_Sheet • And of course CSP (Content Security Policy)
  • 16. Content Security Policy (CSP) • It’s a policy how Browser/UserAgent adhere to as a directive from HTTP Server in order to display, execute scripts • New HTTP Headers introduced to enable CSP • Content-Security-Policy: script-src 'self' Trusted Source https://abc.MyWebpage.com Resource Trusted Source
  • 17. Content Security Policy • If a malicious code is injected through XSS (added <script src=“hackedsite.com”>), browser will detect and prevent • More XSS prevention by • 'unsafe-inline' prevents inline JavaScript and CSS • 'unsafe-eval' prevents text-to-JavaScript mechanisms like eval • Default-src “none” (Shut down any other script, img, media load beyond my own) • Other resources which can be controlled by CSP are font-src,img-src etc – – – – http://www.html5rocks.com/en/tutorials/security/content-security-policy/ http://erlend.oftedal.no/blog/csp/readiness/ http://people.mozilla.org/~bsterne/content-security-policy/details.html https://wiki.mozilla.org/index.php?title=Security/CSP/Spec&oldid=133465