Lodash.merge() is a JavaScript utility function that recursively combines properties from source objects into a destination object. It is used for deep merging of nested data structures, and its handling of arrays and inherited or unexpected properties can affect application state and object integrity.
How Lodash.merge() works
Lodash.merge() performs a deep, recursive merge rather than a shallow property copy. That means nested objects are combined level by level, with later sources influencing the destination object’s final shape. For developers, the important point is that merge behaviour is not just about convenience, it can also change how application state, defaults, and nested configuration objects are interpreted.
Because the function walks object structure recursively, the result depends on how it treats arrays, plain objects, and inherited properties. In practice, that makes Lodash.merge() useful for configuration composition, but it also means the merge rule itself becomes part of the application’s security and integrity boundary.
Why deep merging can affect application integrity
Deep merge logic can be safe in one context and dangerous in another. If a caller assumes the destination object only receives explicitly intended fields, recursive merging can reintroduce keys from unexpected sources, preserve stale nested values, or overwrite security-sensitive settings in ways that are hard to notice during review.
In security-sensitive code, object merge semantics matter because objects often carry authorization decisions, feature flags, request metadata, policy settings, or runtime configuration. A merge helper is therefore not just a utility, it can become a control point for whether untrusted input is allowed to reshape trusted state.
When you need a broader control lens for object handling and trust boundaries, the same concerns are often discussed in defensive configuration and hardening guidance such as NIST Cybersecurity Framework 2.0 and CIS Benchmarks, which both emphasise reducing unsafe defaults and limiting configuration drift.
Common failure modes to watch for
The main technical concern with deep merge utilities is not the function name, but the data model they operate on. If unexpected keys, inherited properties, or attacker-influenced nested objects reach the merge path, the destination object may gain properties that the rest of the application trusts without revalidation.
Arrays can be especially tricky because developers often expect replacement behaviour while the utility may combine or recurse in ways that do not match the intended business rule. That mismatch can create subtle logic bugs, inconsistent policy enforcement, or state corruption that only appears after several merge operations.
These failure modes are easiest to miss when the merge happens far from the point where the object is later consumed. A harmless-looking helper call can therefore become the place where unsafe object structure enters the application.
Safer usage patterns and review considerations
The safest way to use a deep merge function is to treat it as an input-handling decision, not a formatting convenience. Reviewers should ask which object keys are allowed, which sources are trusted, and whether the destination object contains any fields whose integrity must never be altered by external data.
In practice, teams should prefer explicit assignment or schema-based validation when the object carries security meaning, and reserve deep merge for cases where the merge rules are fully understood and deliberately tested. If a merge is required, the surrounding code should make the trust boundary obvious so future maintainers do not assume the helper is automatically safe.
For code paths that aggregate multiple data sources, the relevant defensive question is whether the application is intentionally composing state or accidentally inheriting it. That distinction is often what separates a useful utility from a hidden integrity risk.
Risk and Threat Considerations
Deep merge utilities can expose applications to object injection, prototype pollution style issues, and integrity failures when untrusted properties are merged into trusted state. The risk is highest when the merged object later drives access control, configuration, templating, or server-side behaviour.
Failure mechanism: Unexpected keys or inherited properties are merged into a destination object, then later code reads those properties as if they were legitimate application state.
Impact: Attackers may influence runtime behaviour, weaken policy enforcement, or corrupt configuration in ways that are difficult to detect until after the merge result is consumed.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
NIST CSF 2.0, CIS Controls v8 and OWASP ASVS set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| NIST CSF 2.0 | PR.DS-10 — Integrity Mechanisms | Deep merge logic can alter object integrity and trusted state. |
| GV.RM-01 — Risk Management Strategy | Merge utilities create application integrity risk that needs explicit governance. | |
| Recommendation — Apply PR.DS-10 to preserve integrity checks around merged configuration and state objects. Define a risk strategy for trusted versus untrusted object composition paths. | ||
| CIS Controls v8 | CIS-5 — Account Management | Merged objects can influence privilege and access-related settings. |
| Recommendation — Restrict merge-driven state changes from altering account or access control data. | ||
| OWASP ASVS | V15 — Secure Coding and Architecture | Safe object composition is a secure-coding concern for web applications. |
| V2 — Validation and Business Logic | Unexpected merged properties can bypass intended object validation and business rules. | |
| Recommendation — Review deep merge usage as part of secure architecture and implementation review. Validate object structure before merge and enforce business rules after composition. | ||
Practitioner Guidance
What to watch for: Review any code path where Lodash.merge() processes partially trusted input, especially when the target object carries permissions, security settings, or framework configuration. A merge helper should be treated as a trust-boundary operation, not as a neutral convenience function.
Governance implication: Teams should document when deep merge is allowed, when explicit property assignment is required, and which object shapes are prohibited before merge. That keeps implementation choices aligned with the data integrity expectations of the system.
Related resources from NHI Mgmt Group
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on November 3, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org