# 允许上传 HTML 是否安全？

**URL:** https://meta.discourse.org/t/is-it-safe-to-allow-html-uploads/136797
**Category:** Support
**Created:** [2019年十二月21日 22:43 UTC](https://meta.discourse.org/t/is-it-safe-to-allow-html-uploads/136797 "2019-12-21T22:43:40Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Alex\_P](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex_p/32/163548_2.png) [@Alex\_P](https://meta.discourse.org/u/Alex_P)
#### Post date: [2019年十二月21日 22:43 UTC](https://meta.discourse.org/t/is-it-safe-to-allow-html-uploads/136797/1 "2019-12-21T22:43:40Z")

</div>

据我所见，上传的 HTML 文件只是作为普通文件（例如压缩包或 PDF）被下载，因此是否打开它们取决于用户和浏览器设置（例如下载后自动打开等）。

但此话题提到了与 XSS 相关的内容：

> [@Security checks on uploads](https://meta.discourse.org/t/security-checks-on-uploads/26839/20?u=alex_p):
>
> If I can upload an HTML page I can do what’s called a Cross Site Scripting (XSS) attack, as some browsers will render gifs as HTML. [Neal Poole from Facebook wrote about a Wordpress vuln like this back in 2011](https://nealpoole.com/blog/2011/04/file-upload-xss-vulnerability-in-wordpress/) The HTML file could have javascript in it, which I could use to do bad things. Again, I’m not a security researcher (though that’s what our community is made up of), so I can’t speak at length about XSS or test this thoroughly. I’m just wondering if you guys do anything to check headers o…

据我理解，从下载的文件中无法与 Discourse 页面进行交互？  
因此，最坏的情况不过是显示某种钓鱼内容。

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2019年十二月21日 22:55 UTC](https://meta.discourse.org/t/is-it-safe-to-allow-html-uploads/136797/2 "2019-12-21T22:55:33Z")

</div>

在我看来，这不是个好主意。

---

<div class="post-metadata">

### Author: ![Alex\_P](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex_p/32/163548_2.png) [@Alex\_P](https://meta.discourse.org/u/Alex_P)
#### Post date: [2019年十二月22日 11:26 UTC](https://meta.discourse.org/t/is-it-safe-to-allow-html-uploads/136797/3 "2019-12-22T11:26:41Z")

</div>

能否请您详细说明一下原因？🤔

---

<div class="post-metadata">

### Author: ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)
#### Post date: [2019年十二月22日 11:32 UTC](https://meta.discourse.org/t/is-it-safe-to-allow-html-uploads/136797/4 "2019-12-22T11:32:22Z")

</div>

这和你不希望社区中出现网络钓鱼链接的原因完全一样，对吧？

Discourse 并不是即时通讯工具或文件存储服务，你为什么要用它来专门提供 HTML 文件的下载呢？代码理应托管在 GitHub 这样的地方才更合适，不是吗？

---

<div class="post-metadata">

### Author: ![Alex\_P](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex_p/32/163548_2.png) [@Alex\_P](https://meta.discourse.org/u/Alex_P)
#### Post date: [2019年十二月22日 13:19 UTC](https://meta.discourse.org/t/is-it-safe-to-allow-html-uploads/136797/5 "2019-12-22T13:19:50Z")

</div>

有时候，直接上传包含简单问题等内容的 HTML 文件更为方便；如果只有一个文件，将其放入 ZIP 归档只会带来不必要的麻烦。

该论坛主要面向开发者，因此可以合理假设他们在打开下载的文件时清楚自己在做什么，能够识别网络钓鱼尝试，不会在其中输入任何密码，并能迅速报告此类问题等。  
因此，唯一需要关注的是：上传 HTML 文件（与包含 HTML 文件的 ZIP 归档相比）是否存在被利用漏洞（如 XSS 等）的风险。

---

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [2019年十二月22日 15:08 UTC](https://meta.discourse.org/t/is-it-safe-to-allow-html-uploads/136797/6 "2019-12-22T15:08:33Z")

</div>

> [@Alex\_P](#):
>
> 有时候，直接上传展示某个简单问题的 HTML 文件会更便捷。如果只有一个文件，将其打包成 ZIP 只会徒增麻烦。

> [@Alex\_P](#):
>
> 本论坛主要面向开发者，因此可以合理假设，他们在打开下载的文件时清楚自己在做什么，能够识别网络钓鱼企图，不会在其中输入任何密码，并能迅速举报等。

就个人偏好而言，我希望能完全避免下载任何文件。我更倾向于直接在帖子中查看代码。因此，我的问题是……

是否有任何理由不能将 HTML 直接作为代码块添加到帖子中？

```markdown
<section>
  <h1>引言</h1>
  <p>早在有文字记载的历史之前，人们就开始捕鱼作为食物……</p>
</section>

<section>
  <h1>装备</h1>
  <p>您首先需要一根您觉得舒适且足以应对您预期捕获的鱼类的钓竿或鱼杆……</p>
</section>
...

```

这样一来，内容即可立即显示，无需下载任何文件，且不会占用太多帖子空间——因为我们对超过一定高度的代码块强制启用滚动条。此外，由于 HTML 不会被解析，因此完全不存在安全风险。

如果此方案可行，我可以进一步展开说明。

---

<div class="post-metadata">

### Author: ![Alex\_P](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex_p/32/163548_2.png) [@Alex\_P](https://meta.discourse.org/u/Alex_P)
#### Post date: [2019年十二月22日 15:13 UTC](https://meta.discourse.org/t/is-it-safe-to-allow-html-uploads/136797/7 "2019-12-22T15:13:07Z")

</div>

> [@Johani](#):
>
> 为什么 HTML 没有直接作为代码块添加到帖子中？

是的，我同意通常这样做更好，但有时人们仍然更喜欢附加文件，例如当文件太大时，或者当他们想展示某种视觉问题时（比复制粘贴到新文件等更方便）。

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2023年十月16日 15:04 UTC](https://meta.discourse.org/t/is-it-safe-to-allow-html-uploads/136797/8 "2023-10-16T15:04:35Z")

</div>



---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [2023年十月16日 15:21 UTC](https://meta.discourse.org/t/is-it-safe-to-allow-html-uploads/136797/9 "2023-10-16T15:21:39Z")

</div>


