Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should ScriptableObject.sealObject also prevent modifications of parent and prototype #1085

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,11 @@ public static ScriptableObject initSafeStandardObjects(
scope.associateValue(LIBRARY_SCOPE_KEY, scope);
new ClassCache().associate(scope);

BaseFunction.init(cx, scope, sealed);
NativeObject.init(scope, sealed);
BaseFunction.init(cx, scope, sealed);

Scriptable objectProto = ScriptableObject.getObjectPrototype(scope);

// Function.prototype.__proto__ should be Object.prototype
Scriptable functionProto = ScriptableObject.getClassPrototype(scope, "Function");
functionProto.setPrototype(objectProto);

// Set the prototype of the object passed in if need be
if (scope.getPrototype() == null) scope.setPrototype(objectProto);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ public Scriptable getPrototype() {
/** Sets the prototype of the object. */
@Override
public void setPrototype(Scriptable m) {
checkNotSealed("__proto__", 0);
prototypeObject = m;
}

Expand All @@ -722,6 +723,7 @@ public Scriptable getParentScope() {
/** Sets the parent (enclosing) scope of the object. */
@Override
public void setParentScope(Scriptable m) {
checkNotSealed("__parent__", 0);
parentScopeObject = m;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.mozilla.javascript.IdFunctionObject;
import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Wrapper;

@RunWith(BlockJUnit4ClassRunner.class)
Expand All @@ -36,6 +37,19 @@ public class SealedSharedScopeTest {
public void setUp() throws Exception {
try (Context tmpCtx = Context.enter()) {
sharedScope = new ImporterTopLevel(tmpCtx, true);
tmpCtx.evaluateString(
sharedScope,
"jsObj = {'bar': 42};\n"
// Some tests...
// + "Object.defineProperties(jsObj, { baz : { writable: true, value: 'aaa'
// }});\n"
// + "Object.seal(jsObj);"
,
"init",
1,
null);
// Note: Object.seal != ScriptableObject.sealObject
((ScriptableObject) sharedScope.get("jsObj", sharedScope)).sealObject();
sharedScope.sealObject();
}

Expand Down Expand Up @@ -177,4 +191,56 @@ public void importClassSucceedsOnScope() throws Exception {
assertEquals("ReferenceError: \"Locale\" is not defined. (test#1)", e.getMessage());
}
}

@Test
public void testSealedJsModifyProp() {
String s = evaluateString(scope1, "jsObj").toString();
assertEquals("[object Object]", s);

try {
evaluateString(scope1, "'use strict';jsObj.bar = 3");
fail("EvaluatorException expected");
} catch (EvaluatorException e) {
assertEquals(
"Cannot modify a property of a sealed object: bar. (test#1)", e.getMessage());
}
}

@Test
public void testSealedJsAddProp() {
String s = evaluateString(scope1, "jsObj").toString();
assertEquals("[object Object]", s);

try {
evaluateString(scope1, "'use strict';jsObj.foo = 3");
fail("EvaluatorException expected");
} catch (EvaluatorException e) {
assertEquals(
"Cannot modify a property of a sealed object: foo. (test#1)", e.getMessage());
}
}

@Test
public void testSealedJsModifyProto() {
try {
evaluateString(scope1, "jsObj.__proto__ = {}");
fail("EvaluatorException expected");
} catch (EvaluatorException e) {
assertEquals(
"Cannot modify a property of a sealed object: __proto__. (test#1)",
e.getMessage());
}
}

@Test
public void testSealedJsModifyParent() {
try {
evaluateString(scope1, "jsObj.__parent__ = {}");
fail("EvaluatorException expected");
} catch (EvaluatorException e) {
assertEquals(
"Cannot modify a property of a sealed object: __parent__. (test#1)",
e.getMessage());
}
}
}
Loading