Skip to content

feat(utils): implement defensive keyBy utility with prototype pollution defense (ref #743) #13803

Open

Description

@whyst0ne

IMPORTANT: Only the issue creator (@whyst0ne) is authorized to submit a PR for this issue. Any uninvited PRs from other accounts will be rejected and closed.

Overview

Entity indexing, dictionary lookup caches, and collection transformation in high-throughput API workflows require a defensive keyBy utility. Naive implementations are vulnerable to Prototype Pollution (CWE-1321) when untrusted collection keys evaluate to __proto__, constructor, or prototype, allowing arbitrary object modification. Additionally, unhandled iteratee exceptions trigger service crashes (CWE-755), sparse array traversals risk prototype leakage or denial-of-service (CWE-400), and non-array inputs cause unhandled type errors (CWE-1287). A defensive keyBy(array, iteratee, options) utility with prototype pollution defense, configurable collision strategies ('overwrite', 'keepFirst', 'ignore', 'error', custom resolver), custom error recovery (onError, suppressErrors), sparse array protection, and 100% unit test coverage is required.

Scope & Acceptance Criteria

  • Implement keyBy(array, iteratee, options) in apps/api/src/utils/keyBy.js.
  • Prototype pollution defense: safely reject or omit __proto__, constructor, and prototype (default 'skip', optional 'throw').
  • Configurable collision resolution: support 'overwrite', 'keepFirst', 'ignore', 'error', and custom resolver callbacks (prev, next, key) => val.
  • Custom iteratee error recovery: support onError hook and suppressErrors option collecting failed items under 'errors'.
  • Sparse array and non-array safety: check own properties to avoid prototype leakage in sparse arrays; safely handle non-arrays, primitives, and iterables.
  • Full unit test suite in apps/api/src/tests/keyBy.test.js validating 100% test pass rate via Node test runner.

Refs #743.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions