From 49a298551512696ebf3b96a71d1aab5c9fd0bc66 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Tue, 5 Dec 2017 00:08:17 +0000 Subject: [PATCH] Register message endpoints. Store endpoint origins only. --- src/WidgetMessaging.js | 29 ++++++++++++++++++++---- src/components/views/elements/AppTile.js | 2 ++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/WidgetMessaging.js b/src/WidgetMessaging.js index 7addbcae67..a5809bfee9 100644 --- a/src/WidgetMessaging.js +++ b/src/WidgetMessaging.js @@ -111,6 +111,8 @@ Example: */ +import URL from 'url'; + const WIDGET_API_VERSION = '0.0.1'; // Current API version const SUPPORTED_WIDGET_API_VERSIONS = [ '0.0.1', @@ -160,12 +162,20 @@ function stopListening() { * @param {string} endpointUrl Widget wurl origin (protocol + (optional port) + host) */ function addEndpoint(widgetId, endpointUrl) { - const endpoint = new WidgetMessageEndpoint(widgetId, endpointUrl); - if (global.mxWidgetMessagingMessageEndpoints && global.mxWidgetMessagingMessageEndpoints.length > 0) { - if (global.mxWidgetMessagingMessageEndpoints.filter(function(ep) { + const u = URL.parse(endpointUrl); + if (!u || !u.protocol || !u.host) { + console.warn("Invalid origin"); + return; + } + + const origin = u.protocol + '//' + u.host; + const endpoint = new WidgetMessageEndpoint(widgetId, origin); + if (global.mxWidgetMessagingMessageEndpoints) { + if (global.mxWidgetMessagingMessageEndpoints.some(function(ep) { return (ep.widgetId === widgetId && ep.endpointUrl === endpointUrl); - }).length > 0) { + })) { // Message endpoint already registered + console.warn("Endpoint already registered"); return; } global.mxWidgetMessagingMessageEndpoints.push(endpoint); @@ -179,10 +189,19 @@ function addEndpoint(widgetId, endpointUrl) { * @return {boolean} True if endpoint was successfully removed */ function removeEndpoint(widgetId, endpointUrl) { + const u = URL.parse(endpointUrl); + if (!u || !u.protocol || !u.host) { + console.warn("Invalid origin"); + return; + } + + console.warn("Origin url:", u); + const origin = u.protocol + '//' + u.host; + if (global.mxWidgetMessagingMessageEndpoints && global.mxWidgetMessagingMessageEndpoints.length > 0) { const length = global.mxWidgetMessagingMessageEndpoints.length; global.mxWidgetMessagingMessageEndpoints = global.mxWidgetMessagingMessageEndpoints.filter(function(endpoint) { - return (endpoint.widgetId != widgetId || endpoint.endpointUrl != endpointUrl); + return (endpoint.widgetId != widgetId || endpoint.endpointUrl != origin); }); return (length > global.mxWidgetMessagingMessageEndpoints.length); } diff --git a/src/components/views/elements/AppTile.js b/src/components/views/elements/AppTile.js index 11b3462092..1dbb7af586 100644 --- a/src/components/views/elements/AppTile.js +++ b/src/components/views/elements/AppTile.js @@ -153,6 +153,7 @@ export default React.createClass({ componentWillMount() { WidgetMessaging.startListening(); + WidgetMessaging.addEndpoint(this.props.id, this.props.url); window.addEventListener('message', this._onMessage, false); this.setScalarToken(); }, @@ -206,6 +207,7 @@ export default React.createClass({ componentWillUnmount() { WidgetMessaging.stopListening(); + WidgetMessaging.removeEndpoint(this.props.id, this.props.url); window.removeEventListener('message', this._onMessage); },