@nevware21/ts-utils
    Preparing search index...

    Function isUnsafeTarget

    • Checks whether the supplied target is a built-in prototype object.

      This helper can be used as an additional guard when assigning properties, to avoid mutating native prototype objects and reduce prototype pollution risk.

      The built-in prototypes include those of the global classes which are defined by the runtime, and the built-in prototypes they inherit from (eg. %TypedArray%.prototype). A class which is added after the first check (eg. by a Promise or Map polyfill) is also identified, when its prototype (whose own constructor is the class) is checked, so a polyfill whose prototype doesn't have its own constructor (eg. one replaced by an object literal) is only identified once the prototype of another class is checked. The missing classes are only looked up again when the class differs from the last class whose prototype caused them to be looked up, so a class prototype which was checked before the polyfill was added is only identified once the prototype of a different class has been checked in between. A target whose own constructor can't be read (eg. a revoked Proxy) is not identified (rather than throwing), and a class whose prototype can't be read is treated as not defined.

      As the prototype of a polyfill's class is identified once the class is the global, the guards which use this (eg. objExtend, objDefaults and setValueByKey) don't change it after that, so a polyfill should use objDefine / objDefineProps to add to its prototype. Adding to it with one of the guards before the global class is assigned makes it the last class looked up, so the global's prototype is then not identified until the prototype of a different class has been checked.

      Parameters

      • target: any

        The target object to validate.

      Returns boolean

      true when the target is a built-in prototype object; otherwise false.

      0.14.0

      isUnsafeTarget(Object.prototype);   // true
      isUnsafeTarget({}); // false