# DOM XSS

If a user can control the executed code or HTML, a malicious user can **send a crafted URL to a victim and control the executed code**.

### Vulnerable code examples

{% hint style="danger" %}

```javascript
eval(document.querySelector('input[name="expression"]').value);
```

{% endhint %}

{% hint style="danger" %}

```javascript
const firstName = document.querySelector('input[name="firstName"]').value;
document.querySelector('.wt-first-name').innerHTML = firstName;
```

{% endhint %}

#### ECMAScript Template String

ECMAScript template strings should not be used for HTML templating.

{% hint style="danger" %}

```javascript
element.innerHTML = `<div>Hi, ${firstName}</div>`;
```

{% endhint %}

Where `firstName` might be controlled by a malicious user.

{% hint style="danger" %}
Except the application's code, every external source should be considered harmful.
{% endhint %}

{% hint style="success" %}
User your frameworks escaping features.
{% endhint %}


---

# 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://web-dev-guide.wishtack.io/security/dom-xss.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.
