cross domain access policy solution using cross origin resource sharing

47
Breaking The Cross Domain Breaking The Cross Domain Barrier Barrier Alex Sexto Alex Sexto

Upload: ankit-garg

Post on 15-Dec-2014

2.066 views

Category:

Technology


3 download

DESCRIPTION

Cross Domain Access Policy solution using Cross Origin Resource sharing

TRANSCRIPT

Page 1: Cross Domain Access Policy solution using Cross Origin Resource sharing

Breaking The Cross Domain Breaking The Cross Domain BarrierBarrierBreaking The Cross Domain Breaking The Cross Domain BarrierBarrier

Alex SextonAlex Sexton

Page 2: Cross Domain Access Policy solution using Cross Origin Resource sharing

A Story...A Story...

AJAX is so nifty!AJAX is so nifty!

We can do anything!We can do anything!

FML :(FML :(

Page 3: Cross Domain Access Policy solution using Cross Origin Resource sharing

Same Origin PolicySame Origin PolicyApplies to XMLHTTPRequestsApplies to XMLHTTPRequests

Stops hackers from getting our data!Stops hackers from getting our data!

Page 4: Cross Domain Access Policy solution using Cross Origin Resource sharing

Same Origin PolicySame Origin PolicyIt’s actually an important rule. You It’s actually an important rule. You wouldn’t want to have this happen:wouldn’t want to have this happen:

Page 5: Cross Domain Access Policy solution using Cross Origin Resource sharing

Same Origin PolicySame Origin PolicyIt’s actually an important rule. You It’s actually an important rule. You wouldn’t want to have this happen:wouldn’t want to have this happen:

Page 6: Cross Domain Access Policy solution using Cross Origin Resource sharing

YEA BUT WHAT IF...YEA BUT WHAT IF...

I own both sites and I just want to make them I own both sites and I just want to make them talk?talk?

The site I want information from says it’s okay?The site I want information from says it’s okay?

I don’t give a shit?!I don’t give a shit?!

Page 7: Cross Domain Access Policy solution using Cross Origin Resource sharing

The SolutionThe Solution

Post Message.Post Message.

Page 8: Cross Domain Access Policy solution using Cross Origin Resource sharing

THE END.THE END.

kthnxbai.kthnxbai.

Page 9: Cross Domain Access Policy solution using Cross Origin Resource sharing

<RecordScratch.wav><RecordScratch.wav>

Browser Vendors have realized that there is a Browser Vendors have realized that there is a need for cross domain messaging.need for cross domain messaging.

IE6 ruins your life again and again.IE6 ruins your life again and again.

There is not a single solution that solves every There is not a single solution that solves every problem in every browser :(problem in every browser :(

Page 10: Cross Domain Access Policy solution using Cross Origin Resource sharing

Some OptionsSome OptionspostMessagepostMessage

JSONPJSONP

CORSCORS

document.domain modsdocument.domain mods

window.name Transportwindow.name Transport

Server-Side ProxyServer-Side Proxy

CRAZY IFRAME STUFFCRAZY IFRAME STUFF

MOARMOAR

Page 11: Cross Domain Access Policy solution using Cross Origin Resource sharing

Post MessagePost MessageJust pass messages between window objects!Just pass messages between window objects!

It’s safe(ish) because both pages have to know about It’s safe(ish) because both pages have to know about it.it.

Page 12: Cross Domain Access Policy solution using Cross Origin Resource sharing

Post MessagePost MessageHandle the message event in the otherWindowHandle the message event in the otherWindow

Page 13: Cross Domain Access Policy solution using Cross Origin Resource sharing

Post MessagePost Message

Passing events along from one window to the Passing events along from one window to the otherother

Initializing the state of a new windowInitializing the state of a new window

Synchronizing two pagesSynchronizing two pages

Most things, but it’s not always practicalMost things, but it’s not always practical

What’s it good for?What’s it good for?

Page 14: Cross Domain Access Policy solution using Cross Origin Resource sharing

Post Message Works In...Post Message Works In...

FF3+FF3+

IE8+IE8+

Chrome 1+Chrome 1+

Safari 4+Safari 4+

Kind of in Opera for a while but it’s a little Kind of in Opera for a while but it’s a little different but good enough probably so we’ll different but good enough probably so we’ll count itcount it

Page 15: Cross Domain Access Policy solution using Cross Origin Resource sharing

JSONPJSONP

JavaScript Object Notation with Padding. JavaScript Object Notation with Padding. (dumb I (dumb I know)know)

Page 16: Cross Domain Access Policy solution using Cross Origin Resource sharing

JSONENJSONENJavaScript Object Notation Except JavaScript Object Notation Except NotNot

**Formerly JSONP**Formerly JSONP

Page 17: Cross Domain Access Policy solution using Cross Origin Resource sharing

JSONJSON

A standard (mostly) created by the Crock.A standard (mostly) created by the Crock.

A subset of JavaScript with some extra rulesA subset of JavaScript with some extra rules

Non-Executable - just for dataNon-Executable - just for data

Page 18: Cross Domain Access Policy solution using Cross Origin Resource sharing

JSONPJSONP

JavaScript...JavaScript...

Page 19: Cross Domain Access Policy solution using Cross Origin Resource sharing

JSONP - Why it’s specialJSONP - Why it’s special

The ‘P’The ‘P’

<script> tags are not subject to the Same <script> tags are not subject to the Same Origin Policy Origin Policy (A (A totaltotal security flaw that will never change) security flaw that will never change)

Page 20: Cross Domain Access Policy solution using Cross Origin Resource sharing

How JSONP WorksHow JSONP Works

Step 1: You create a callback function that Step 1: You create a callback function that accepts some dataaccepts some data

Page 21: Cross Domain Access Policy solution using Cross Origin Resource sharing

How JSONP WorksHow JSONP Works

Step 2: Include a script with a hint of what your Step 2: Include a script with a hint of what your function is called.function is called.

hint-hinthint-hint

Page 22: Cross Domain Access Policy solution using Cross Origin Resource sharing

How JSONP WorksHow JSONP Works

Step 3: Output a script that calls the function Step 3: Output a script that calls the function and passes in the necessary data.and passes in the necessary data.

Page 23: Cross Domain Access Policy solution using Cross Origin Resource sharing

JSONP Is Good For...JSONP Is Good For...

Data PassingData Passing

RESTful APIsRESTful APIs

1-(way/time)-ish cross domain communication1-(way/time)-ish cross domain communication

HackingHacking

Page 24: Cross Domain Access Policy solution using Cross Origin Resource sharing

CORS | Tap the RockiesCORS | Tap the Rockies

Cross-Origin Resource Sharing (CORS) is a W3C Working Draft that defines how the browser and server must communicate when accessing sources across origins.

Page 25: Cross Domain Access Policy solution using Cross Origin Resource sharing

CORS - HOW?CORS - HOW?

Page 26: Cross Domain Access Policy solution using Cross Origin Resource sharing

CORS - HOW?CORS - HOW?

Use it or lose itUse it or lose it

Page 27: Cross Domain Access Policy solution using Cross Origin Resource sharing

CORS - From the CORS - From the Server...Server...

CORS sends along an extra header:CORS sends along an extra header:

Your server must send back another, saying it’s Your server must send back another, saying it’s ok:ok:

Page 28: Cross Domain Access Policy solution using Cross Origin Resource sharing

CORS - CompatibilityCORS - Compatibility

IE 8+ (most of it, at least)IE 8+ (most of it, at least)

FF 3.5+FF 3.5+

Safari 4+Safari 4+

ChromeChrome

Unrelated GraphUnrelated Graph

Page 29: Cross Domain Access Policy solution using Cross Origin Resource sharing

CORS - What’s it Good CORS - What’s it Good For?For?

Not working on 40% of the internetNot working on 40% of the internet

Creating an extra http request (usually only Creating an extra http request (usually only once)once)

Custom grouping optionsCustom grouping options

Finer grain control over what’s accessible Finer grain control over what’s accessible Access-Control-Allow-Credentials: true

Straight Up. Cross Domain XHR Straight Up. Cross Domain XHR (yay!)(yay!)

Page 30: Cross Domain Access Policy solution using Cross Origin Resource sharing

Document.domain Document.domain HackzHackz

Good for allowing Cross Good for allowing Cross SubSubdomain window domain window accessaccess

Now the subdomain has the same permissions for Now the subdomain has the same permissions for accessaccess

Can be very useful even if you don’t own the site, Can be very useful even if you don’t own the site, because subdomains can be cnamed to totally because subdomains can be cnamed to totally different webserversdifferent webservers

Works in all relevant browsersWorks in all relevant browsers

Page 31: Cross Domain Access Policy solution using Cross Origin Resource sharing

Window.nameWindow.name

HI! I’m Jerry the Window!

HI! I’m Jerry the Window!

Page 32: Cross Domain Access Policy solution using Cross Origin Resource sharing

Window.nameWindow.nameSuperhacky but safer than jsonp!Superhacky but safer than jsonp!

Works everywhere relevantWorks everywhere relevant

**This is obviously a little simplified**This is obviously a little simplified

Added to Dojo 2 years ago:Added to Dojo 2 years ago:http://bugs.dojotoolkit.org/ticket/6893

Page 33: Cross Domain Access Policy solution using Cross Origin Resource sharing

Server Side ProxiesServer Side Proxies

Pretty simple concept, only slightly more Pretty simple concept, only slightly more difficult to implementdifficult to implement

mySitemySiteyourSiteyourSite

myServermyServerhttp://benalman.com/projects/php-simple-proxy/

Works everywhere XHR doesWorks everywhere XHR does

Page 34: Cross Domain Access Policy solution using Cross Origin Resource sharing

Crazy iFrame HacksCrazy iFrame HacksA parent window can’t A parent window can’t readread just about just about anything from a child window (iframe) that is anything from a child window (iframe) that is on a different domain.on a different domain.

A parent window can traverse any known A parent window can traverse any known elements in an iframe though.elements in an iframe though.

A parent window can A parent window can setset properties on the properties on the iframe.iframe.

FACTSFACTS

Page 35: Cross Domain Access Policy solution using Cross Origin Resource sharing

Crazy iFrame HacksCrazy iFrame HacksA window can read and write properties of an iframe A window can read and write properties of an iframe if it’s on the same domain - EVEN IF it’s inside of if it’s on the same domain - EVEN IF it’s inside of another iframe that isn’t on the same domain!another iframe that isn’t on the same domain!

a.coma.com

b.comb.coma.coma.com

Page 36: Cross Domain Access Policy solution using Cross Origin Resource sharing

Crazy iFrame HacksCrazy iFrame HacksIf B wants to talk to AIf B wants to talk to A

a.coma.com

b.comb.coma.coma.com

Change the url hash on the innermost iframe to the Change the url hash on the innermost iframe to the messagemessage

/#secret/#secret

Page 37: Cross Domain Access Policy solution using Cross Origin Resource sharing

Crazy iFrame HacksCrazy iFrame HacksIf B wants to talk to AIf B wants to talk to A

a.coma.com

b.comb.coma.coma.com

Have the top level frame read the message on the Have the top level frame read the message on the hashhash

/#secret/#secret

Page 38: Cross Domain Access Policy solution using Cross Origin Resource sharing

Crazy iFrame HacksCrazy iFrame Hacks

Poll for hashchange the entire timePoll for hashchange the entire time

Set up iframes to destroy themselves after Set up iframes to destroy themselves after each message and just wait for the load eventeach message and just wait for the load event

Resize the iframe on change, then attach an Resize the iframe on change, then attach an event handler on the resize event that checks event handler on the resize event that checks for new datafor new data

How to know when to receive How to know when to receive datadata

Fast (where it works)Fast (where it works)

Page 39: Cross Domain Access Policy solution using Cross Origin Resource sharing

Crazy iFrame HacksCrazy iFrame Hacks

Works at varying levels of success via some Works at varying levels of success via some slightly different methods in all relevant slightly different methods in all relevant browsersbrowsers

Unfortunately often our best choice for Unfortunately often our best choice for something that works everywheresomething that works everywhere

Page 40: Cross Domain Access Policy solution using Cross Origin Resource sharing

Best of both worlds?Best of both worlds?

EasyXDMEasyXDMA library that will use postMessage first and then a series of A library that will use postMessage first and then a series of different techniques based on which browser you use, butdifferent techniques based on which browser you use, butwith normalized syntax.with normalized syntax.

http://easyxdm.net/

Page 41: Cross Domain Access Policy solution using Cross Origin Resource sharing

Best of Both Worlds?Best of Both Worlds?flXHR / Flash + your own fallbackflXHR / Flash + your own fallback

http://flxhr.flensed.com/

Page 42: Cross Domain Access Policy solution using Cross Origin Resource sharing

What about cookies?What about cookies?Cookies are insanely easy to Cookies are insanely easy to stealsteal, err.. I mean , err.. I mean share, across domains with these techniques.share, across domains with these techniques.

With the exception of Safari, cookies are passed With the exception of Safari, cookies are passed from the server along with script includes and from the server along with script includes and iframe injection. (You might need some P3P headers iframe injection. (You might need some P3P headers in IE)in IE)

Page 43: Cross Domain Access Policy solution using Cross Origin Resource sharing

What about cookies in What about cookies in Safari?Safari?

Safari doesn’t send cookies along in scripts and Safari doesn’t send cookies along in scripts and iframes, so there’s nothing to send to the iframes, so there’s nothing to send to the parent.parent.

Unless you ask nicely...Unless you ask nicely...

Page 44: Cross Domain Access Policy solution using Cross Origin Resource sharing

What about cookies in What about cookies in Safari?Safari?If we post to an iframe it will thank us by sending If we post to an iframe it will thank us by sending

cookiescookies

http://anantgarg.com/2010/02/18/cross-domain-cookies-in-safari/http://anantgarg.com/2010/02/18/cross-domain-cookies-in-safari/

Page 45: Cross Domain Access Policy solution using Cross Origin Resource sharing

Why Cookies?Why Cookies?If I had a network of sites that I wanted to track If I had a network of sites that I wanted to track you across, it would be easy for me maintain a you across, it would be easy for me maintain a central cookie and check for it on every site that central cookie and check for it on every site that you enter that contains my code. you enter that contains my code. (<cough>advertisers</cough>)(<cough>advertisers</cough>)

TotallyNotTrackingYou.comTotallyNotTrackingYou.com

Other SitesOther Sites

Holds your unique cookieHolds your unique cookie

Page 46: Cross Domain Access Policy solution using Cross Origin Resource sharing

LessonsLessonsWith great cross domain communication With great cross domain communication techniques come great cross domain security techniques come great cross domain security holesholes

Safe and FUN cross-domain communication is Safe and FUN cross-domain communication is possiblepossible

Paul Irish Paul Irish hateshates cold-cuts , cold-cuts , seriouslyseriously

Page 47: Cross Domain Access Policy solution using Cross Origin Resource sharing

Thanks!Thanks!

Alex SextonAlex Sexton

AlexSexton [at] gmail [dot] comAlexSexton [at] gmail [dot] com

@SlexAxton@SlexAxton

http://yayQuery.comhttp://yayQuery.com

Special Thanks toSpecial Thanks to: yayQuery Peeps, : yayQuery Peeps, BazaarVoice, Aaron Dixon, Shawn Smith, BazaarVoice, Aaron Dixon, Shawn Smith, EasyXDM, flXHR, Mozilla MDCEasyXDM, flXHR, Mozilla MDC