SlideShare a Scribd company logo
CORS and Javascript
Integration in the browser
Michael Neale
@michaelneale
developer.cloudbees.com
http://en.wikipedia.org/wiki/Cross-
origin_resource_sharing
Thursday, 12 September 13
Integration in the browser
Lots of services, microservices
Everything has an API
JSON == lingua franca
Why have servers that are just text pumps:
Integrate into new apps in the browser
Thursday, 12 September 13
For example
Thursday, 12 September 13
CI serverApp service
JSON !!111
repos
Thursday, 12 September 13
but - same origin policy?
http://en.wikipedia.org/wiki/Same-origin_policy
Thursday, 12 September 13
same origin policy
Thursday, 12 September 13
but - same origin policy?
Web security model - not bad track record.
Not going to change... so, how to work around:
Thursday, 12 September 13
integration middleware?
Thursday, 12 September 13
Overkill
Thursday, 12 September 13
JSON-P
All JSON world - add “padding”.
JSON P: take pure json, make it a function call - then
eval it.
Single Origin policy doesn’t apply to resource
loading
Thursday, 12 September 13
jsonp: Most glorious hack
ever
Thursday, 12 September 13
JSON-P
JSON: { “foo” : 42 }
JSONP: callback({ “foo” : 42 });
Widely supported (both by servers and of course
jquery makes it transparent)
Thursday, 12 September 13
JSON-P
What it is really doing: creating script tags, and
making your browser “eval” the lot. Each time.
Don’t think too hard about it...
Thursday, 12 September 13
JSON-P
Really misses the “spirit” of same-origin.
Security holes: any script you bring in has access to
your data/dom/private parts.
How secure is server serving up json-p?
Thursday, 12 September 13
JSON-P
Also, JSON is not Javascript
JSON can be safely read - no eval
JSON-P only eval
JSONP is GET only
Thursday, 12 September 13
CORS
http://en.wikipedia.org/wiki/Cross-
origin_resource_sharing
Allows servers to specify who/what can access
endpoint directly
Use plain JSON, ALL HTTP Verbs: PUT, DELETE
etc
Thursday, 12 September 13
CORS
Thursday, 12 September 13
Oy, they’re my sisters yer lookin atNOT THESE:
Thursday, 12 September 13
CORS
Trivial to consume: plain web calls, direct.
Complexity: on the server/config side.
Browser support: complete(ish):
http://enable-cors.org/
All verbs, all data types
Thursday, 12 September 13
How it works
Most work is between browser and server, via http
headers.
“Pre flight checks”:
Browser passes Origin header to server:
Origin: http://www.example-social-network.com
Server responds (header) saying what is allowed:
Access-Control-Allow-Origin: http://www.example-social-network.com
Thursday, 12 September 13
How it works
browser server
http OPTIONS (Origin: http://boo.com)
Access-Control-Allow.... etc
direct http GET (as allowed by Access headers)
...
pre-
flight
your
app
Thursday, 12 September 13
How it works
“Pre flight checks”:
Performed by browser, opaque to client app.
Browser enforces. You don’t see them.
Uses “OPTION” http verb.
Thursday, 12 September 13
Security Theatre?
“Pre flight checks”:
Can be just an annoyance.
Access-Control-Allow-Origin: *
Downside: allows any script with right creds to pull
data from you (do you want this? Think, as always)
Thursday, 12 September 13
Common pattern
Access-Control-Allow-Origin: $origin-from-request
The returned value is really echoing back what Origin
was - checked off against a whitelist:
Server needs to know whitelist, how to check, return
value dynamically.
Not a static web server config. SAD FACE.
Thursday, 12 September 13
Middleware
All app server environments have a way to do the
Right Thing with CORS headers:
Rack-cors: ruby
Servlet-filter: java
Node: express middleware
etc...
(it isn’t hard, just not as easy as it should be)
http://stackoverflow.com/questions/7067966/how-to-allow-
cors-in-express-nodejs
Thursday, 12 September 13
Other CORS headers
Access-Control-Allow-Headers (headers to be
included in requests)
Access-Control-Allow-Methods: GET, PUT, POST,
DELETE etc
Access-Control-Allow-Credentials: boolean
(lists always comma separated)
Thursday, 12 September 13
Authorization
You can use per request tokens, eg OAuth
OpenID and OAuth based sessions will work
(browser has done redirect “dance” - Access-
Control-Allow-Credentials: true -- needed to ensure
cookies/auth info flows with requests)
Thursday, 12 September 13
Authorization
requests
authorization
identity/auth
pre-flight
your app
browser
Thursday, 12 September 13
Debugging
Pesky pre-flight checks are often opaque - may
show up as “cancelled” requests without a reason.
Use chrome://net-internals/#events
Thursday, 12 September 13
Debugging
Following screen cap shows it working...
note the match between Origin and Access-control -
if you don’t see those headers in response -
something is wrong.
Thursday, 12 September 13
Thursday, 12 September 13
Thursday, 12 September 13
Debugging
t=1374052796709 [st=262] +URL_REQUEST_BLOCKED_ON_DELEGATE [dt=0]
t=1374052796709 [st=262] CANCELLED
t=1374052796709 [st=262] -URL_REQUEST_START_JOB
--> net_error = -3 (ERR_ABORTED)
This is it failing: look for “cancelled”.
Could be due to incorrect headers returned, or
perhaps Authorization failures (cookies, session etc)
Thursday, 12 September 13
My Minimal Setup
 Access-Control-Allow-Methods: GET, POST, PUT, DELETE
 Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: $ORIGIN
$ORIGIN = if (inWhitelist(requestOriginHeader) return
requestOriginHeader
INCLUDE PORTS IN Access-Control-Allow-Origin!!
Thursday, 12 September 13
Servlet Filter
https://github.com/cloudbees/cors-plugin/blob/
master/src/main/java/org/jvnet/hudson/plugins/
cors/JenkinsCorsFilter.java
Thursday, 12 September 13
Thank you
Michael Neale
https://twitter.com/michaelneale
https://developer-blog.cloudbees.com
Thursday, 12 September 13

More Related Content

PDF
jAPS 2 0 - Presentation Layer Comparison
PDF
Introduction to Web Sockets
PDF
Async Tasks with Django Channels
PDF
Real-Time Django
PPTX
Web Sockets
PDF
Web Sockets - HTML5
PDF
Alex carcea, radu macovei a story of how java script joined the big league
PPTX
Turning Your Website Performance to 11
jAPS 2 0 - Presentation Layer Comparison
Introduction to Web Sockets
Async Tasks with Django Channels
Real-Time Django
Web Sockets
Web Sockets - HTML5
Alex carcea, radu macovei a story of how java script joined the big league
Turning Your Website Performance to 11

What's hot (7)

PDF
Timeless - Websocket on Rails
PDF
Dev con kolkata 2012 websockets
KEY
SimpleTestによるPHPのテスト
PPTX
Drupal Development Tips
PDF
大众点评网 Web开发之路
PDF
PHP SA 2014 - Releasing Your Open Source Project
PPT
Varnish - Full page caching
Timeless - Websocket on Rails
Dev con kolkata 2012 websockets
SimpleTestによるPHPのテスト
Drupal Development Tips
大众点评网 Web开发之路
PHP SA 2014 - Releasing Your Open Source Project
Varnish - Full page caching
Ad

Similar to Cors michael (20)

PDF
Cross site calls with javascript - the right way with CORS
PPTX
Conquering CORS. Taming Cross-Origin Resource Sharing.
PDF
CORS and (in)security
PDF
JavaScript Security: Mastering Cross Domain Communications in complex JS appl...
PDF
Cross-domain requests with CORS
PDF
CORS in Action Creating and consuming cross origin APIs 1st Edition Monsur Ho...
KEY
Message in a Bottle
PDF
Cors
PPTX
Cross-origin resource sharing
PDF
What Is Cross-Origin Resource Sharing in Web Development.pdf
PDF
Javascript cross domain communication
ODP
CONFidence 2014: Kiss, Zagon, Sseller: Scaling security
PPTX
Cross Origin Resource Sharing (CORS) - Azizul Hakim
PPTX
W3 conf hill-html5-security-realities
PPTX
Of CORS thats a thing how CORS in the cloud still kills security
PDF
Using Communication and Messaging API in the HTML5 World
PDF
Evolving web security model v1.1 - Portland OWASP May 29 2014
PPTX
Web Security - Cookies, Domains and CORS
PPTX
W3 conf hill-html5-security-realities
PPTX
Cors (cross origin request sharing) in mule
Cross site calls with javascript - the right way with CORS
Conquering CORS. Taming Cross-Origin Resource Sharing.
CORS and (in)security
JavaScript Security: Mastering Cross Domain Communications in complex JS appl...
Cross-domain requests with CORS
CORS in Action Creating and consuming cross origin APIs 1st Edition Monsur Ho...
Message in a Bottle
Cors
Cross-origin resource sharing
What Is Cross-Origin Resource Sharing in Web Development.pdf
Javascript cross domain communication
CONFidence 2014: Kiss, Zagon, Sseller: Scaling security
Cross Origin Resource Sharing (CORS) - Azizul Hakim
W3 conf hill-html5-security-realities
Of CORS thats a thing how CORS in the cloud still kills security
Using Communication and Messaging API in the HTML5 World
Evolving web security model v1.1 - Portland OWASP May 29 2014
Web Security - Cookies, Domains and CORS
W3 conf hill-html5-security-realities
Cors (cross origin request sharing) in mule
Ad

More from Michael Neale (15)

PDF
Jenkins X intro (from google app dev conference)
PDF
Devoxx 2014 michael_neale
PDF
PDF
Microservices and functional programming
KEY
Osdc 2011 michael_neale
KEY
Scala sydoct2011
KEY
Java one 2011_michaelneale
PDF
Errors and handling them. YOW nights Sydney 2011
KEY
Sjug aug 2010_cloud
KEY
SJUG March 2010 Restful design
PPT
On Scala Slides - OSDC 2009
PPT
Osdc Complex Event Processing
PDF
Scala Sjug 09
PDF
Jaoo Michael Neale 09
PDF
Osdc Michael Neale 2008
Jenkins X intro (from google app dev conference)
Devoxx 2014 michael_neale
Microservices and functional programming
Osdc 2011 michael_neale
Scala sydoct2011
Java one 2011_michaelneale
Errors and handling them. YOW nights Sydney 2011
Sjug aug 2010_cloud
SJUG March 2010 Restful design
On Scala Slides - OSDC 2009
Osdc Complex Event Processing
Scala Sjug 09
Jaoo Michael Neale 09
Osdc Michael Neale 2008

Recently uploaded (20)

PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Approach and Philosophy of On baking technology
PDF
August Patch Tuesday
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPT
Teaching material agriculture food technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
1. Introduction to Computer Programming.pptx
PDF
Getting Started with Data Integration: FME Form 101
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
A comparative analysis of optical character recognition models for extracting...
Heart disease approach using modified random forest and particle swarm optimi...
Network Security Unit 5.pdf for BCA BBA.
OMC Textile Division Presentation 2021.pptx
Approach and Philosophy of On baking technology
August Patch Tuesday
SOPHOS-XG Firewall Administrator PPT.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Reach Out and Touch Someone: Haptics and Empathic Computing
Mobile App Security Testing_ A Comprehensive Guide.pdf
Teaching material agriculture food technology
Advanced methodologies resolving dimensionality complications for autism neur...
Spectral efficient network and resource selection model in 5G networks
1. Introduction to Computer Programming.pptx
Getting Started with Data Integration: FME Form 101
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Empathic Computing: Creating Shared Understanding
Digital-Transformation-Roadmap-for-Companies.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

Cors michael