Skip to content

Commit

Permalink
fix: only receive messages from intended app
Browse files Browse the repository at this point in the history
closes #136
  • Loading branch information
juancarlosfarah committed Jul 21, 2019
1 parent 303c49e commit 7b3cfbc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
52 changes: 31 additions & 21 deletions src/components/phase/PhaseApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PATCH_APP_INSTANCE_RESOURCE,
GET_APP_INSTANCE,
POST_APP_INSTANCE_RESOURCE,
APP_INSTANCE_RESOURCE_TYPES,
} from '../../types';
import {
getAppInstanceResources,
Expand Down Expand Up @@ -62,36 +63,45 @@ class PhaseApp extends Component {
const { appInstanceId: messageAppInstanceId } = data;

// only post message to intended app instance
if (componentAppInstanceId !== messageAppInstanceId) {
return;
}

const message = JSON.stringify(data);
if (componentAppInstanceId === messageAppInstanceId) {
const message = JSON.stringify(data);

if (this.iframe.contentWindow.postMessage) {
this.iframe.contentWindow.postMessage(message, '*');
} else {
console.error('unable to find postMessage');
if (this.iframe.contentWindow.postMessage) {
this.iframe.contentWindow.postMessage(message, '*');
} else {
console.error('unable to find postMessage');
}
}
};

handleReceiveMessage = event => {
try {
const { dispatchGetAppInstance } = this.props;
const { dispatchGetAppInstance, appInstance } = this.props;

// get app instance id in message
const { id: componentAppInstanceId } = appInstance || {};
const { type, payload } = JSON.parse(event.data);
let { id: messageAppInstanceId } = payload;
if (APP_INSTANCE_RESOURCE_TYPES.includes(type)) {
({ appInstanceId: messageAppInstanceId } = payload);
}

switch (type) {
case GET_APP_INSTANCE_RESOURCES:
return getAppInstanceResources(payload, this.postMessage);
case POST_APP_INSTANCE_RESOURCE:
return postAppInstanceResource(payload, this.postMessage);
case PATCH_APP_INSTANCE_RESOURCE:
return patchAppInstanceResource(payload, this.postMessage);
case GET_APP_INSTANCE:
return dispatchGetAppInstance(payload, this.postMessage);
default:
return false;
// only receive message from intended app instance
if (componentAppInstanceId === messageAppInstanceId) {
switch (type) {
case GET_APP_INSTANCE_RESOURCES:
return getAppInstanceResources(payload, this.postMessage);
case POST_APP_INSTANCE_RESOURCE:
return postAppInstanceResource(payload, this.postMessage);
case PATCH_APP_INSTANCE_RESOURCE:
return patchAppInstanceResource(payload, this.postMessage);
case GET_APP_INSTANCE:
return dispatchGetAppInstance(payload, this.postMessage);
default:
return false;
}
}
return false;
} catch (e) {
console.error(e);
return false;
Expand Down
16 changes: 16 additions & 0 deletions src/types/appInstanceResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,19 @@ export const DELETE_APP_INSTANCE_RESOURCE_SUCCEEDED =
'DELETE_APP_INSTANCE_RESOURCE_SUCCEEDED';
export const DELETE_APP_INSTANCE_RESOURCE_FAILED =
'DELETE_APP_INSTANCE_RESOURCE_FAILED';

// all of the app instance resource types
export const APP_INSTANCE_RESOURCE_TYPES = [
GET_APP_INSTANCE_RESOURCES,
GET_APP_INSTANCE_RESOURCE,
POST_APP_INSTANCE_RESOURCE,
PATCH_APP_INSTANCE_RESOURCE,
DELETE_APP_INSTANCE_RESOURCE,
GET_APP_INSTANCE_RESOURCES_FAILED,
POST_APP_INSTANCE_RESOURCE_SUCCEEDED,
POST_APP_INSTANCE_RESOURCE_FAILED,
PATCH_APP_INSTANCE_RESOURCE_SUCCEEDED,
PATCH_APP_INSTANCE_RESOURCE_FAILED,
DELETE_APP_INSTANCE_RESOURCE_SUCCEEDED,
DELETE_APP_INSTANCE_RESOURCE_FAILED,
];

0 comments on commit 7b3cfbc

Please sign in to comment.