extract taming of Math.random to a shim
add tests
MODIFY
src/bundle/createSES.js
src/bundle/createSES.js
// limitations under the License.
import tameDate from './tame-date.js';
+import tameMath from './tame-math.js';
export function createSESWithRealmConstructor(creatorStrings, Realm) {
function makeSESRootRealm(options) {
options = Object(options); // Todo: sanitize
let shims = [];
- const dateNowMode = options.dateNowMode || false; // "allow" or not
// "allow" enables real Date.now(), anything else gets NaN
// (it'd be nice to allow a fixed numeric value, but too hard to
// implement right now)
- if (dateNowMode !== "allow") {
+ if (options.dateNowMode !== "allow") {
shims.push(`(${tameDate})();`);
}
+ if (options.mathRandomMode !== "allow") {
+ shims.push(`(${tameMath})();`);
+ }
+
const r = Realm.makeRootRealm({shims: shims});
const b = r.evaluate(creatorStrings);
b.createSESInThisRealm(r.global, creatorStrings, r);

ADD
src/bundle/tame-math.js
+
+export default function tameMath() {
+ //Math.random = () => 4; // https://www.xkcd.com/221
+ Math.random = () => NaN;
+}

MODIFY
src/bundle/tame.js
src/bundle/tame.js
// See the License for the specific language governing permissions and
// limitations under the License.
-function tameMath(global) {
- //global.Math.random = () => 4; // https://www.xkcd.com/221
- global.Math.random = () => NaN;
-}
-
function tameIntl(global) {
// todo: somehow fix these. These almost certainly don't enable the reading
// of side-channels, but we want things to be deterministic across
@@ -35,7 +30,6 @@ function tameError(global) {
}
export function tamePrimordials(global, options) {
- tameMath(global);
tameIntl(global);
tameError(global);
}

ADD
test/test-math.js
+import test from 'tape';
+import SES from '../src/index.js';
+
+test('Math.random neutered by default', function(t) {
+ const s = SES.makeSESRootRealm();
+ t.ok(Number.isNaN(s.evaluate('Math.random()')));
+ t.end();
+});
+
+test('Math.random neutered upon request', function(t) {
+ const s = SES.makeSESRootRealm({mathRandomMode: false});
+ const random = s.evaluate('Math.random()');
+ t.ok(Number.isNaN(s.evaluate('Math.random()')));
+ t.end();
+});
+
+test('Math.random can be left alone', function(t) {
+ const s = SES.makeSESRootRealm({mathRandomMode: "allow"});
+ const random = s.evaluate('Math.random()');
+ t.equal(typeof random, "number");
+ t.notOk(Number.isNaN(random));
+ t.end();
+});

extract taming of Math.random to a shim

add tests