To prevent prototype pollution, you should validate and sanitize any user input before using it to modify prototypes. Additionally, avoid using untrusted data to directly modify object prototypes, and use proper input validation techniques to prevent malicious input from causing unexpected behaviour.
Use Object.create(null) : When creating an object to be used as a map or a data store, initialize it without a prototype. This makes it impossible for an attacker to traverse 'up' the chain to Object.prototype.
Use Map instead of {}: The ES6 Map collection is inherently safer because it doesn't store data as properties on the object itself. It uses a separate internal mechanism, so even if someone pollutes Object.prototype, it won't appear in your Map.get() results.
Always check for 'magic' keys that provide access to the prototype chain: proto, constructor, and prototype.
Object.freeze(): You can freeze the base prototypes at the very start of your application (the entry point). Once frozen, no one—not even your own code—can add or modify properties on them.
Never trust JSON data coming from a user. Use a schema validation library like Zod, Joi, or Ajv. These libraries ensure the object matches a specific shape and strip out any unexpected keys like proto.
Static Analysis (Linting): Use ESLint plugins like eslint-plugin-security. They can flag dangerous patterns like obj[key] = value where key comes from user input.
Dependency Auditing: Regularly run npm audit. Many prototype pollution vulnerabilities are found in popular npm packages (like lodash, extend, or dot-prop). If a vulnerability is found, update to the patched version immediately.
You need to merge user‑provided JSON into a config object using Object.assign. How would you ensure this doesn't introduce prototype pollution?
If a third‑party library you use calls _.merge on an object you control, what check would you add before passing data to avoid polluting Object.prototype?
During a bug‑hunt you notice that setting a property on a request payload unexpectedly adds a new method to all objects. Walk me through how you'd debug and fix the prototype pollution.
Your team wants to use a deep‑merge utility for feature flags. What trade‑offs would you consider to prevent prototype pollution, and how would you test it?
Design a secure input‑validation layer for a microservice that receives arbitrary JSON and must guard against prototype pollution across multiple downstream services. What components would you include?
At scale, you have dozens of npm packages, some of which may be vulnerable to prototype pollution. How would you set up a CI/CD pipeline to detect and mitigate these risks?
Our organization is migrating a legacy monolith that heavily relies on lodash's merge into a new platform. How would you plan a phased migration to eliminate prototype pollution while keeping feature parity?
Across multiple teams you notice inconsistent handling of object merging leading to security gaps. Propose an architecture‑level policy and tooling strategy to enforce safe merging practices organization‑wide.