From: Lady Date: Fri, 24 Nov 2023 17:03:52 +0000 (-0500) Subject: Make getOwnPropertyDescriptor(s) safer X-Git-Url: https://git.ladys.computer/Pisces/commitdiff_plain/37155822c6ea043e92fe17fbb97086ad1785b89b?ds=inline Make getOwnPropertyDescriptor(s) safer Return proxied property descriptor records from these functions (which inherit from null) to prevent weirdness should somebody try to modify `Object.prototype`. --- diff --git a/object.js b/object.js index aa3538d..9132e4b 100644 --- a/object.js +++ b/object.js @@ -15,7 +15,9 @@ import { toFunctionName, toLength, toPrimitive, + toPropertyDescriptor, type, + UNDEFINED, } from "./value.js"; /** @@ -143,6 +145,27 @@ export const { */ frozenCopy, + /** + * Returns the property descriptor record for the own property with + * the provided property key on the provided object, or null if none + * exists. + * + * ※ This is effectively an alias for + * `Object.getOwnPropertyDescriptor`, but the return value is a + * proxied object with null prototype. + */ + getOwnPropertyDescriptor, + + /** + * Returns the property descriptors for the own properties on the + * provided object. + * + * ※ This is effectively an alias for + * `Object.getOwnPropertyDescriptors`, but the values on the + * resulting object are proxied objects with null prototypes. + */ + getOwnPropertyDescriptors, + /** * Returns whether the provided object is frozen. * @@ -182,6 +205,7 @@ export const { const { create, defineProperties, + getOwnPropertyDescriptor: objectGetOwnPropertyDescriptor, getPrototypeOf, isFrozen, isSealed, @@ -266,6 +290,27 @@ export const { ); } }, + getOwnPropertyDescriptor: (O, P) => { + const desc = objectGetOwnPropertyDescriptor(O, P); + return desc === UNDEFINED + ? UNDEFINED + : toPropertyDescriptor(desc); + }, + getOwnPropertyDescriptors: (O) => { + const obj = toObject(O); + const ownKeys = getOwnPropertyKeys(obj); + const descriptors = {}; + for (let k = 0; k < ownKeys.length; ++k) { + const key = ownKeys[k]; + defineOwnProperty(descriptors, key, { + configurable: true, + enumerable: true, + value: getOwnPropertyDescriptor(O, key), + writable: true, + }); + } + return descriptors; + }, isUnfrozenObject: (O) => !isFrozen(O), isUnsealedObject: (O) => !isSealed(O), setPrototype: (O, proto) => { @@ -413,29 +458,6 @@ export const getMethod = (V, P) => { } }; -/** - * Returns the property descriptor for the own property with the - * provided property key on the provided object, or null if none - * exists. - * - * ※ This is effectively an alias for - * `Object.getOwnPropertyDescriptor`. - */ -export const getOwnPropertyDescriptor = createArrowFunction( - Object.getOwnPropertyDescriptor, -); - -/** - * Returns the property descriptors for the own properties on the - * provided object. - * - * ※ This is effectively an alias for - * `Object.getOwnPropertyDescriptors`. - */ -export const getOwnPropertyDescriptors = createArrowFunction( - Object.getOwnPropertyDescriptors, -); - /** * Returns an array of string‐valued own property keys on the * provided object.