tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

bindings-inject-values-bypass.any.js (2156B)


      1 // META: global=window,worker
      2 // META: title=IndexedDB: ES bindings - Inject a key into a value - Values bypass chain and setters
      3 // META: script=resources/support-promises.js
      4 'use strict';
      5 
      6 
      7 promise_test(async t => {
      8  const db = await createDatabase(t, db => {
      9    db.createObjectStore('store', {autoIncrement: true, keyPath: 'a.b.c'});
     10  });
     11 
     12  Object.prototype.a = {b: {c: 'on proto'}};
     13  t.add_cleanup(() => { delete Object.prototype.a; });
     14 
     15  const tx = db.transaction('store', 'readwrite');
     16  tx.objectStore('store').put({});
     17  const result = await promiseForRequest(t, tx.objectStore('store').get(1));
     18 
     19  assert_true(result.hasOwnProperty('a'),
     20              'Result should have own-properties overriding prototype.');
     21  assert_true(result.a.hasOwnProperty('b'),
     22              'Result should have own-properties overriding prototype.');
     23  assert_true(result.a.b.hasOwnProperty('c'),
     24              'Result should have own-properties overriding prototype.');
     25  assert_equals(result.a.b.c, 1,
     26                'Own property should match primary key generator value');
     27  assert_equals(Object.prototype.a.b.c, 'on proto',
     28                'Prototype should not be modified');
     29 }, 'Returning values to script should bypass prototype chain');
     30 
     31 promise_test(async t => {
     32  const db = await createDatabase(t, db => {
     33    db.createObjectStore('store', {autoIncrement: true, keyPath: 'id'});
     34  });
     35 
     36  let setter_called = false;
     37  Object.defineProperty(Object.prototype, 'id', {
     38    configurable: true,
     39    set: value => { setter_called = true; },
     40  });
     41  t.add_cleanup(() => { delete Object.prototype['id']; });
     42 
     43  const tx = db.transaction('store', 'readwrite');
     44  tx.objectStore('store').put({});
     45  const result = await promiseForRequest(t, tx.objectStore('store').get(1));
     46 
     47  assert_false(setter_called,
     48               'Setter should not be called for key result.');
     49  assert_true(result.hasOwnProperty('id'),
     50              'Result should have own-property overriding prototype setter.');
     51  assert_equals(result.id, 1,
     52                'Own property should match primary key generator value');
     53 }, 'Returning values to script should bypass prototype setters');