views:

226

answers:

3

For a specific web application, I would like to implement a scripting language to assist in partially automating certain tasks. I cannot modify the web application itself, but I can send JavaScript code to each client that connects. Because multiple users of this publicly accessible site will be creating and posting scripts, I cannot use JavaScript itself for security reasons.

I would like some tips on whether such a scripting language implementation exists, or if not, tips on creating it. The focus should be on ease of text processing, Ajax requests, and implementation.

Edit: Some have questioned why I would want to do such a thing. The web application is a wiki, and I do not want to allow any administrative actions. Wikis allow other actions to be easily undone. I can't install anything such as Google Caja on the server.

Edit 2: Here is an example of what such a script would need to be able to do. This example is taken from Wikipedia's procedure for requesting article deletion.

  1. Ask the user for the name of a wiki page and a good reason to delete it.
  2. Get the source code of that page, prepend some text (a deletion notice) to it, then save it back.
  3. Create a new page (its name including that of the first page) with some text that includes the user's reason to delete the page.
  4. Get the list of users who edited the page and notify the first one (again, by editing a specific page) that the page he created is about to be deleted.

Edit 3: I have answered my own question, but not to my satisfaction. If anyone could suggest either

  1. A scripting language implementation I have overlooked; or
  2. Tips on designing a simple programming language and interpreter for the application I mention

I would appreciate it.

+1  A: 

You could just sandbox; that is, scope in a couple of key variables so that the user's code is unable to access unsafe objects.

var execSandboxedJS = function (jsCode) {
    var window = document.getElementById('myRootElement');
    var document = window;
    eval(jsCode);
};

Though, allowing user code to make ajax requests is, in itself, inherently unsafe. I would reconsider the sanity of the project if that's what's called for.

Fordi
Not good enough. For starters, that can easily be broken out of by using `self` instead of window. The way JavaScript is, it would be extremely hard to patch all the holes - see http://code.google.com/p/google-caja/wiki/AttackVectors.
idealmachine
+2  A: 

ADsafe is supposed to be a secure subset of JavaScript. It consists of a runtime library (~20 KB minified) and a verifier (which happens to be JSLint).

  • Pros: JSLint is a JavaScript program, so user scripts can be verified within the web browser. It is possible to download JSLint's source code. And it already exists as opposed to designing a new programming language and interpreter.
  • Cons: Douglas Crockford decided to add "The Software shall be used for Good, not Evil" to what would otherwise be the "MIT" license. The possible GPL incompatibility makes JSLint/ADsafe unsuitable for my application. Writing AJAX-using scripts using the asynchronous event model of JavaScript (particularly dealing with the "edittoken" anti-CSRF mechanism of MediaWiki) might be difficult, a problem which a custom interpreter could address.
idealmachine
+2  A: 

Here's an implementation of Tcl in javascript: Tcl in Javascript.

Here's the source: tcl.js.

And here's code implementing a live console in your browser to play with: A little tcl.js console

Tcl may not be your cup of tea but the implementation looks fairly simple straightforward. This is mainly because tcl itself is such a simple language. You can use it to get ideas on how to implement variables and functions.

Hint: in tcl, control structures are functions so look at where built-in functions are implemented to see the implementation of for, while and foreach.

slebetman
Thanks, I didn't know Tcl had been implemented in JavaScript.
idealmachine