ts-utils

Comprehensive TypeScript/JavaScript utility library with cross-environment support (Node.js, browser, web worker) providing helper functions, polyfills (ES5-ES2023), type checking utilities, and optimized implementations for better minification and code readability.

View on GitHub

Security Helpers

When copying, merging, or transforming untrusted object-like input, JavaScript applications can accidentally allow prototype pollution via dangerous keys such as __proto__, constructor, and prototype.

This package includes helpers to reduce that risk in common object traversal and assignment flows.

Available Helpers

Use isUnsafeTarget() to guard the destination object, and forEachOwnKeySafe() to iterate source keys.

import {
  forEachOwnKeySafe,
  isUnsafeTarget
} from "@nevware21/ts-utils";

function copyTrustedValues(source: any, target: any) {
  if (isUnsafeTarget(target)) {
    throw new TypeError("Refusing to write to a built-in prototype");
  }

  forEachOwnKeySafe(source, (key, value) => {
    target[key] = value;
  });

  return target;
}

const payload = {
  safe: true,
  __proto__: {
    polluted: true
  }
};

copyTrustedValues(payload, {});
// Result only includes the safe enumerable keys.

Notes