javascriptsandboxgoogle-caja

Restrict third party scripts - Google Caja?


I'm building a CMS system and I want to restrict third party javascript includes that users can embed within their site.

I will use tracking as an example scenario as it best describes what I want to achieve.

  1. I create a white-list of authorized scripts (i.e Google Analytics, Optimizely)
  2. A user enters a new script and it isn't on the white-list, i.e. Tableau, and the core functionality would be blocked/restricted/reduced.

Defining "blocked": Where the script cannot interact with any elements on the page (i.e. watch form elements and catch data from submitted forms) or user visits where the data is obfuscated and fundamentally useless.

So, with the above example in mind I would expect that the functionality of Tableau to be reduced. Things like Heatmap reports would no longer work (as we are blocking interaction with the DOM the client events should not work). Also I assume looking at the architecture of Caja a visit would appear to come from the Cajoled service and not from the users browser?

Looking at Google Caja it seems it may be the choice, but the focus is always around entire HTML/CSS/JS (a gadget/app) where what I'm looking for is slightly less reduced in that sense. Perhaps there is an alternative solution

Is it something that can be setup to be generic, or does it need specific work for every white-list/black-list item?

EDIT - Looking at it, there are various types of sandboxing scripts available but they all look relatively unsupported or in per-production stages of their development.

JSandbox https://github.com/eligrey/jsandbox

JS.js https://github.com/jterrace/js.js


Solution

  • Caja will not do all of what you want “out of the box”, but there's a good chance it has useful components for your sandbox.

    Caja itself does not at all support your proposed “whitelisted scripts” model; the guiding principle is that no code should be “trusted” more than it needs to do its job, and so we focus on authorization based on the intended use of a program, not the author of it. If you want a whitelist of full-page-access scripts then that is something you will need to build yourself, but it wouldn't be that complex a problem and probably needs to integrate closely with your CMS anyway.

    Now, supporting running scripts with “reduced” functionality is something Caja can help with. However, it's unclear exactly what sort of reduction you want.

    If you are still interested in using Caja, I recommend you start a discussion on google-caja-discuss and describe exactly what kind of reduced functionality you want to support.

    Disclaimer: I work for Google on the Caja project, but this post is entirely my own perspective and opinion.