# Prototype Pollution

#### Prototype Pollution

**Example 1: JavaScript**

**Vulnerable Code:**

```javascript
javascriptCopy codelet user = JSON.parse(req.body.user);
```

**Reason for vulnerability:** If `req.body.user` contains `__proto__`, it can pollute the object prototype.

**Fixed Code:**

```javascript
javascriptCopy codelet user = JSON.parse(req.body.user);
if (user.hasOwnProperty('__proto__')) {
    throw new Error('Prototype pollution attempt detected');
}
```

**Reason for fix:** Check for the presence of `__proto__` and prevent prototype pollution.

**Example 2: JavaScript**

**Vulnerable Code:**

```javascript
javascriptCopy codelet user = Object.assign({}, req.body);
```

**Reason for vulnerability:** If `req.body` contains `__proto__`, it can pollute the object prototype.

**Fixed Code:**

```javascript
javascriptCopy codelet user = Object.assign({}, req.body);
if (user.hasOwnProperty('__proto__')) {
    throw new Error('Prototype pollution attempt detected');
}
```

**Reason for fix:** Check for the presence of `__proto__` and prevent prototype pollution.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://playbook.sidthoviti.com/devsecops/secure-coding/code-review-examples/prototype-pollution.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
