Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 28 | // Check that message and name are not enumerable on Error objects. |
| 29 | var desc = Object.getOwnPropertyDescriptor(Error.prototype, 'name'); |
| 30 | assertFalse(desc['enumerable']); |
| 31 | desc = Object.getOwnPropertyDescriptor(Error.prototype, 'message'); |
| 32 | assertFalse(desc['enumerable']); |
| 33 | |
| 34 | var e = new Error("foobar"); |
| 35 | desc = Object.getOwnPropertyDescriptor(e, 'message'); |
| 36 | assertFalse(desc['enumerable']); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 37 | desc = Object.getOwnPropertyDescriptor(e, 'stack'); |
| 38 | assertFalse(desc['enumerable']); |
| 39 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 40 | var e = new Error(); |
| 41 | assertFalse(e.hasOwnProperty('message')); |
| 42 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 43 | // name is not tested above, but in addition we should have no enumerable |
| 44 | // properties, so we simply assert that. |
| 45 | for (var v in e) { |
| 46 | assertUnreachable(); |
| 47 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | |
| 49 | // Check that error construction does not call setters for the |
| 50 | // properties on error objects in prototypes. |
| 51 | function fail() { assertUnreachable(); }; |
| 52 | ReferenceError.prototype.__defineSetter__('name', fail); |
| 53 | ReferenceError.prototype.__defineSetter__('message', fail); |
| 54 | ReferenceError.prototype.__defineSetter__('stack', fail); |
| 55 | |
| 56 | var e = new ReferenceError(); |
| 57 | assertTrue(e.hasOwnProperty('stack')); |
| 58 | |
| 59 | var e = new ReferenceError('123'); |
| 60 | assertTrue(e.hasOwnProperty('message')); |
| 61 | assertTrue(e.hasOwnProperty('stack')); |
| 62 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 63 | try { |
| 64 | eval("var error = reference"); |
| 65 | } catch (error) { |
| 66 | e = error; |
| 67 | } |
| 68 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 69 | assertTrue(e.hasOwnProperty('stack')); |
| 70 | |
| 71 | // Check that intercepting property access from toString is prevented for |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 72 | // compiler errors. This is not specified, but allowing interception through a |
| 73 | // getter can leak error objects from different script tags in the same context |
| 74 | // in a browser setting. Use Realm.eval as a proxy for loading scripts. We |
| 75 | // ignore the exception thrown from it since that would not be catchable from |
| 76 | // user-land code. |
| 77 | var errors = [SyntaxError, ReferenceError, TypeError, RangeError, URIError]; |
| 78 | var error_triggers = ["syntax error", |
| 79 | "var error = reference", |
| 80 | "undefined()", |
| 81 | "String.fromCodePoint(0xFFFFFF)", |
| 82 | "decodeURI('%F')"]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 83 | for (var i in errors) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 84 | // Monkey-patch prototype. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 85 | for (var prop of ["name", "message", "stack"]) { |
| 86 | errors[i].prototype.__defineGetter__(prop, fail); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 87 | } |
| 88 | // String conversion should not invoke monkey-patched getters on prototype. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 89 | assertThrows(()=>Realm.eval(0, error_triggers[i])); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 92 | // Monkey-patching non-internal errors should still be observable. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 93 | function MyError() {} |
| 94 | MyError.prototype = new Error; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 95 | var errors = [Error, RangeError, EvalError, URIError, |
| 96 | SyntaxError, ReferenceError, TypeError, MyError]; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 97 | for (var i in errors) { |
| 98 | errors[i].prototype.__defineGetter__("name", function() { return "my"; }); |
| 99 | errors[i].prototype.__defineGetter__("message", function() { return "moo"; }); |
| 100 | var e = new errors[i]; |
| 101 | assertEquals("my: moo", e.toString()); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | Error.prototype.toString = Object.prototype.toString; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 106 | assertEquals("[object Object]", Error.prototype.toString()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 107 | assertEquals(Object.prototype, Error.prototype.__proto__); |
| 108 | var e = new Error("foo"); |
| 109 | assertEquals("[object Error]", e.toString()); |