Your SlideShare is downloading. ×
  • Like
Going Beyond Cross Domain Boundaries (jQuery Bulgaria)
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Going Beyond Cross Domain Boundaries (jQuery Bulgaria)


Same-origin policy is an important security concept of the modern browser languages like JavaScript but becomes an obstacle for developers when building complex client-side apps. Over time there have …

Same-origin policy is an important security concept of the modern browser languages like JavaScript but becomes an obstacle for developers when building complex client-side apps. Over time there have been lots of ingenious workarounds using JSON-P, IFRAME and proxies. As of January 2013 the well known Cross Origin Resource Sharing (CORS) comes as proposed standard by W3C and has now native support by all major browsers.

Published in Software , Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. var title = “Go Beyond Cross Domain Boundaries”; $(this).attr(“title”, title); $(this).data({ font: ‘Segoe UI’, size: ‘30pt’, speaker: ‘Ivelin Andreev’ });
  • 2. About me • Project Manager @ • 11 years professional experience • MCPD .NET Web Development • • • Business Interests o Web Development (ASP.NET, jQuery, AJAX) o SOA, Integration o GIS, Mapping o Performance tuning, Network security
  • 3. Agenda • • • • • • • What is Same Origin Policy Security issues it solves Security issues it does not solve X-domain techniques CORS Why CORS? Demo
  • 4. The Same Origin Policy (SOP) • Same origin - if scheme://host:port are the same • JavaScript limited by SOP • Script access properties of documents with same origin o DOM objects o Cookies
  • 5. Same origin policy is the most important security concept in modern browsers
  • 6. Same Origin Policy as Concept • Not a single policy but set of mechanisms o o o o o o SOP for DOM access SOP for XMLHttpRequest SOP for Cookies SOP for Flash SOP for Java SOP for Silverlight • Significant bottleneck in browsers • Behavior is different among browsers • Static bound to single domain o Not all content on site should be trusted the same
  • 7. Change origin is possible (with some limitations)
  • 8. Changing Origin • document.domain o Allow subdomain to access parent domain o Must set to the same value for parent domain and subdomain • Port number set to null • Even in document.domain = document.domain • Cross-origin network access o X-Origin Writes – Typically Allowed (redirects, form submissions) o X-Origin Embed – Typically Allowed • JavaScript <script src="..."></script> • CSS <link rel="stylesheet" href="..."> • Frames <frame>, <iframe> • Media & Plugins <img>, <video>, <audio>, <object>, <embed> o X-Origin Reads – Typically Not allowed
  • 9. Same-Origin Policy Limits • o <a> • Can link to resource in another domain • But cannot control site from another domain o <iframe> • Include resource from another domain • But cannot directly access DOM o <script> • Include script from another domain • But cannot act on behalf of the script • Implement policy check and inspect contents of enclosing page o <form method=“POST” name=“f1” action=“”> • Submit forms without user input • But cannot access user cookies
  • 10. Cross domain policy does NOT prevent web application exploits
  • 11. Cross Site Request Forgery (XSRF) • Case o User logs in as usual o can • POST new password in form to • GET o Authenticated because cookies are sent • Impact o cannot read DOM but can POST to app o User access is blocked or stolen o Act on behalf of the user (payment) • Prevention o Identify valid requests • By user provided secret (low usability) • By XSRF token for every request
  • 12. Cross Site Scripting Inclusion (XSSI) • Case o o o o o o includes <script> to perform AJAX request includes the same script Authenticated because cookies are sent JSONP (SCRIPT + JSON) returned by server as usual SCRIPT evaluated in context and JSON is stolen redefines callback • Impact o User data are stolen • Prevention o must check policy of script inclusion
  • 13. Cross Site Scripting (XSS) • Case o injects <script> in application context • By posting HTML form field • By tricking user to click link with query parameters sent by mail o • Impact o Steel user cookies for and transfer to o Phishing attack redirects to copy o Script modify content (even SSL cert will not warn) • Prevention o Filter user input o ALWAYS HTML and URL Encode/Decode o Do not send untrusted data to browser
  • 14. There is need of reliable and secure Cross Domain Messaging
  • 15. Common X-Domain Use Cases Typical cases • Consume REST APIs • Build mashups • Operate Ads • Synchronize two pages Use when • You own both sites • Request information from a site that trusts you Note • No solution solves the problems in every browser.
  • 16. Policy limitations forced developers create ingenious workarounds
  • 17. Hack • Child window (frame/iframe) sets: = ‘{“message”:”text”}’; • Parent window: f = document.createElement('iframe'); f.onload = function () { doWork(); f.src='about:blank'}; f.src = ''; document.body.appendChild(f); Notes: • Very tricky, works on all browsers
  • 18. document.domain Hack • Allows cross SUB-domain access //From a page in document.domain = “”; • Pages can access each other’s DOM objects • The sub- and parent domain have the same permissions Notes: • document.domain is ReadOnly property in HTML spec. • Useful when you do not own both sites • Works on all browsers • Port set to null when document.domain is set
  • 19. iFrame Proxy Hack • domainB tries to get parent.document o Permission denied to access property ‘document’’ • Hidden iFrame to exchange data • Proxy: subscribes to onResize event • Child: domainB sets hash on proxy • Proxy: reads message and changes Notes: • Do-it-yourself approach • Complex and browser-dependent • Widely accepted as standard
  • 20. Other solutions are not that hacky
  • 21. What is new in HTML5 window.postMessage Pass message between two windows safely otherWindow.postMessage(message, targetOrigin, [FF:transfer]); • otherWindow can listen for sent messages by executing: function receiveMessage(event) { if (event.origin !== "") return; ... } window.addEventListener("message", receiveMessage, false); Notes: • Basic support in IE8, IE9, limitations in IE10 • Always check origin to prevent XSS attacks • If you do not expect messages, do not subscribe
  • 22. JSON-P • Loads JSON from another domain • Exploits HTML <script> element exception to SOP • Client adds query parameters to server <script type="application/javascript" src= "" /> • Server returns JSON wrapped in function call parseResponse ({“this”:”is”,”json”:”data”}); • JS function callback evaluated in page Notes: • Useful for RESTful APIs • Vulnerable to XSRF and XSS attacks
  • 23. easyXDM Library • Pass string messages between domains o Enables developers to workaround SOP limitations o postMessage transport on modern browsers, fallback to frame element • Consumer var socket = new easyXDM.Socket({ remote: “”, //provider path onMessage: function(message, origin){ if (origin==“…”) alert(message); } }); socket.postMessage(“message"); • Provider var socket = new easyXDM.Socket({ onMessage: function(message, origin) {alert(message); } });
  • 24. Cross Origin Resource Sharing
  • 25. How does CORS Work • Request headers o Origin: • Response headers o Access-Control-Allow-Origin: • • “*” – all domains allowed o Error if not allowed Note: “*” does not allow supply of credentials o HTTP authentication will not work o Client SSL certificates will not work o Cookies will not work
  • 26. Preflight Request • Required when o HTTP verb other than GET/POST o Request MIME type other than text/plain (i.e. application/json) o Custom headers • Headers determine whether CORS is enabled o Request (HTTP OPTIONS method) • Origin: • Access-Control-Request-Method: [method the request wants to use] • Access-Control-Request-Headers: [optional CSV, custom headers] o Response • Access-Control-Allow-Origin: [allowed origin] • Access-Control-Allow-Methods: [CSV allowed methods] • Access-Control-Allow-Headers: [CSV allowed headers] • Access-Control-Max-Age: [seconds preflight is valid]
  • 27. Credential Request • By Default o X-domain do not send credentials (cookies, client SSL, HTTP authentication) • Request (specify send credentials) o xmlHttpRequest.withCredentials = true; • Response headers (if server allows ) o o o o Access-Control-Allow-Credentials: true Access-Control-Allow-Origin: Otherwise response will be ignored by browser Header can be sent during pre-flight request
  • 28. Can I Use IE 8 and IE9 limitations • • • Use XDomainRequest Preflight not supported Request limited to the target scheme of hosting page
  • 29. Why use CORS • The most robust solution for X-domain requests with JS • The best approach to consume RESTful API with JS • Modern alternative to JSON-P and W3C standard JSON-P HTTP Verbs GET Browser Support All CORS GET,PUT,POST,DELETE,OPTIONS Does not < IE 8 Error Handling Tricky to none HTTP status access via XHR Performance 1 HTTP Request 2 Requests (up to 3) Authentication Cookies only Cookies, Basic, client SSL X-Site Scripting If external site compromised Consumer parses response
  • 30. Check this out • Open Web Application Security Project o • Mozilla Developer Network o • Html5rocks CORS Tutorial o • Gruyere Code Lab - Exploits and Defenses o
  • 31. Demo DEMO
  • 32. Thanks to our Sponsors: Diamond Sponsor: Gold Sponsors: Silver Sponsors: Technological Partners: Bronze Partners: Swag Sponsors: Media Partners: