Fix legacy accessors
ADD
shim/src/repair/accessors.js
+// Adapted from SES/Caja - Copyright (C) 2011 Google Inc.
+// https://github.com/google/caja/blob/master/src/com/google/caja/ses/startSES.js
+// https://github.com/google/caja/blob/master/src/com/google/caja/ses/repairES5.js
+
+import { getPrototypeOf, defineProperty, getOwnPropertyDescriptor } from '../commons';
+
+// Fix legacy accessors to comply with strict mode and ES2016 semantics,
+// we need to redefine them while in strict mode.
+// https://tc39.github.io/ecma262/#sec-object.prototype.__defineGetter__
+
+export default function repairAccessors(objProto) {
+
+ try {
+
+ defineProperty(objProto, '__defineGetter__', {
+ value: function (prop, func) {
+ return defineProperty(this, prop, {
+ get: func,
+ enumerable: true,
+ configurable: true
+ });
+ }
+ });
+
+ defineProperty(objProto, '__defineSetter__', {
+ value: function (prop, func) {
+ return defineProperty(this, prop, {
+ set: func,
+ enumerable: true,
+ configurable: true
+ });
+ }
+ });
+
+ defineProperty(objProto, '__lookupGetter__', {
+ value: function (prop) {
+ let base = this;
+ let desc;
+ while (base && !(desc = getOwnPropertyDescriptor(base, prop))) {
+ base = getPrototypeOf(base);
+ }
+ return desc && desc.get;
+ }
+ });
+
+ defineProperty(objProto, '__lookupSetter__', {
+ value: function (prop) {
+ let base = this;
+ let desc;
+ while (base && !(desc = getOwnPropertyDescriptor(base, prop))) {
+ base = getPrototypeOf(base);
+ }
+ return desc && desc.set;
+ }
+ });
+
+ } catch (ignore) {
+ // Ignored
+ }
+}
\ No newline at end of file

MODIFY
shim/src/sanitize.js
shim/src/sanitize.js
+import repairAccessors from "./repair/accessors";
+
// locking down the environment
export function sanitize(sandbox) {
- const { confinedWindow: { Object: o } } = sandbox;
- try {
- // Fixing properties of Object to comply with strict mode
- // and ES2016 semantics, we do this by redefining them while in 'use strict'
- // https://tc39.github.io/ecma262/#sec-object.prototype.__defineGetter__
- if (o === undefined) {
- return;
- }
- o.defineProperty(o.prototype, '__defineGetter__', {
- value: function (key, fn) {
- return o.defineProperty(this, key, {
- get: fn
- });
- }
- });
- o.defineProperty(o.prototype, '__defineSetter__', {
- value: function (key, fn) {
- return o.defineProperty(this, key, {
- set: fn
- });
- }
- });
- o.defineProperty(o.prototype, '__lookupGetter__', {
- value: function (key) {
- var d, p = this;
- while (p && (d = o.getOwnPropertyDescriptor(p, key)) === undefined) {
- p = o.getPrototypeOf(this);
- }
- return d ? d.get : undefined;
- }
- });
- o.defineProperty(o.prototype, '__lookupSetter__', {
- value: function (key) {
- var d, p = this;
- while (p && (d = o.getOwnPropertyDescriptor(p, key)) === undefined) {
- p = o.getPrototypeOf(this);
- }
- return d ? d.set : undefined;
- }
- });
- // Immutable Prototype Exotic Objects
- // https://github.com/tc39/ecma262/issues/272
- o.seal(o.prototype);
- } catch (ignore) {
- // Ignored
- }
+
+ const { confinedWindow: { Object: { prototype: objProto } } } = sandbox;
+
+ repairAccessors(objProto);
+ // TODO: other steps
}
\ No newline at end of file

Fix legacy accessors