6

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.
Orbit
  • 2,985
  • 9
  • 49
  • 106

2 Answers2

8

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
  });
};
Guig
  • 9,891
  • 7
  • 64
  • 126
2

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

JeremyK
  • 3,240
  • 1
  • 11
  • 24
  • So how would I load the widget in an `iframe`? Would I create a completely separate Meteor application and use its url for the `iframes` `src` attribute, or is there a better way? – Orbit Dec 15 '16 at 22:40
  • Yes, running in a sandbox allows you to develop this as a separate (meteor) application. Yes, it would be have it's own url that is loaded into the iframe. – JeremyK Dec 15 '16 at 22:48
  • The application used by the `iframe` would be publicly visible and people would be able to navigate to it like a regular site. The issue is the widget is designed to be used as exactly that, a widget, so I wouldnt want visitors the the "full" site. How is this scenario handled? – Orbit Dec 15 '16 at 23:00
  • http://stackoverflow.com/questions/925039/detect-iframe-embedding-in-javascript and http://stackoverflow.com/questions/326069/how-to-identify-if-a-webpage-is-being-loaded-inside-an-iframe-or-directly-into-t provide suggestions on how to detect if your widget/app is running in a iframe or not – JeremyK Dec 15 '16 at 23:09
  • However generally I would see this as beneficial for development, and by using an obscure route you can discourage casual visitors to the app standalone. e.g. `https://chatwidget.com/27tGpRtMWYqPpFkDN`. This url would not be linked to anywhere, so unless someone is reading your the source on the host app, should never see it. – JeremyK Dec 15 '16 at 23:13
  • So to summarize, the `iframe` approach allows you to sandbox your app, providing security to your customers. Your own app's security needs to be server side - where you [Never Trust the Client](http://security.stackexchange.com/questions/105389/dont-trust-the-client) – JeremyK Dec 15 '16 at 23:15
  • That being said, almost all third parties ask you to include some of their code that loads the iframe, instead of setting the iframe directly. This let them expose some API to communicate with the iframe that you control and can design safely. – Guig Dec 19 '16 at 23:00