| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ const { | |||
| 9 | 9 | NumberMAX_SAFE_INTEGER, | |
| 10 | 10 | NumberMIN_SAFE_INTEGER, | |
| 11 | 11 | NumberParseInt, | |
| 12 | + ObjectPrototypeHasOwnProperty, | ||
| 12 | 13 | RegExpPrototypeExec, | |
| 13 | 14 | String, | |
| 14 | 15 | StringPrototypeToUpperCase, | |
@@ -135,6 +136,12 @@ function validateBoolean(value, name) { | |||
| 135 | 136 | throw new ERR_INVALID_ARG_TYPE(name, 'boolean', value); | |
| 136 | 137 | } | |
| 137 | 138 | ||
| 139 | + function getOwnPropertyValueOrDefault(options, key, defaultValue) { | ||
| 140 | + return options == null || !ObjectPrototypeHasOwnProperty(options, key) ? | ||
| 141 | + defaultValue : | ||
| 142 | + options[key]; | ||
| 143 | + } | ||
| 144 | + | ||
| 138 | 145 | /** | |
| 139 | 146 | * @param {unknown} value | |
| 140 | 147 | * @param {string} name | |
@@ -146,10 +153,9 @@ function validateBoolean(value, name) { | |||
| 146 | 153 | */ | |
| 147 | 154 | const validateObject = hideStackFrames( | |
| 148 | 155 | (value, name, options) => { | |
| 149 | - const useDefaultOptions = options == null; | ||
| 150 | - const allowArray = useDefaultOptions ? false : options.allowArray; | ||
| 151 | - const allowFunction = useDefaultOptions ? false : options.allowFunction; | ||
| 152 | - const nullable = useDefaultOptions ? false : options.nullable; | ||
| 156 | + const allowArray = getOwnPropertyValueOrDefault(options, 'allowArray', false); | ||
| 157 | + const allowFunction = getOwnPropertyValueOrDefault(options, 'allowFunction', false); | ||
| 158 | + const nullable = getOwnPropertyValueOrDefault(options, 'nullable', false); | ||
| 153 | 159 | if ((!nullable && value === null) || | |
| 154 | 160 | (!allowArray && ArrayIsArray(value)) || | |
| 155 | 161 | (typeof value !== 'object' && ( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -105,6 +105,10 @@ const invalidArgValueError = { | |||
| 105 | 105 | ||
| 106 | 106 | { | |
| 107 | 107 | // validateObject tests. | |
| 108 | + Object.prototype.nullable = true; | ||
| 109 | + Object.prototype.allowArray = true; | ||
| 110 | + Object.prototype.allowFunction = true; | ||
| 111 | + | ||
| 108 | 112 | validateObject({}, 'foo'); | |
| 109 | 113 | validateObject({ a: 42, b: 'foo' }, 'foo'); | |
| 110 | 114 | ||
@@ -119,6 +123,15 @@ const invalidArgValueError = { | |||
| 119 | 123 | validateObject(null, 'foo', { nullable: true }); | |
| 120 | 124 | validateObject([], 'foo', { allowArray: true }); | |
| 121 | 125 | validateObject(() => {}, 'foo', { allowFunction: true }); | |
| 126 | + | ||
| 127 | + // validateObject should not be affected by Object.prototype tampering. | ||
| 128 | + assert.throws(() => validateObject(null, 'foo', { allowArray: true }), invalidArgTypeError); | ||
| 129 | + assert.throws(() => validateObject([], 'foo', { nullable: true }), invalidArgTypeError); | ||
| 130 | + assert.throws(() => validateObject(() => {}, 'foo', { nullable: true }), invalidArgTypeError); | ||
| 131 | + | ||
| 132 | + delete Object.prototype.nullable; | ||
| 133 | + delete Object.prototype.allowArray; | ||
| 134 | + delete Object.prototype.allowFunction; | ||
| 122 | 135 | } | |
| 123 | 136 | ||
| 124 | 137 | { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments