Giter VIP home page Giter VIP logo

jsandbox's Introduction

DISCONTINUED - Not safe; Do not use

JSandbox

Version 0.2.3

JSandbox is an open source JavaScript sandboxing library that makes use of HTML5 web workers. JSandbox makes it possible to run untrusted JavaScript without having to worry about any potential dangers.

Getting Started

  1. Download JSandbox.
  2. Include <link rel="jsandbox" href="path/to/jsandbox-worker.js" /> anywhere in your document. I recommend putting it in the document's <head>.
  3. Place <script type="text/javascript" src="path/to/jsandbox.js"></script> anywhere after the <link> tag.
  4. Read the API documentation below.

Example Code

This example code demonstrates the JSandbox API.

Tested Working Browsers

  • Firefox 3.5+
  • Google Chrome 4+

API

Worker script location

Instead of using a <link> tag, you may define JSandbox.url to specify the location of the JSandbox worker script.

Methods

All of these methods can be accessed on the JSandbox constructor (in one-use sandboxes) and JSandbox instances:

eval(options)
eval()s options.data. If options.callback is a function, it is passed the results as long as no errors occur. If options.onerror is a function and an error occurs, it is passed the error object. The code is eval()ed in a top-level pseudo-function-scope. If you define a variable using a var statement, the variable is private to the eval. this is still the global object. If this method is called on JSandbox, the JSandbox object is returned. Otherwise, the ID of the request is returned.
exec(options)
Executes code in a faster method than eval, but does not pass a return value to the callback function (though the function is still called if defined). Unlike eval, the code is run in the global scope (var statements affect this).
load(options)
If options.data is a string, options.data will attempt to be loaded in the sandbox. If options.data is an array, every string it contains will attempt be loaded. If options.onerror is a function and an error is thrown while parsing a script or a script could not be resolved, options.onerror is passed the error object. Otherwise, options.callback is called when the scripts are finished loading.

Instance-only methods

These methods can only be on JSandbox instances:

abort(requestID)
Aborts a pending request with the ID, requestID.
terminate()
Terminates the worker thread and any pending requests are aborted. You cannot use the JSandbox instance on which you called this method after it is called.

options object

The following are all of the properties that may be included in an options object.

data [Required]
In the case of eval and exec, it is the code to execute. In the case of load, it is an array of the script(s) to load. If you only need to load one script, just pass a string instead.
input
The input data available to the code via the input variable. The input should be JSON-convertible.
callback
The callback to pass the return value of the executed code if no exceptions were thrown.
onerror
The callback to pass an exception if one is thrown upon executing the code.

Alternative syntax

Any method that takes an options object can also be called using the following positional-arguments syntax:

someMethod(data [, callback] [, input] [, onerror]);

The global JSandbox object can also be referenced as Sandbox.

Tracking image

jsandbox's People

Contributors

amrdraz avatar eligrey avatar merlinran avatar wizzwizz4 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsandbox's Issues

This is not safe!

Web Workers are not sandboxed! They run with the same origin as the host page and, as such, have access to all the same cookies, IndexedDB (still not implemented in all browsers, but being added), and the ability to make trusted AJAX requests to the origin domain including POSTs.

Please consider rebranding the purpose of this project because it can not be used to run untrusted code safely and anyone using ti as such is putting their site and their users in danger!

ToString function is not clonable in postMessage

Including toString method causes DataCloneError: The object could not be cloned. in both jsandbox and worker in FF20 (at least).

this[$worker].postMessage({
  toString : jsonStringifyThis
});

Safari Type Issue

It seems that Safari 6.0.5 (8536.30.1) cannot do

    delete self.onmessage; // in case the code defined it

Checking first resolved this problem for me:

    if (self.onmessage)  delete self.onmessage;

I'm not sure why this happens. Deleting properties that don't exist works fine:

    var a = {};
    delete a.b; // true

"load" call seems to not do anything at all - no errors, nothing.

I made a fairly simple test case (based on the example gist found in README):

JSandbox.url = './test.js';
sandbox   = new JSandbox();
something_happened = false;

var options = {
    data: [JSandbox.url],
    callback: function() { something_happened = true; },
    onerror: function()  { something_happened = true; }
}

sandbox.load(options);

// Keep checking for a change.
setInterval(function() {
    console.log(something_happened);
}, 1000);

With both a success and error callback defined, I would expect to see something happen no matter what. However, nothing happens. And there are no errors in the JavaScript console.

I've hosted this test case on Github here.

Will the sandboxing still work when using a WebWorker polyfill?

JSandbox looks like a very cool library for use in sandboxing 3rd party JavaScript.

However, as cross-browser support for HTML5 WebWorkers is still up-and-coming (especially for IE and mobile browsers), I was curious if any existing WebWorker polyfill (e.g. IEWebWorker, WebWorker API Shim on top of Google Gears, fakeworker.js, etc.), would still provide an actual sandbox environment or if its use would allow "badness" to seep out.

Can you confirm one way or the other? Thanks!

Chrome worker error when evaled code throws an error

response.error = e; in the messageHandler throws DataCloneError: The object could not be cloned. in Google Chrome (30.0.1568.2 dev).

This problem can be resolved by manually cloning the error instead:

        response.error = { 
            name: e.name,
            message: e.message,
            stack: e.stack
                            ...
        };

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.