# HG changeset patch # User Atul Varma # Date 1252367621 25200 # Node ID dace90a7f5e3baae8deaf653d7ac8cb56ee52124 # Parent 35ab0ad3c294addac5e7c9736cf9dd271364e078 Added a 'jsexposed' decorator and, for security purposes, required that __jsexposed__ be True on all callables passed into JS-land as well. diff -r 35ab0ad3c294 -r dace90a7f5e3 pydershell/pydershell.py --- a/pydershell/pydershell.py Mon Sep 07 16:25:58 2009 -0700 +++ b/pydershell/pydershell.py Mon Sep 07 16:53:41 2009 -0700 @@ -194,6 +194,31 @@ lines.insert(0, "Traceback (most recent call last):") return '\n'.join(lines) +def jsexposed(name=None, on=None): + """ + Decorator used to expose the decorated function or method to + untrusted JS. + + 'name' is an optional alternative name for the function. + + 'on' is an optional SafeJsObjectWrapper that the function can be + automatically attached as a property to. + """ + + if callable(name): + func = name + func.__jsexposed__ = True + return func + + def make_exposed(func): + if name: + func.__name__ = name + func.__jsexposed__ = True + if on: + on[func.__name__] = func + return func + return make_exposed + class JsExposedObject(object): """ Trivial base/mixin class for any Python classes that choose to @@ -337,6 +362,10 @@ # It's already wrapped, just unwrap it. return value.wrapped_jsobject elif callable(value): + if not (hasattr(value, '__jsexposed__') and + value.__jsexposed__): + raise ValueError("Callable isn't configured for exposure " + "to untrusted JS code") return self.__wrap_pycallable(value) elif isinstance(value, JsExposedObject): return self.__wrap_pyinstance(value) diff -r 35ab0ad3c294 -r dace90a7f5e3 pydershell/test.py --- a/pydershell/test.py Mon Sep 07 16:25:58 2009 -0700 +++ b/pydershell/test.py Mon Sep 07 16:53:41 2009 -0700 @@ -5,38 +5,38 @@ import sys import pydermonkey -from pydershell import JsSandbox, JsExposedObject +from pydershell import JsSandbox, JsExposedObject, jsexposed if __name__ == '__main__': sandbox = JsSandbox() class Baz(JsExposedObject): + @jsexposed def woozle(self, blap): return blap + 5 - woozle.__jsexposed__ = True + @jsexposed(on=sandbox.root) def baz(): return Baz() - sandbox.root.baz = baz + @jsexposed(on=sandbox.root) def bar(obj): print obj.bar() - sandbox.root.bar = bar + @jsexposed(on=sandbox.root) def foo(callback): return callback() - sandbox.root.foo = foo + @jsexposed(on=sandbox.root) def ensureBaz(baz): if not isinstance(baz, Baz): print "Uhoh" else: print "ok" - sandbox.root.ensureBaz = ensureBaz + @jsexposed(name="print", on=sandbox.root) def jsprint(string): print string - sandbox.root['print'] = jsprint retval = sandbox.run_script('test.js')