Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | const { sandbox, load, evaluate, nuke } = require('sdk/loader/sandbox'); |
michael@0 | 6 | const xulApp = require("sdk/system/xul-app"); |
michael@0 | 7 | const fixturesURI = module.uri.split('test-sandbox.js')[0] + 'fixtures/'; |
michael@0 | 8 | |
michael@0 | 9 | // The following adds Debugger constructor to the global namespace. |
michael@0 | 10 | const { Cu } = require('chrome'); |
michael@0 | 11 | const { addDebuggerToGlobal } = |
michael@0 | 12 | Cu.import('resource://gre/modules/jsdebugger.jsm', {}); |
michael@0 | 13 | addDebuggerToGlobal(this); |
michael@0 | 14 | |
michael@0 | 15 | exports['test basics'] = function(assert) { |
michael@0 | 16 | let fixture = sandbox('http://example.com'); |
michael@0 | 17 | assert.equal(evaluate(fixture, 'var a = 1;'), undefined, |
michael@0 | 18 | 'returns expression value'); |
michael@0 | 19 | assert.equal(evaluate(fixture, 'b = 2;'), 2, |
michael@0 | 20 | 'returns expression value'); |
michael@0 | 21 | assert.equal(fixture.b, 2, 'global is defined as property'); |
michael@0 | 22 | assert.equal(fixture.a, 1, 'global is defined as property'); |
michael@0 | 23 | assert.equal(evaluate(fixture, 'a + b;'), 3, 'returns correct sum'); |
michael@0 | 24 | }; |
michael@0 | 25 | |
michael@0 | 26 | exports['test non-privileged'] = function(assert) { |
michael@0 | 27 | let fixture = sandbox('http://example.com'); |
michael@0 | 28 | if (xulApp.versionInRange(xulApp.platformVersion, "15.0a1", "18.*")) { |
michael@0 | 29 | let rv = evaluate(fixture, 'Compo' + 'nents.utils'); |
michael@0 | 30 | assert.equal(rv, undefined, |
michael@0 | 31 | "Components's attributes are undefined in content sandboxes"); |
michael@0 | 32 | } |
michael@0 | 33 | else { |
michael@0 | 34 | assert.throws(function() { |
michael@0 | 35 | evaluate(fixture, 'Compo' + 'nents.utils'); |
michael@0 | 36 | }, 'Access to components is restricted'); |
michael@0 | 37 | } |
michael@0 | 38 | fixture.sandbox = sandbox; |
michael@0 | 39 | assert.throws(function() { |
michael@0 | 40 | evaluate(fixture, sandbox('http://foo.com')); |
michael@0 | 41 | }, 'Can not call privileged code'); |
michael@0 | 42 | }; |
michael@0 | 43 | |
michael@0 | 44 | exports['test injection'] = function(assert) { |
michael@0 | 45 | let fixture = sandbox(); |
michael@0 | 46 | fixture.hi = function(name) 'Hi ' + name |
michael@0 | 47 | assert.equal(evaluate(fixture, 'hi("sandbox");'), 'Hi sandbox', |
michael@0 | 48 | 'injected functions are callable'); |
michael@0 | 49 | }; |
michael@0 | 50 | |
michael@0 | 51 | exports['test exceptions'] = function(assert) { |
michael@0 | 52 | let fixture = sandbox(); |
michael@0 | 53 | try { |
michael@0 | 54 | evaluate(fixture, '!' + function() { |
michael@0 | 55 | var message = 'boom'; |
michael@0 | 56 | throw Error(message); |
michael@0 | 57 | } + '();'); |
michael@0 | 58 | } |
michael@0 | 59 | catch (error) { |
michael@0 | 60 | assert.equal(error.fileName, '', 'no fileName reported'); |
michael@0 | 61 | assert.equal(error.lineNumber, 3, 'reports correct line number'); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | try { |
michael@0 | 65 | evaluate(fixture, '!' + function() { |
michael@0 | 66 | var message = 'boom'; |
michael@0 | 67 | throw Error(message); |
michael@0 | 68 | } + '();', 'foo.js'); |
michael@0 | 69 | } |
michael@0 | 70 | catch (error) { |
michael@0 | 71 | assert.equal(error.fileName, 'foo.js', 'correct fileName reported'); |
michael@0 | 72 | assert.equal(error.lineNumber, 3, 'reports correct line number'); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | try { |
michael@0 | 76 | evaluate(fixture, '!' + function() { |
michael@0 | 77 | var message = 'boom'; |
michael@0 | 78 | throw Error(message); |
michael@0 | 79 | } + '();', 'foo.js', 2); |
michael@0 | 80 | } |
michael@0 | 81 | catch (error) { |
michael@0 | 82 | assert.equal(error.fileName, 'foo.js', 'correct fileName reported'); |
michael@0 | 83 | assert.equal(error.lineNumber, 4, 'line number was opted'); |
michael@0 | 84 | } |
michael@0 | 85 | }; |
michael@0 | 86 | |
michael@0 | 87 | exports['test opt version'] = function(assert) { |
michael@0 | 88 | let fixture = sandbox(); |
michael@0 | 89 | assert.throws(function() { |
michael@0 | 90 | evaluate(fixture, 'let a = 2;', 'test.js', 1, '1.5'); |
michael@0 | 91 | }, 'No let in js 1.5'); |
michael@0 | 92 | }; |
michael@0 | 93 | |
michael@0 | 94 | exports['test load'] = function(assert) { |
michael@0 | 95 | let fixture = sandbox(); |
michael@0 | 96 | load(fixture, fixturesURI + 'sandbox-normal.js'); |
michael@0 | 97 | assert.equal(fixture.a, 1, 'global variable defined'); |
michael@0 | 98 | assert.equal(fixture.b, 2, 'global via `this` property was set'); |
michael@0 | 99 | assert.equal(fixture.f(), 4, 'function was defined'); |
michael@0 | 100 | }; |
michael@0 | 101 | |
michael@0 | 102 | exports['test load with data: URL'] = function(assert) { |
michael@0 | 103 | let code = "var a = 1; this.b = 2; function f() 4"; |
michael@0 | 104 | let fixture = sandbox(); |
michael@0 | 105 | load(fixture, "data:," + encodeURIComponent(code)); |
michael@0 | 106 | |
michael@0 | 107 | assert.equal(fixture.a, 1, 'global variable defined'); |
michael@0 | 108 | assert.equal(fixture.b, 2, 'global via `this` property was set'); |
michael@0 | 109 | assert.equal(fixture.f(), 4, 'function was defined'); |
michael@0 | 110 | }; |
michael@0 | 111 | |
michael@0 | 112 | exports['test load script with complex char'] = function(assert) { |
michael@0 | 113 | let fixture = sandbox(); |
michael@0 | 114 | load(fixture, fixturesURI + 'sandbox-complex-character.js'); |
michael@0 | 115 | assert.equal(fixture.chars, 'გამარჯობა', 'complex chars were loaded correctly'); |
michael@0 | 116 | }; |
michael@0 | 117 | |
michael@0 | 118 | exports['test load script with data: URL and complex char'] = function(assert) { |
michael@0 | 119 | let code = "var chars = 'გამარჯობა';"; |
michael@0 | 120 | let fixture = sandbox(); |
michael@0 | 121 | load(fixture, "data:," + encodeURIComponent(code)); |
michael@0 | 122 | |
michael@0 | 123 | assert.equal(fixture.chars, 'გამარჯობა', 'complex chars were loaded correctly'); |
michael@0 | 124 | }; |
michael@0 | 125 | |
michael@0 | 126 | exports['test metadata'] = function(assert) { |
michael@0 | 127 | let dbg = new Debugger(); |
michael@0 | 128 | dbg.onNewGlobalObject = function(global) { |
michael@0 | 129 | let metadata = Cu.getSandboxMetadata(global.unsafeDereference()); |
michael@0 | 130 | assert.ok(metadata, 'this global has attached metadata'); |
michael@0 | 131 | assert.equal(metadata.addonID, self.id, 'addon ID is set'); |
michael@0 | 132 | |
michael@0 | 133 | dbg.onNewGlobalObject = undefined; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | let fixture = sandbox(); |
michael@0 | 137 | let self = require('sdk/self'); |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | exports['test nuke sandbox'] = function(assert) { |
michael@0 | 141 | |
michael@0 | 142 | let fixture = sandbox('http://example.com'); |
michael@0 | 143 | fixture.foo = 'foo'; |
michael@0 | 144 | |
michael@0 | 145 | let ref = evaluate(fixture, 'let a = {bar: "bar"}; a'); |
michael@0 | 146 | |
michael@0 | 147 | nuke(fixture); |
michael@0 | 148 | |
michael@0 | 149 | assert.ok(Cu.isDeadWrapper(fixture), 'sandbox should be dead'); |
michael@0 | 150 | |
michael@0 | 151 | assert.throws( |
michael@0 | 152 | () => fixture.foo, |
michael@0 | 153 | /can't access dead object/, |
michael@0 | 154 | 'property of nuked sandbox should not be accessible' |
michael@0 | 155 | ); |
michael@0 | 156 | |
michael@0 | 157 | assert.ok(Cu.isDeadWrapper(ref), 'ref to object from sandbox should be dead'); |
michael@0 | 158 | |
michael@0 | 159 | assert.throws( |
michael@0 | 160 | () => ref.bar, |
michael@0 | 161 | /can't access dead object/, |
michael@0 | 162 | 'object from nuked sandbox should not be alive' |
michael@0 | 163 | ); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | require('test').run(exports); |