Bug 946067 part 3. Adjust codegen to allow cross-origin this values based on WebIDL annotations. r=peterv

This commit is contained in:
Boris Zbarsky 2013-12-09 10:34:04 -05:00
parent cf5a96ff48
commit b3607329f0
3 changed files with 177 additions and 24 deletions

View File

@ -35,6 +35,7 @@ support-files =
[test_setting_opener.html]
[test_url.html]
[test_window_constructor.html]
[test_window_cross_origin_props.html]
[test_window_enumeration.html]
[test_window_extensible.html]
[test_window_indexing.html]

View File

@ -0,0 +1,102 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=946067
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 946067</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript">
/** Test for Bug 946067 **/
SimpleTest.waitForExplicitFinish();
function doGet(prop, thisObj) {
return Object.getOwnPropertyDescriptor(window, prop).get.call(thisObj);
}
function doSet(prop, thisObj, value) {
return Object.getOwnPropertyDescriptor(window, prop).set.call(thisObj, value);
}
window.onload = function() {
frames[0].focus();
is(document.activeElement.id, "x", "Should have focused first subframe");
frames[0].blur();
window.focus.call(frames[1]);
is(document.activeElement.id, "y", "Should have focused second subframe");
window.blur.call(frames[1]);
frames[0].close();
is(frames[0].closed, false, "Subframe is not closed");
window.close.call(frames[0]);
is(doGet("closed", frames[0]), false, "Subframe is still not closed");
is(frames[0].frames, frames[0], "window.frames === window");
is(doGet("frames", frames[0]), frames[0],
"Really, window.frames === window");
try {
frames[0].frames = 1;
ok(false, "Should throw when setting .frames");
} catch (e) {
ok(true, "Should throw when setting .frames");
}
try {
doSet("frames", frames[0], 1);
ok(false, "Should still throw when setting .frames");
} catch (e) {
ok(true, "Should still throw when setting .frames");
}
// Just check whether we can get the location without throwing:
is(frames[0].location, doGet("location", frames[0]),
"Should be same Location object");
is(frames[0].length, 0, "404 page has no subframes");
is(doGet("length", frames[0]), 0, "404 page has no subframes");
is(frames[0].opener, null, "subframe has no opener");
is(doGet("opener", frames[0]), null, "subframe still has no opener");
is(frames[0].parent, window, "subframe has us as parent");
is(doGet("parent", frames[0]), window, "subframe still has us as parent");
// Check that postMessage doesn't throw
frames[0].postMessage(null, "*");
is(frames[0].self, frames[0], "self should work");
is(doGet("self", frames[0]), frames[0], "self should still work");
is(frames[0].top, window.top, "Our subframe's top should be our top");
is(doGet("top", frames[0]), window.top,
"Our subframe's top should still be our top");
is(frames[0].window, frames[0], "window getter should work");
// "window" is not a getter property yet
//is(doGet("window", frames[0]), frames[0], "window getter should still work");
todo_isnot(Object.getOwnPropertyDescriptor(window, "window").get, undefined,
"Should have a getter here");
// Finally, check that we can set the location
frames[0].location = "about:blank";
doSet("location", frames[1], "about:blank");
SimpleTest.finish();
}
</script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=946067">Mozilla Bug 946067</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<iframe id="x" src="http://www.example.com/nosuchpageIhope"></iframe>
<iframe id="y" src="http://www.example.com/nosuchpageIhope"></iframe>
</pre>
</body>
</html>

View File

@ -1523,7 +1523,8 @@ class MethodDefiner(PropertyDefiner):
"methodInfo": not m.isStatic(),
"length": methodLength(m),
"flags": "JSPROP_ENUMERATE",
"condition": PropertyDefiner.getControllingCondition(m) }
"condition": PropertyDefiner.getControllingCondition(m),
"allowCrossOriginThis": m.getExtendedAttribute("CrossOriginCallable")}
if isChromeOnly(m):
self.chrome.append(method)
else:
@ -1598,7 +1599,10 @@ class MethodDefiner(PropertyDefiner):
accessor = m.get("nativeName", m["name"])
if m.get("methodInfo", True):
jitinfo = ("&%s_methodinfo" % accessor)
accessor = "genericMethod"
if m.get("allowCrossOriginThis", False):
accessor = "genericCrossOriginMethod"
else:
accessor = "genericMethod"
else:
jitinfo = "nullptr"
@ -1651,8 +1655,12 @@ class AttrDefiner(PropertyDefiner):
accessor = 'get_' + attr.identifier.name
jitinfo = "nullptr"
else:
accessor = ("genericLenientGetter" if attr.hasLenientThis()
else "genericGetter")
if attr.hasLenientThis():
accessor = "genericLenientGetter"
elif attr.getExtendedAttribute("CrossOriginReadable"):
accessor = "genericCrossOriginGetter"
else:
accessor = "genericGetter"
jitinfo = "&%s_getterinfo" % attr.identifier.name
return "{ JS_CAST_NATIVE_TO(%s, JSPropertyOp), %s }" % \
(accessor, jitinfo)
@ -1666,8 +1674,12 @@ class AttrDefiner(PropertyDefiner):
accessor = 'set_' + attr.identifier.name
jitinfo = "nullptr"
else:
accessor = ("genericLenientSetter" if attr.hasLenientThis()
else "genericSetter")
if attr.hasLenientThis():
accessor = "genericLenientSetter"
elif attr.getExtendedAttribute("CrossOriginWritable"):
accessor = "genericCrossOriginSetter"
else:
accessor = "genericSetter"
jitinfo = "&%s_setterinfo" % attr.identifier.name
return "{ JS_CAST_NATIVE_TO(%s, JSStrictPropertyOp), %s }" % \
(accessor, jitinfo)
@ -5675,10 +5687,14 @@ class CGAbstractBindingMethod(CGAbstractStaticMethod):
callArgs should be code for getting a JS::CallArgs into a variable
called 'args'. This can be "" if there is already such a variable
around.
If allowCrossOriginThis is true, then this-unwrapping will first do an
UncheckedUnwrap and after that operate on the result.
"""
def __init__(self, descriptor, name, args, unwrapFailureCode=None,
getThisObj=None,
callArgs="JS::CallArgs args = JS::CallArgsFromVp(argc, vp);"):
callArgs="JS::CallArgs args = JS::CallArgsFromVp(argc, vp);",
allowCrossOriginThis=False):
CGAbstractStaticMethod.__init__(self, descriptor, name, "bool", args)
if unwrapFailureCode is None:
@ -5707,6 +5723,7 @@ class CGAbstractBindingMethod(CGAbstractStaticMethod):
getThisObj)],
"\n")
self.callArgs = callArgs
self.allowCrossOriginThis = allowCrossOriginThis
def definition_body(self):
# Our descriptor might claim that we're not castable, simply because
@ -5718,10 +5735,19 @@ class CGAbstractBindingMethod(CGAbstractStaticMethod):
self.getThisObj,
CGGeneric("%s* self;" % self.descriptor.nativeType)
], "\n")
objName = "uncheckedObj" if self.allowCrossOriginThis else "obj"
unwrapThis = CGGeneric(
str(CastableObjectUnwrapper(
self.descriptor,
"obj", "self", self.unwrapFailureCode)))
objName, "self", self.unwrapFailureCode)))
if self.allowCrossOriginThis:
unwrapThis = CGWrapper(
CGIndenter(unwrapThis),
pre=("{ // Scope for the uncheckedObj JSAutoCompartment\n"
" JS::Rooted<JSObject*> uncheckedObj(cx, js::UncheckedUnwrap(obj));\n"
" JSAutoCompartment ac(cx, uncheckedObj);\n"),
post="\n}")
return CGList([ CGIndenter(getThis), CGIndenter(unwrapThis),
self.generate_code() ], "\n").define()
@ -5758,17 +5784,22 @@ def MakeNativeName(name):
class CGGenericMethod(CGAbstractBindingMethod):
"""
A class for generating the C++ code for an IDL method..
A class for generating the C++ code for an IDL method.
If allowCrossOriginThis is true, then this-unwrapping will first do an
UncheckedUnwrap and after that operate on the result.
"""
def __init__(self, descriptor):
def __init__(self, descriptor, allowCrossOriginThis=False):
args = [Argument('JSContext*', 'cx'), Argument('unsigned', 'argc'),
Argument('JS::Value*', 'vp')]
unwrapFailureCode = (
'return ThrowInvalidThis(cx, args, GetInvalidThisErrorForMethod(%%(securityError)s), "%s");' %
descriptor.interface.identifier.name)
CGAbstractBindingMethod.__init__(self, descriptor, 'genericMethod',
name = "genericCrossOriginMethod" if allowCrossOriginThis else "genericMethod"
CGAbstractBindingMethod.__init__(self, descriptor, name,
args,
unwrapFailureCode=unwrapFailureCode)
unwrapFailureCode=unwrapFailureCode,
allowCrossOriginThis=allowCrossOriginThis)
def generate_code(self):
return CGIndenter(CGGeneric(
@ -5951,7 +5982,7 @@ class CGGenericGetter(CGAbstractBindingMethod):
"""
A class for generating the C++ code for an IDL attribute getter.
"""
def __init__(self, descriptor, lenientThis=False):
def __init__(self, descriptor, lenientThis=False, allowCrossOriginThis=False):
args = [Argument('JSContext*', 'cx'), Argument('unsigned', 'argc'),
Argument('JS::Value*', 'vp')]
if lenientThis:
@ -5964,12 +5995,16 @@ class CGGenericGetter(CGAbstractBindingMethod):
"args.rval().set(JS::UndefinedValue());\n"
"return true;")
else:
name = "genericGetter"
if allowCrossOriginThis:
name = "genericCrossOriginGetter"
else:
name = "genericGetter"
unwrapFailureCode = (
'return ThrowInvalidThis(cx, args, GetInvalidThisErrorForGetter(%%(securityError)s), "%s");' %
descriptor.interface.identifier.name)
CGAbstractBindingMethod.__init__(self, descriptor, name, args,
unwrapFailureCode)
unwrapFailureCode,
allowCrossOriginThis=allowCrossOriginThis)
def generate_code(self):
return CGIndenter(CGGeneric(
@ -6059,7 +6094,7 @@ class CGGenericSetter(CGAbstractBindingMethod):
"""
A class for generating the C++ code for an IDL attribute setter.
"""
def __init__(self, descriptor, lenientThis=False):
def __init__(self, descriptor, lenientThis=False, allowCrossOriginThis=False):
args = [Argument('JSContext*', 'cx'), Argument('unsigned', 'argc'),
Argument('JS::Value*', 'vp')]
if lenientThis:
@ -6072,13 +6107,17 @@ class CGGenericSetter(CGAbstractBindingMethod):
"args.rval().set(JS::UndefinedValue());\n"
"return true;")
else:
name = "genericSetter"
if allowCrossOriginThis:
name = "genericCrossOriginSetter"
else:
name = "genericSetter"
unwrapFailureCode = (
'return ThrowInvalidThis(cx, args, GetInvalidThisErrorForSetter(%%(securityError)s), "%s");' %
descriptor.interface.identifier.name)
CGAbstractBindingMethod.__init__(self, descriptor, name, args,
unwrapFailureCode)
unwrapFailureCode,
allowCrossOriginThis=allowCrossOriginThis)
def generate_code(self):
return CGIndenter(CGGeneric(
@ -8490,7 +8529,8 @@ class CGDescriptor(CGThing):
cgThings.append(CGMemberJITInfo(descriptor, m))
if m.getExtendedAttribute("CrossOriginCallable"):
crossOriginMethods.add(m.identifier.name)
hasMethod = True
else:
hasMethod = True
elif m.isAttr():
if m.isStatic():
assert descriptor.interface.hasInterfaceObject
@ -8499,9 +8539,9 @@ class CGDescriptor(CGThing):
cgThings.append(CGSpecializedGetter(descriptor, m))
if m.hasLenientThis():
hasLenientGetter = True
elif m.getExtendedAttribute("CrossOriginReadable"):
crossOriginGetters.add(m.identifier.name)
else:
if m.getExtendedAttribute("CrossOriginReadable"):
crossOriginGetters.add(m.identifier.name)
hasGetter = True
if not m.readonly:
if m.isStatic():
@ -8511,15 +8551,16 @@ class CGDescriptor(CGThing):
cgThings.append(CGSpecializedSetter(descriptor, m))
if m.hasLenientThis():
hasLenientSetter = True
elif m.getExtendedAttribute("CrossOriginWritable"):
crossOriginSetters.add(m.identifier.name)
else:
if m.getExtendedAttribute("CrossOriginWritable"):
crossOriginSetters.add(m.identifier.name)
hasSetter = True
elif m.getExtendedAttribute("PutForwards"):
cgThings.append(CGSpecializedForwardingSetter(descriptor, m))
if m.getExtendedAttribute("CrossOriginWritable"):
crossOriginSetters.add(m.identifier.name)
hasSetter = True
else:
hasSetter = True
elif m.getExtendedAttribute("Replaceable"):
cgThings.append(CGSpecializedReplaceableSetter(descriptor, m))
hasSetter = True
@ -8530,12 +8571,21 @@ class CGDescriptor(CGThing):
cgThings.append(CGJsonifierMethod(descriptor, jsonifierMethod))
cgThings.append(CGMemberJITInfo(descriptor, jsonifierMethod))
if hasMethod: cgThings.append(CGGenericMethod(descriptor))
if len(crossOriginMethods):
cgThings.append(CGGenericMethod(descriptor,
allowCrossOriginThis=True))
if hasGetter: cgThings.append(CGGenericGetter(descriptor))
if hasLenientGetter: cgThings.append(CGGenericGetter(descriptor,
lenientThis=True))
if len(crossOriginGetters):
cgThings.append(CGGenericGetter(descriptor,
allowCrossOriginThis=True))
if hasSetter: cgThings.append(CGGenericSetter(descriptor))
if hasLenientSetter: cgThings.append(CGGenericSetter(descriptor,
lenientThis=True))
if len(crossOriginSetters):
cgThings.append(CGGenericSetter(descriptor,
allowCrossOriginThis=True))
if descriptor.interface.getNavigatorProperty():
cgThings.append(CGConstructNavigatorObjectHelper(descriptor))