Questions
21 of 32
1What is the constructor function?
2What are Objects?
3What are the properties of an object?
4How does javascript implements object oriented programming?
5What are the key differences between javascript’s Object-based Inheritance and Conventional Class-based Inheritance?
6What is a prototype in JavaScript?
7How we can implement prototype-based inheritance?
8Explain the prototype chain and its role in property lookup.
9What is the difference between an object's `__proto__` and a constructor function's `prototype` property and [[Prototype]]?
10What are object wrappers for primitive type?
11Can you explain the use of object wrappers with examples?
12Explain how the `instanceof` operator works.
13What are different methods to create an object?
14What is an object initializer?
15What does the `Object.create()` method do?
16How are constructor functions used to create objects with shared properties and methods?
17Show an example of ES6 classes to create objects and inherit properties?
18What are factory functions in JavaScript ?
19What are Object constructors in JavaScript ?
20How can you add new properties and methods to the prototype of an existing constructor function?
21What is prototype pollution, and how can it be a security risk in JavaScript?
22Is modifying or extending built-in object prototypes recommended (e.g., adding a method to `Array.prototype`)? Why or why not?
23What happens when you try to access a property that doesn't exist on an object?
24How can you prevent or mitigate prototype pollution vulnerabilities in your code?
25How does the `Object.prototype.hasOwnProperty()` method work?
26How can you optimize prototype chain lookup for better performance?
27Can you explain how `Object.prototype.constructor` property works?
28What is the difference between modifying the prototype of an object and adding a property directly to the object?
29How does JavaScript handle circular references in object prototypes?
30What is static dispatching in object-oriented programming?
31What is the purpose of the `Object.keys()` method?
32Write a function flattenObject(obj) that flattens a deeply nested object into a single-level object, using dot notation for nested keys.
21 / 32

What is prototype pollution, and how can it be a security risk in JavaScript?

Prototype pollution is a vulnerability that occurs when an attacker manipulates the prototype of an object to introduce malicious properties or methods. This can lead to unintended behaviour or security breaches in the application.

Why is it a Security Risk?
  1. 1

    Privilege Escalation: As shown above, if an application checks if (user.isAdmin), an attacker can make themselves an admin globally.

  2. 2

    Denial of Service (DoS): An attacker can overwrite built-in methods like toString or valueOf with a value that isn't a function, causing the entire application to crash the next time that method is called.

  3. 3

    Remote Code Execution (RCE): If the polluted property is used as a configuration for a system command (like a file path or a template engine setting), the attacker can execute arbitrary code on the server.

  4. 4

    Bypassing Input Validation: Attackers can inject properties that bypass security filters or sanitizers that rely on checking object properties.

How to prevent it:
  1. 1

    Use Object.create(null): If you are creating an object to store data (like a map or a cache), create it without a prototype. This makes it immune to pollution.

  2. 2

    Validate Keys: Always check if the key being processed is proto, constructor, or prototype and block it.

  3. 3

    Freeze the Prototype: In your main entry file, you can freeze the base prototype to prevent any changes at runtime.

  4. 4

    Use Map instead of Object: For collections of dynamic keys, use the Map data structure. It does not use the prototype chain for its entries, making it safe from this specific attack.

javascript
Difficulty: 6/10
Topics: prototype chain, object property injection, security vulnerabilities

Scenario Questions

0-2 years experience
  1. 1

    If you receive a JSON payload that will be merged into a plain object using Object.assign, what could go wrong if the payload contains a proto property?

  2. 2

    How would you prevent prototype pollution when you need to deep‑merge user‑provided configuration objects?

2-5 years experience
  1. 1

    We have a Node.js microservice that uses lodash's merge to combine request bodies into a settings object. Suddenly some users can read other users' data. Walk me through how prototype pollution could cause this and how you'd debug it.

  2. 2

    When adding a new feature that stores user preferences in a shared in‑memory cache, what trade‑offs would you consider to avoid prototype pollution while keeping performance high?

5-8 years experience
  1. 1

    Design a library that safely merges arbitrary objects from untrusted sources. What patterns would you use to guard against prototype pollution, and how would you test it at scale?

  2. 2

    Our platform runs third‑party plugins that may extend objects. How would you architect a sandbox or isolation layer to ensure prototype pollution cannot affect the core runtime?

8+ years experience
  1. 1

    Across multiple services we have a shared internal SDK that many teams import. How would you lead a migration to eliminate prototype‑pollution risks without breaking existing integrations?

  2. 2

    What long‑term governance processes would you put in place to detect and prevent prototype pollution in a large codebase that includes many open‑source dependencies?

Follow-up Questions

  • Can you show a minimal code snippet that demonstrates the vulnerability?
  • What other JavaScript features could be abused in a similar way?
  • How would you monitor for such attacks in production?