SlideShare a Scribd company logo
Berlin, 2018-01-01
JavaScript Security
Mastering Cross Domain Communications
in complex JS applications
Grill.js Wrocław, 2018-08-18, 7pm
<thomas.witt@infopark.com>
@thomas_witt
linkedin.com/in/thomaswitt
Thomas Witt
Co-Founder
JavaScript Security: Mastering Cross Domain Communications in complex JS applications
@thomas_witt
We're hiring!
Boston · Berlin · Wrocław
JavaScript Security: Mastering Cross Domain Communications in complex JS applications
JavaScript Security: Mastering Cross Domain Communications in complex JS applications
The Problem
JS Applications
need to exchange data with
Backend APIs
running on domains other than your own
SECURELY
Challange
Only load code from trusted origins,
don't execute malicious JS code,
don't let people steal your data.
Same Origin Policy
For security reasons, browsers restrict cross-
origin HTTP requests initiated from within
scripts.
For example, XMLHttpRequest and the Fetch
API follow the same-origin policy. This means
that a web application using those APIs can
only request HTTP resources from the same
origin the application was loaded from.
– https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
“
How to …
Exchange data
from JS applications with backend APIs
Two ways how to talk to other services
CORS postMessage
#1: CORS
Cross-Origin Resource Sharing
Same Origin Policy
A web application using those APIs can only
request HTTP resources from the same origin
the application was loaded from, unless the
response from the other origin includes
the right CORS headers.
– https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
“
The easy one:
Simple Requests
Simple GET request via CORS
var req = new XMLHttpRequest();
var url = 'http://bar.other/resources/public-data/';
function callOtherDomain() {
if(req) {
req.open('GET', url, true);
req.onreadystatechange = handler;
req.send();
}
}
Simple GET request via CORS
var req = new XMLHttpRequest();
var url = 'http://bar.other/resources/public-data/';
function callOtherDomain() {
if(req) {
req.open('GET', url, true);
req.onreadystatechange = handler;
req.send();
}
}
GET /resources/public-data/ HTTP/1.1
Origin: http://foo.example
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Client (foo.example) Server (bar.other)
The uglyness:
Preflight Requests
aka not-so-simple-requests
Preflighted POST request via CORS
var invocation = new XMLHttpRequest();
var url = 'http://bar.other/resources/post-here/';
var body = '<?xml version="1.0"?><person><name>Thomas Witt</name></person>';
function callOtherDomain(){
if(invocation)
{
invocation.open('POST', url, true);
invocation.setRequestHeader('Content-Type', 'application/xml');
invocation.onreadystatechange = handler;
invocation.send(body);
}
}
Preflighted POST request via CORS - Overview
Client (foo.example) Server (bar.other)
OPTIONS /resources/post-here/ HTTP/1.1
Origin: http://foo.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: Content-Type
HTTP/1.1 200 OK
Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: POST, GET
Access-Control-Allow-Headers: Content-Type
Access-Control-Max-Age: 86400
var invocation = new XMLHttpRequest();
var url = 'http://bar.other/resources/post-here/';
var body = '<?xml version="1.0"?><person><name>Thomas Witt</name></person>';
function callOtherDomain(){
if(invocation)
{
invocation.open('POST', url, true);
invocation.setRequestHeader('Content-Type', 'application/xml');
invocation.onreadystatechange = handler;
invocation.send(body);
}
}
POST /resources/post-here/ HTTP/1.1
Content-Type: text/xml; charset=UTF-8
Origin: http://foo.example
HTTP/1.1 200 OK
Access-Control-Allow-Origin: http://foo.example
PreflightPhaseMainRequestPhase
For every new resource accessed,

a preflight request will be issued.
JavaScript Security: Mastering Cross Domain Communications in complex JS applications
CORS Security
Introducing CSP
CORS vs CSP
CORS
CORS allows a server (bar.other) to give
permission to a site (foo.example) to read
(potentially private) data from a server
(bar.other), using the visitor's browser and
credentials.
CSP
CSP allows a site (foo.example) to prevent itself
from loading (potentially malicious) content
from unexpected sources (e.g. against XSS).
GET /resources/public-data/ HTTP/1.1
Origin: http://foo.example
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Security-Policy: base-uri 'none'; default-
src 'self' 'unsafe-inline' data: https: wss:; object-
src 'none'; script-src 'self' https://beta-
api.scrivito.com; upgrade-insecure-requests;
0,133%
of all 1M Alexa sites implement a CSP
June 2015, "CSP adoption: current status and future prospects", Ming Ying / Shu Qin Li
Example Response: www.scrivito.com (shortened)
HTTP/1.1 200 OK
Connection: keep-alive
Content-Encoding: gzip
Content-Security-Policy: base-uri 'none'; default-src 'self' 'unsafe-inline' data: https: wss:;
object-src 'none'; script-src 'self' https://assets.scrivito.com https://beta-api.scrivito.com https://
maps.googleapis.com https://www.google-analytics.com; upgrade-insecure-requests;
Content-Type: text/html; charset=UTF-8
Date: Wed, 18 Aug 2018 19:00:00 GMT
Implementation
A closer look
CSP Directives
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src
<script>
<img>
<link rel="stylesheet"> <style>
<audio> <video>
<iframe>
@font-face
WebSocket, XMLHttpRequest, EventSource
<embed> <object>
script-src Values
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src
self
unsafe-inline
unsafe-eval
*
none
my.url.com
<script src="/public/mylib.js" />
<script>
alert('Hello JavaScript!');
</script>
setTimeout();
eval();
All of it
None of it
Space-separated (*.js.com | api.js.com | https: | https://api.js.com | data:)
Examples
default-src 'none';
Examples
default-src *;
Examples
default-src 'self';
Our typical CSP
Content-Security-Policy:
base-uri 'none';
default-src 'self' https: wss:;
script-src 'self'
https://api.scrivito.com
https://assets.scrivito.com
https://www.google-analytics.com
https://maps.googleapis.com
https://whatever.service.you.need.com;
object-src 'none';
upgrade-insecure-requests;
CSP Key Takeaways
Every JavaScript application

should have a CSP.
It will most likely break your site
At the first try
script-src is most important
Always set default-src
Test it!
CSP v1 Browser Support
https://caniuse.com/#search=csp
CSP - further reading
developer.mozilla.org/docs/Web/HTTP/CSP
content-security-policy.com
csp-evaluator.withgoogle.com
cspvalidator.org
caniuse.com/#feat=contentsecuritypolicy
#2: postMessage
via iFrame
Cross-domain communication via window.postMessage
<iframe name="receiver"
src="https://bar.other/my.js">
<script>
let win = window.frames.receiver;
win.postMessage("message",
"https://bar.other");
</script>
window.addEventListener("message",
function(event) {
if (event.origin !=
'https://foo.example') {
// Unknown domain? Ignore!
return;
}
alert( "Rcvd: " + event.data );
});
Sender (foo.example) Receiver (bar.other)
That's it
window.postMessage vs CORS/CSP
Advantages
CORS is not very simple, postMessage is
No pre-flight requests
No need to modify HTTP headers
Not convinced?
Google uses it as well in it's Google API JS SDK
<iframe name="receiver"
src="https://bar.other/my.js">
<script>
let win = window.frames.receiver;
win.postMessage("message",
"https://bar.other");
</script>
window.addEventListener("message",
function(event) {
if (event.origin !=
'https://foo.example') {
// Unknown domain? Ignore!
return;
}
alert( "Rcvd: " + event.data );
});
window.postMessage Security
Security caveats
No side can figure out whether malicious code
has been inserted via XSS!
Check-List
Does the browser support postMessage?
Is the message origin correct and known?
Is the message data sanitized and validated?
Are origins matched using strict equality

(no indexOf(".foo.com") > 0)?
Are messages sent without using wildcards in the
origin?
<iframe name="receiver"
src="https://bar.other/my.js">
<script>
let win = window.frames.receiver;
win.postMessage("message",
"https://bar.other");
</script>
window.addEventListener("message",
function(event) {
if (event.origin !=
'https://foo.example') {
// Unknown domain? Ignore!
return;
}
alert( "Rcvd: " + event.data );
});
Bonus Tip:
github.com/jpillora/xdomain
Xdomain
A pure JavaScript CORS alternative.
No server configuration required - just add a
proxy.html on the domain you wish to
communicate with.
– https://github.com/jpillora/xdomain
“
window.postMessage - further reading
developer.mozilla.org/docs/Web/API/Window/postMessage
seclab.stanford.edu/websec/frames/post-message.pdf
labs.detectify.com/2016/12/08/the-pitfalls-of-postmessage/
caniuse.com/#feat=x-doc-messaging
Summary
Always have a CSP!
Use postMessage instead of CORS
when implementing Cross Domain Communication.
There's one more thing…
Bonus Tip #2:
HSTS
(You use TLS, right?)
Example Response: www.scrivito.com
HTTP/1.1 200 OK
Age: 83186
Cache-Control: public, max-age=0, must-revalidate
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 1313
Content-Security-Policy: base-uri 'none'; default-src 'self' 'unsafe-inline' data: https: wss:;
object-src 'none'; script-src 'self' https://assets.scrivito.com https://beta-api.scrivito.com https://
maps.googleapis.com https://www.google-analytics.com; upgrade-insecure-requests;
Content-Type: text/html; charset=UTF-8
Date: Wed, 15 Aug 2018 12:59:51 GMT
Etag: "7cbc5a14104f9d58b476b606e830533b-ssl-df"
Server: Netlify
Strict-Transport-Security: max-age=31536000
Vary: Accept-Encoding
We're hiring.
In Wroclaw!
Your questions?
Can I haz question, please?
JavaScript Security: Mastering Cross Domain Communications in complex JS applications
JavaScript Security: Mastering Cross Domain Communications in complex JS applications
JavaScript Security: Mastering Cross Domain Communications in complex JS applications
JavaScript Security: Mastering Cross Domain Communications in complex JS applications
JavaScript Security: Mastering Cross Domain Communications in complex JS applications
JavaScript Security: Mastering Cross Domain Communications in complex JS applications
JavaScript Security: Mastering Cross Domain Communications in complex JS applications
JavaScript Security: Mastering Cross Domain Communications in complex JS applications
JavaScript Security: Mastering Cross Domain Communications in complex JS applications

More Related Content

PPT
External Data Access with jQuery
PDF
RESTful Web Services
PPTX
Design Beautiful REST + JSON APIs
PPTX
01. http basics v27
PPT
Advanced Json
PPT
Browser security
PDF
Top Ten Web Hacking Techniques (2010)
PPTX
Elegant Rest Design Webinar
External Data Access with jQuery
RESTful Web Services
Design Beautiful REST + JSON APIs
01. http basics v27
Advanced Json
Browser security
Top Ten Web Hacking Techniques (2010)
Elegant Rest Design Webinar

What's hot (20)

PPTX
Evolution Of The Web Platform & Browser Security
PPT
Understanding REST
PPTX
REST API Design for JAX-RS And Jersey
PPTX
Client sidesec 2013-intro
PPTX
Rest presentation
PPTX
Web Security - Cookies, Domains and CORS
PPT
Scalable Reliable Secure REST
PDF
Owasp eee 2015 csrf
PDF
Cross site calls with javascript - the right way with CORS
PDF
Representational State Transfer (REST) and HATEOAS
PPTX
Rest & RESTful WebServices
PPTX
MITM Attacks on HTTPS: Another Perspective
PPTX
Build a Node.js Client for Your REST+JSON API
PPTX
ASP.NET WEB API
PPTX
Proxy log review and use cases
PDF
CSRF: ways to exploit, ways to prevent
PDF
CORS and (in)security
PPTX
CORS - Enable Alfresco for CORS
PDF
DEF CON 27 - BEN SADEGHIPOUR - owning the clout through ssrf and pdf generators
PDF
Secure java script-for-developers
Evolution Of The Web Platform & Browser Security
Understanding REST
REST API Design for JAX-RS And Jersey
Client sidesec 2013-intro
Rest presentation
Web Security - Cookies, Domains and CORS
Scalable Reliable Secure REST
Owasp eee 2015 csrf
Cross site calls with javascript - the right way with CORS
Representational State Transfer (REST) and HATEOAS
Rest & RESTful WebServices
MITM Attacks on HTTPS: Another Perspective
Build a Node.js Client for Your REST+JSON API
ASP.NET WEB API
Proxy log review and use cases
CSRF: ways to exploit, ways to prevent
CORS and (in)security
CORS - Enable Alfresco for CORS
DEF CON 27 - BEN SADEGHIPOUR - owning the clout through ssrf and pdf generators
Secure java script-for-developers
Ad

Similar to JavaScript Security: Mastering Cross Domain Communications in complex JS applications (20)

KEY
Message in a Bottle
PPT
Browser Security
PDF
Krzysztof Kotowicz - Hacking HTML5
PDF
White paper screen
PDF
WebAppSec Updates from W3C
PDF
Security enforcement of Java Microservices with Apiman & Keycloak
PPTX
Cross Origin Resource Sharing (CORS) - Azizul Hakim
PDF
WebCamp: Developer Day: Web Security: Cookies, Domains and CORS - Юрий Чайков...
PDF
XST - Cross Site Tracing
PPTX
W3 conf hill-html5-security-realities
PDF
Hacking HTML5 offensive course (Zeronights edition)
PPTX
JSON based CSRF
PDF
Pushing the Web: Interesting things to Know
PDF
Evolution Of Web Security
PPT
Pentesting web applications
PDF
AJAX Transport Layer
PPTX
JSFoo Chennai 2012
PPT
Web Attacks - Top threats - 2010
DOCX
Copy of cgi
PPTX
Android and REST
Message in a Bottle
Browser Security
Krzysztof Kotowicz - Hacking HTML5
White paper screen
WebAppSec Updates from W3C
Security enforcement of Java Microservices with Apiman & Keycloak
Cross Origin Resource Sharing (CORS) - Azizul Hakim
WebCamp: Developer Day: Web Security: Cookies, Domains and CORS - Юрий Чайков...
XST - Cross Site Tracing
W3 conf hill-html5-security-realities
Hacking HTML5 offensive course (Zeronights edition)
JSON based CSRF
Pushing the Web: Interesting things to Know
Evolution Of Web Security
Pentesting web applications
AJAX Transport Layer
JSFoo Chennai 2012
Web Attacks - Top threats - 2010
Copy of cgi
Android and REST
Ad

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
MIND Revenue Release Quarter 2 2025 Press Release
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Encapsulation theory and applications.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Big Data Technologies - Introduction.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Per capita expenditure prediction using model stacking based on satellite ima...
MYSQL Presentation for SQL database connectivity
Spectral efficient network and resource selection model in 5G networks
Network Security Unit 5.pdf for BCA BBA.
Programs and apps: productivity, graphics, security and other tools
The Rise and Fall of 3GPP – Time for a Sabbatical?
Review of recent advances in non-invasive hemoglobin estimation
MIND Revenue Release Quarter 2 2025 Press Release
The AUB Centre for AI in Media Proposal.docx
Encapsulation theory and applications.pdf
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Big Data Technologies - Introduction.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Digital-Transformation-Roadmap-for-Companies.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

JavaScript Security: Mastering Cross Domain Communications in complex JS applications

  • 1. Berlin, 2018-01-01 JavaScript Security Mastering Cross Domain Communications in complex JS applications Grill.js Wrocław, 2018-08-18, 7pm <thomas.witt@infopark.com> @thomas_witt linkedin.com/in/thomaswitt Thomas Witt Co-Founder
  • 4. We're hiring! Boston · Berlin · Wrocław
  • 8. JS Applications need to exchange data with Backend APIs running on domains other than your own SECURELY
  • 9. Challange Only load code from trusted origins, don't execute malicious JS code, don't let people steal your data.
  • 10. Same Origin Policy For security reasons, browsers restrict cross- origin HTTP requests initiated from within scripts. For example, XMLHttpRequest and the Fetch API follow the same-origin policy. This means that a web application using those APIs can only request HTTP resources from the same origin the application was loaded from. – https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS “
  • 11. How to … Exchange data from JS applications with backend APIs
  • 12. Two ways how to talk to other services CORS postMessage
  • 14. Same Origin Policy A web application using those APIs can only request HTTP resources from the same origin the application was loaded from, unless the response from the other origin includes the right CORS headers. – https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS “
  • 16. Simple GET request via CORS var req = new XMLHttpRequest(); var url = 'http://bar.other/resources/public-data/'; function callOtherDomain() { if(req) { req.open('GET', url, true); req.onreadystatechange = handler; req.send(); } }
  • 17. Simple GET request via CORS var req = new XMLHttpRequest(); var url = 'http://bar.other/resources/public-data/'; function callOtherDomain() { if(req) { req.open('GET', url, true); req.onreadystatechange = handler; req.send(); } } GET /resources/public-data/ HTTP/1.1 Origin: http://foo.example HTTP/1.1 200 OK Access-Control-Allow-Origin: * Client (foo.example) Server (bar.other)
  • 18. The uglyness: Preflight Requests aka not-so-simple-requests
  • 19. Preflighted POST request via CORS var invocation = new XMLHttpRequest(); var url = 'http://bar.other/resources/post-here/'; var body = '<?xml version="1.0"?><person><name>Thomas Witt</name></person>'; function callOtherDomain(){ if(invocation) { invocation.open('POST', url, true); invocation.setRequestHeader('Content-Type', 'application/xml'); invocation.onreadystatechange = handler; invocation.send(body); } }
  • 20. Preflighted POST request via CORS - Overview Client (foo.example) Server (bar.other) OPTIONS /resources/post-here/ HTTP/1.1 Origin: http://foo.example Access-Control-Request-Method: POST Access-Control-Request-Headers: Content-Type HTTP/1.1 200 OK Access-Control-Allow-Origin: http://foo.example Access-Control-Allow-Methods: POST, GET Access-Control-Allow-Headers: Content-Type Access-Control-Max-Age: 86400 var invocation = new XMLHttpRequest(); var url = 'http://bar.other/resources/post-here/'; var body = '<?xml version="1.0"?><person><name>Thomas Witt</name></person>'; function callOtherDomain(){ if(invocation) { invocation.open('POST', url, true); invocation.setRequestHeader('Content-Type', 'application/xml'); invocation.onreadystatechange = handler; invocation.send(body); } } POST /resources/post-here/ HTTP/1.1 Content-Type: text/xml; charset=UTF-8 Origin: http://foo.example HTTP/1.1 200 OK Access-Control-Allow-Origin: http://foo.example PreflightPhaseMainRequestPhase
  • 21. For every new resource accessed,
 a preflight request will be issued.
  • 24. CORS vs CSP CORS CORS allows a server (bar.other) to give permission to a site (foo.example) to read (potentially private) data from a server (bar.other), using the visitor's browser and credentials. CSP CSP allows a site (foo.example) to prevent itself from loading (potentially malicious) content from unexpected sources (e.g. against XSS). GET /resources/public-data/ HTTP/1.1 Origin: http://foo.example HTTP/1.1 200 OK Access-Control-Allow-Origin: * Content-Security-Policy: base-uri 'none'; default- src 'self' 'unsafe-inline' data: https: wss:; object- src 'none'; script-src 'self' https://beta- api.scrivito.com; upgrade-insecure-requests;
  • 25. 0,133% of all 1M Alexa sites implement a CSP June 2015, "CSP adoption: current status and future prospects", Ming Ying / Shu Qin Li
  • 26. Example Response: www.scrivito.com (shortened) HTTP/1.1 200 OK Connection: keep-alive Content-Encoding: gzip Content-Security-Policy: base-uri 'none'; default-src 'self' 'unsafe-inline' data: https: wss:; object-src 'none'; script-src 'self' https://assets.scrivito.com https://beta-api.scrivito.com https:// maps.googleapis.com https://www.google-analytics.com; upgrade-insecure-requests; Content-Type: text/html; charset=UTF-8 Date: Wed, 18 Aug 2018 19:00:00 GMT
  • 29. script-src Values https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src self unsafe-inline unsafe-eval * none my.url.com <script src="/public/mylib.js" /> <script> alert('Hello JavaScript!'); </script> setTimeout(); eval(); All of it None of it Space-separated (*.js.com | api.js.com | https: | https://api.js.com | data:)
  • 33. Our typical CSP Content-Security-Policy: base-uri 'none'; default-src 'self' https: wss:; script-src 'self' https://api.scrivito.com https://assets.scrivito.com https://www.google-analytics.com https://maps.googleapis.com https://whatever.service.you.need.com; object-src 'none'; upgrade-insecure-requests;
  • 36. It will most likely break your site At the first try
  • 37. script-src is most important
  • 40. CSP v1 Browser Support https://caniuse.com/#search=csp
  • 41. CSP - further reading developer.mozilla.org/docs/Web/HTTP/CSP content-security-policy.com csp-evaluator.withgoogle.com cspvalidator.org caniuse.com/#feat=contentsecuritypolicy
  • 43. Cross-domain communication via window.postMessage <iframe name="receiver" src="https://bar.other/my.js"> <script> let win = window.frames.receiver; win.postMessage("message", "https://bar.other"); </script> window.addEventListener("message", function(event) { if (event.origin != 'https://foo.example') { // Unknown domain? Ignore! return; } alert( "Rcvd: " + event.data ); }); Sender (foo.example) Receiver (bar.other)
  • 45. window.postMessage vs CORS/CSP Advantages CORS is not very simple, postMessage is No pre-flight requests No need to modify HTTP headers Not convinced? Google uses it as well in it's Google API JS SDK <iframe name="receiver" src="https://bar.other/my.js"> <script> let win = window.frames.receiver; win.postMessage("message", "https://bar.other"); </script> window.addEventListener("message", function(event) { if (event.origin != 'https://foo.example') { // Unknown domain? Ignore! return; } alert( "Rcvd: " + event.data ); });
  • 46. window.postMessage Security Security caveats No side can figure out whether malicious code has been inserted via XSS! Check-List Does the browser support postMessage? Is the message origin correct and known? Is the message data sanitized and validated? Are origins matched using strict equality
 (no indexOf(".foo.com") > 0)? Are messages sent without using wildcards in the origin? <iframe name="receiver" src="https://bar.other/my.js"> <script> let win = window.frames.receiver; win.postMessage("message", "https://bar.other"); </script> window.addEventListener("message", function(event) { if (event.origin != 'https://foo.example') { // Unknown domain? Ignore! return; } alert( "Rcvd: " + event.data ); });
  • 48. Xdomain A pure JavaScript CORS alternative. No server configuration required - just add a proxy.html on the domain you wish to communicate with. – https://github.com/jpillora/xdomain “
  • 49. window.postMessage - further reading developer.mozilla.org/docs/Web/API/Window/postMessage seclab.stanford.edu/websec/frames/post-message.pdf labs.detectify.com/2016/12/08/the-pitfalls-of-postmessage/ caniuse.com/#feat=x-doc-messaging
  • 52. Use postMessage instead of CORS when implementing Cross Domain Communication.
  • 53. There's one more thing…
  • 54. Bonus Tip #2: HSTS (You use TLS, right?)
  • 55. Example Response: www.scrivito.com HTTP/1.1 200 OK Age: 83186 Cache-Control: public, max-age=0, must-revalidate Connection: keep-alive Content-Encoding: gzip Content-Length: 1313 Content-Security-Policy: base-uri 'none'; default-src 'self' 'unsafe-inline' data: https: wss:; object-src 'none'; script-src 'self' https://assets.scrivito.com https://beta-api.scrivito.com https:// maps.googleapis.com https://www.google-analytics.com; upgrade-insecure-requests; Content-Type: text/html; charset=UTF-8 Date: Wed, 15 Aug 2018 12:59:51 GMT Etag: "7cbc5a14104f9d58b476b606e830533b-ssl-df" Server: Netlify Strict-Transport-Security: max-age=31536000 Vary: Accept-Encoding
  • 58. Can I haz question, please?