tame regexps: delete RegExp.prototype.compile, and RegExp.$1
MODIFY
src/bundle/createSES.js
src/bundle/createSES.js
@@ -16,6 +16,7 @@ import tameDate from './tame-date.js';
import tameMath from './tame-math.js';
import tameIntl from './tame-intl.js';
import tameError from './tame-error.js';
+import tameRegExp from './tame-regexp.js';
export function createSESWithRealmConstructor(creatorStrings, Realm) {
function makeSESRootRealm(options) {
@@ -41,6 +42,10 @@ export function createSESWithRealmConstructor(creatorStrings, Realm) {
shims.push(`(${tameError})();`);
}
+ if (options.regexpMode !== "allow") {
+ shims.push(`(${tameRegExp})();`);
+ }
+
const r = Realm.makeRootRealm({shims: shims});
const b = r.evaluate(creatorStrings);
b.createSESInThisRealm(r.global, creatorStrings, r);

ADD
src/bundle/tame-regexp.js
+
+export default function tameRegExp() {
+ delete RegExp.prototype.compile;
+ if ('compile' in RegExp.prototype) {
+ throw Error('hey we could not remove RegExp.prototype.compile');
+ }
+
+ // We want to delete RegExp.$1, as well as any other surprising properties.
+ // On some engines we can't just do 'delete RegExp.$1'.
+ const unsafeRegExp = RegExp;
+
+ RegExp = function RegExp(...args) {
+ return Reflect.construct(unsafeRegExp, args, new.target);
+ };
+ RegExp.prototype = unsafeRegExp.prototype;
+ unsafeRegExp.prototype.constructor = RegExp;
+
+ if ('$1' in RegExp) {
+ throw Error('hey we could not remove RegExp.$1');
+ }
+
+}

ADD
test/test-regexp.js
+import test from 'tape';
+import SES from '../src/index.js';
+
+test('RegExp.compile neutered by default', function(t) {
+ const s = SES.makeSESRootRealm();
+ t.equal(s.evaluate('(new RegExp()).compile'), undefined);
+ t.end();
+});
+
+test('RegExp.compile neutered upon request', function(t) {
+ const s = SES.makeSESRootRealm({regexpMode: false});
+ t.equal(s.evaluate('(new RegExp()).compile'), undefined);
+ t.end();
+});
+
+test('RegExp.compile can be left alone', function(t) {
+ const s = SES.makeSESRootRealm({regexpMode: "allow"});
+ t.notEqual(s.evaluate('(new RegExp()).compile'), undefined);
+ t.end();
+});

tame regexps: delete RegExp.prototype.compile, and RegExp.$1