Skip to content

feat(utils): implement defensive flatObject with prototype pollution and array reconstruction defense (ref #743) #13832

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

Key-path flattening and hierarchical un-flattening across search indexing, query parameter serialization, dynamic configuration hydration, and database schema mappings require a defensive data restructuring module (flatObject). Naive implementations suffer from severe security risks: Prototype Pollution (CWE-1321) where untrusted input keys (__proto__.polluted, constructor.prototype.isAdmin) modify Object.prototype and corrupt global application logic, memory exhaustion denial-of-service (CWE-400 / CWE-770) where crafted sparse array indices (arr.999999999=val) force massive contiguous memory allocations that crash the Node.js event loop, destructive smashing of atomic object types (Date, RegExp, Error, TypedArrays), and infinite loop crashes from cyclic data structures. A defensive flatObject utility providing flattenObject(obj, options) and unflattenObject(flatObj, options) with strict prototype shielding, sparse array bounds protection, circular reference handling, and 100% unit test coverage is required.

Scope & Acceptance Criteria

  • Implement flattenObject(obj, options), unflattenObject(flatObj, options), isFlat(obj, delimiter), and safeUnflatten(flatObj, fallback, options) in apps/api/src/utils/flatObject.js.
  • Prototype Pollution Defense (CWE-1321): screen and neutralize all dangerous key segments (__proto__, constructor, prototype) across both flattening and un-flattening pipelines.
  • Sparse Array DOS Defense (CWE-400): enforce maxArrayLength safeguards to convert oversized numeric key paths into objects instead of crashing with out-of-memory heap allocations.
  • Array vs Object Reconstruction: accurately differentiate and reconstruct array collections from numeric key sequences vs structured dictionary objects.
  • Atomic Leaf Preservation: preserve non-plain structures (Date, RegExp, Error, Uint8Array, etc.) as atomic values without lossy property decomposition.
  • Delimiter Flexibility: support custom path delimiters (e.g. . /, __).
  • Circular Reference Shielding: detect and safely mark cyclic references using WeakSet without crashing.
  • Comprehensive unit test suite in apps/api/src/tests/flatObject.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