1
edit
(added {{draft}} to header) |
m (__exposedProps__ should use colon, not equal sign) |
||
(4 intermediate revisions by one other user not shown) | |||
Line 29: | Line 29: | ||
/* Do something here that requires chrome privileges. */ | /* Do something here that requires chrome privileges. */ | ||
} | } | ||
var sandbox = Cu.Sandbox("http://www.mozilla.org"); | var sandbox = Cu.Sandbox("http://www.mozilla.org"); | ||
Line 37: | Line 36: | ||
In the above example, <tt>foo()</tt> is wrapped by a COW when accessed by sandboxed code executed via <tt>Components.utils.evalInSandbox()</tt>. The object <tt>{bar: 5}</tt> is wrapped in an <tt>XPCSafeJSObjectWrapper</tt> before being passed into <tt>foo()</tt>. | In the above example, <tt>foo()</tt> is wrapped by a COW when accessed by sandboxed code executed via <tt>Components.utils.evalInSandbox()</tt>. The object <tt>{bar: 5}</tt> is wrapped in an <tt>XPCSafeJSObjectWrapper</tt> before being passed into <tt>foo()</tt>. | ||
=== COWing Objects === | === COWing Objects === | ||
By default, Chrome objects passed into content space are completely opaque: no information can be accessed from them, and no properties can be defined on them. | By default, non-function Chrome objects passed into content space are completely opaque: no information can be accessed from them, and no properties can be defined on them. | ||
When a non-writable property is written to, a security exception will be raised. However, when a non-readable property is accessed, its value is <tt>undefined</tt>: a security exception isn't thrown because we don't want to break code that relies on [http://en.wikipedia.org/wiki/Duck_typing duck typing]. | |||
To bypass this default behavior, individual properties can be exposed by defining a <tt>__exposedProps__</tt> property on the object, like so: | |||
<pre class="brush:js;"> | <pre class="brush:js;"> | ||
Line 54: | Line 49: | ||
var MyObj = { | var MyObj = { | ||
__exposedProps__ | __exposedProps__: {foo: "r", bar: "rw"}, | ||
foo: function foo(obj) { | foo: function foo(obj) { | ||
Line 64: | Line 59: | ||
baz: "I am protected information" | baz: "I am protected information" | ||
} | } | ||
</pre> | </pre> | ||
In the above example, <tt>MyObj.foo()</tt> can be accessed but not assigned to | In the above example, <tt>MyObj.foo()</tt> can be accessed but not assigned to, <tt>foo()</tt> itself is callable from content, and <tt>MyObj.bar</tt> is both readable and writable, while <tt>MyObj.baz</tt> can't be accessed at all. | ||
All properties that are exposed to content are enumerable by content as well. | All properties that are exposed to content are enumerable by content as well. | ||
Getters and setters on exposed properties are automatically called as necessary. | |||
If an exposed property is writable by content, it is deletable by content as well. | |||
'''TODO:''' | '''TODO:''' What should the default <tt>toString()</tt> method of a COW'ed object with no metadata yield? | ||
'''TODO:''' Discuss what to do with native objects that get exposed (like the Sidebar object). | '''TODO:''' Discuss what to do with native objects that get exposed (like the Sidebar object). |
edit