[ddc] Seal the native Object prototype in test infra

Update tests to avoid getting or setting `.__proto__`.

Change-Id: I2e80dfc32f162de4f5b3fe5ac74a9e6818a7e55e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/317845
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
This commit is contained in:
Nicholas Shahan 2023-08-10 19:45:59 +00:00 committed by Commit Queue
parent 30869bf7c4
commit 3cff63ff26
6 changed files with 34 additions and 7 deletions

View file

@ -287,7 +287,13 @@ void main(List<String> args) async {
chromeBinary = 'google-chrome';
}
// Seal the native JavaScript Object prototype to avoid pollution before
// loading the Dart SDK module.
var html = '''
<script>
delete Object.prototype.__proto__;
Object.seal(Object.prototype);
</script>
<script src='$requirePath/require.js'></script>
<script>
require.config({
@ -325,6 +331,11 @@ void main(List<String> args) async {
} else if (node) {
var nodePath = '$sdkJsPath:$libRoot';
var runjs = '''
// Seal the native JavaScript Object prototype to avoid pollution before
// loading the Dart SDK module.
delete Object.prototype.__proto__;
Object.seal(Object.prototype);
let source_maps;
try {
source_maps = require('source-map-support');
@ -358,6 +369,11 @@ try {
if (await process.exitCode != 0) exit(await process.exitCode);
} else if (d8) {
var runjs = '''
// Seal the native JavaScript Object prototype to avoid pollution before
// loading the Dart SDK module.
delete Object.prototype.__proto__;
Object.seal(Object.prototype);
load("$ddcPath/lib/js/legacy/dart_library.js");
load("$sdkJsPath/dart_sdk.js");
load("$out");

View file

@ -154,7 +154,8 @@ String ddcHtml(
var ddcGenDir = '/root_build/$genDir';
var packagePaths =
testPackages.map((p) => ' "$p": "$ddcGenDir/pkg/$p",').join("\n");
// Seal the native JavaScript Object prototype to avoid pollution before
// loading the Dart SDK module.
return """
<!DOCTYPE html>
<html>
@ -169,6 +170,10 @@ String ddcHtml(
.unittest-fail { background: #d55;}
.unittest-error { background: #a11;}
</style>
<script>
delete Object.prototype.__proto__;
Object.seal(Object.prototype);
</script>
</head>
<body>
<h1>Running $testName</h1>

View file

@ -258,7 +258,7 @@ window.ExampleJSClass = function ExampleJSClass(x) {
// was shown for this case.
var testClass = new TestClass(17);
var dartConstructor = js_util.getProperty(
js_util.getProperty(testClass, '__proto__'), 'constructor');
js_util.objectGetPrototypeOf(testClass)!, 'constructor');
addFormatterGoldens('Raw reference to dart constructor', dartConstructor);
});

View file

@ -260,7 +260,7 @@ window.ExampleJSClass = function ExampleJSClass(x) {
// was shown for this case.
var testClass = new TestClass(17);
var dartConstructor = js_util.getProperty(
js_util.getProperty(testClass, '__proto__'), 'constructor');
js_util.objectGetPrototypeOf(testClass), 'constructor');
addFormatterGoldens('Raw reference to dart constructor', dartConstructor);
});

View file

@ -12,6 +12,9 @@ import 'package:js/js.dart';
import 'package:js/js_util.dart' as js_util;
import 'package:expect/minitest.dart';
@JS('Object.setPrototypeOf')
external Object objectSetPrototypeOf(Object obj, Object? proto);
@JS()
external String jsFunction();
@ -248,7 +251,7 @@ main() {
expect(js_util.hasProperty(f, 'bar'), isTrue);
expect(js_util.hasProperty(f, 'b'), isFalse);
expect(js_util.hasProperty(f, 'toString'), isTrue);
js_util.setProperty(f, '__proto__', null);
objectSetPrototypeOf(f, null);
expect(js_util.hasProperty(f, 'toString'), isFalse);
});
@ -284,7 +287,7 @@ main() {
expect(js_util.getProperty(f, 'list') is List, isTrue);
expect(js_util.getProperty(f, 'objectProperty') is Object, isTrue);
expect(js_util.getProperty(f, 'toString') is Function, isTrue);
js_util.setProperty(f, '__proto__', null);
objectSetPrototypeOf(f, null);
expect(js_util.getProperty(f, 'toString'), isNull);
});

View file

@ -14,6 +14,9 @@ import 'package:js/js.dart';
import 'package:js/js_util.dart' as js_util;
import 'package:expect/minitest.dart';
@JS('Object.setPrototypeOf')
external Object objectSetPrototypeOf(Object obj, Object proto);
@JS()
external String jsFunction();
@ -250,7 +253,7 @@ main() {
expect(js_util.hasProperty(f, 'bar'), isTrue);
expect(js_util.hasProperty(f, 'b'), isFalse);
expect(js_util.hasProperty(f, 'toString'), isTrue);
js_util.setProperty(f, '__proto__', null);
objectSetPrototypeOf(f, null);
expect(js_util.hasProperty(f, 'toString'), isFalse);
});
@ -286,7 +289,7 @@ main() {
expect(js_util.getProperty(f, 'list') is List, isTrue);
expect(js_util.getProperty(f, 'objectProperty') is Object, isTrue);
expect(js_util.getProperty(f, 'toString') is Function, isTrue);
js_util.setProperty(f, '__proto__', null);
objectSetPrototypeOf(f, null);
expect(js_util.getProperty(f, 'toString'), isNull);
});