Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicating with a web widget-Meteor, React, Node

I'm building a chat dashboard and widget with which a customer should be able to put the widget into their page. Some similar examples would be Intercom or Drift.

Currently, the "main" application is written in Meteor.js (it's front end is in React). I've written a <Widget /> component and thrown it inside a /widget directory. Inside this directory, I also have an index.jsx file, which simply contains the following:

import React from 'react';

import ......

ReactDOM.render(
  <Widget/>,
  document.getElementById('widget-target')
);

I then setup a webpack configuration with an entry point at index.jsx and when webpack is run spits out a bundle.js in a public directory.

This can then be included on another page by simply including a script and div:

<script src="http://localhost:3000/bundle.js" type="text/javascript"></script>
<div id="widget-target"></div>

A few questions:

  1. What is wrong with this implementation? Are their any security issues to be aware of? Both the examples linked earlier seem make use of an iframe in one form or another.
  2. What is the best way to communicate with my main meteor application? A REST API? Emit events with Socket.io? The widget is a chat widget, so I need to send messages back and forth.
  3. How can I implement some sort of unique identifier/user auth for the user and the widget? Currently, the widget is precompiled.
like image 503
Orbit Avatar asked Dec 05 '16 05:12

Orbit


People also ask

How to send and receive messages in real-time using React widget?

Create a new widget for agent, this widget is for the agent, who can see all messages and can reply. Add the widget ID from the right sidebar in the dashboard in the .env.local file for REACT_APP_W2 key. Send and receive messages in real-time. Create a file called Client.js inside the src folder and paste the following:

What is the react chat widget?

That’s when we decided to create the React Chat Widget. An open source widget which can be added to your React App and voilá, you now have your own chat… well, not entirely, but we’ll get there soon.

How do I create a node app using React-node-app?

First create a folder for your project, called react-node-app (for example). Then, drag that folder into your code editor. To create our Node project, run the following command in your terminal: This will create a package.json file which will allow us to keep track of all our app scripts and manage any dependencies our Node app needs.

How to add chat widgets in cometchat?

Go to Chat widgets section in the dashboard of CometChat. Create a new widget for user, this widget will be visible to the new users who come to the website. Add the widget ID from the right sidebar in the dashboard in the .env.local file for REACT_APP_W1 key. Click on customize from the right sidebar and disable all options except the above one.


2 Answers

1 What is wrong with this implementation? Are their any security issues to be aware of? Both the examples linked earlier seem make use of an iframe in one form or another.

As @JeremyK mentioned, you're safer within an iFrame. That being said, there's a middle route that many third parties (Facebook, GA, ...) are using, including Intercom:

  • ask users to integrate your bundled code within their webpage. It's then up to you to ensure you're not introducing a security vulnerability on their site. This code will do two things:
  • take care of setting up an iframe, where the main part of your service is going to happen. You can position it, style it etc. This ensure that all the logic happening in the iframe is safe and you're not exposed.
  • expose some API between your customer webpage and your iframe, using window messaging.
  • the main code (the iframe code) is then loaded by this first script asynchronously, and not included in it.

For instance Intercom ask customers to include some script on their page: https://developers.intercom.com/docs/single-page-app#section-step-1-include-intercom-js-library that's pretty small (https://js.intercomcdn.com/shim.d97a38b5.js). This loads extra code that sets the iFrame and expose their API that will make it easy to interact with the iFrame, like closing it, setting user properties etc.

2 What is the best way to communicate with my main meteor application? A REST API? Emit events with Socket.io? The widget is a chat widget, so I need to send messages back and forth.

You've three options:

  • Build your widget as an entire Meteor app. This will increase the size of the code that needs to be loaded. In exchange for the extra code, you can communicate with your backend through the Meteor API, like Meteor.call, get the reactivity of all data (for instance if you send a response to a user through your main Meteor application, the response would pop up on the client with no work to do as long as they are on the same database (no need to be on the same server)), and the optimistic UI. In short you've all what Meteor offers here, and it's probably going to be easier to integrate with your existing backend that I assume is Meteor.
  • Don't include Meteor. Since you're building a chat app, you'll probably need socket.io over a traditional REST API. For sure you can do a mix of both
  • Use Meteor DDP. (it's kind of like socket.io, but for Meteor. Meteor app use that for all requests to the server) This will include less things that the full Meteor and probably be easier to integrate to your Meteor backend than a REST API / socket.io, and will be some extra work over the full Meteor.

3 How can I implement some sort of unique identifier/user auth for the user and the widget?

This part should probably do some work on the customer website (vs in your iframe) so that you can set cookies on his page, and send that data to your iframe that's gonna talk to your server and identify the user. Wether you use artwells:accounts-guest (that's based on meteor:accounts-base) is going to depend on wether you decide to include Meteor in your iframe.

If you don't have Meteor in your iframe, you can do something like:

  • handle user creation yourself, by simply doing on your server

.

const token = createToken();
Users.insert({ tokens: [token] });
// send the token back to your iframe
// and set is as a cookie on your customer website
  • then for each call to your server, on your iframe:

.

let token;
const makeRequest = async (request) => {
    token = token || getCookieFromCustomerWebsite();
    // pass the token to your HTTP / socket.io / ... request.
    // in the header of whatever
    return await callServer(token, request);
};
  • in the server have a middleware that sets the user. Mine looks like:

.

const loginAs = (userId, cb) => {
  DDP._CurrentInvocation.withValue(new DDPCommon.MethodInvocation({
    isSimulation: false,
    userId,
  }), cb);
};

// my middleware that run on all API requests for a non Meteor client
export const identifyUserIfPossible = (req, res, next) => {
  const token = req.headers.authorization;
  if (!token) {
    return next();
  }
  const user = Users.findOne({ tokens: token });
  if (!user) {
    return next();
  }

  loginAs(user._id, () => {
    next();
    // Now Meteor.userId() === user._id from all calls made on that request
    // So you can do Meteor.call('someMethod') as you'd do on a full Meteor stack
  });
};
like image 135
Guig Avatar answered Oct 06 '22 07:10

Guig


Asking your customers to embed your code like this doesn't follow the principles of Security by Design.

From their point of view, you are asking them to embed your prebundled code into their website, exposing their site up to any hidden security risks (inadvertent or deliberately malicious) that exist in your code which would have unrestricted access to their website's DOM, localstorage, etc.

This is why using an iframe is the prefered method to embed third party content in a website, as that content is sandboxed from the rest of it's host site.

Further, following the security principle of 'Least Privilege' they (with your guidance/examples) can set the sandbox attribute on the iframe, and explicitly lockdown via a whitelist the privileges the widget will have.

Loading your widget in an iframe will also give you more flexibility in how it communicates with your servers. This could now be a normal meteor client, using meteor's ddp to communicate with your servers. Your other suggestions are also possible.

User auth/identification depends on the details of your system. This could range from using Meteor Accounts which would give you either password or social auth solutions. Or you could try an anonymous accounts solution such as artwells:accounts-guest.

html5rocks article on sandboxed-iframes

like image 34
JeremyK Avatar answered Oct 06 '22 07:10

JeremyK