# 在 Safari 中上传文件名复杂时遇到的问题

**URL:** <https://meta.discourse.org/t/issues-with-upload-files-with-complex-names-in-safari/123707>\
**Category:** Bug\
**Created:** [2019年七月23日 05:13 UTC](https://meta.discourse.org/t/issues-with-upload-files-with-complex-names-in-safari/123707 "2019-07-23T05:13:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![peternlewis](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/peternlewis/32/108888_2.png) [@peternlewis](https://meta.discourse.org/u/peternlewis)\
**Post date:** [2019年七月23日 05:13 UTC](https://meta.discourse.org/t/issues-with-upload-files-with-complex-names-in-safari/123707/1 "2019-07-23T05:13:27Z")

</div>

在 Safari 中，当文件名包含引号（问题尤为严重）或非 ASCII 字符时，上传的文件似乎存在问题。涉及的文件名如下：

```
Create New "Open File" Macro and Copy Macro URL 1.1.kmmacros
Create New “Open File” Macro and Copy Macro URL 1.1.kmmacros

```

第一个文件使用普通双引号，表现非常糟糕；第二个文件使用弯引号，但会被破坏。

参见以下论坛主题：

> **[Testing forum upload with quotes](https://forum.keyboardmaestro.com/t/testing-forum-upload-with-quotes/14504)**
>
> Just ignore this, but it needs to be in a public group so I can reference it from meta.discourse.com Create New "Open File" Macro and Copy Macro URL 1.1.kmmacros (987 Bytes) Create New “Open File” Macro and Copy Macro URL 1.1.kmmacros (989 Bytes)

在 Chrome（Mac）中，两个文件均正常下载为：

```
Create New _Open File_ Macro and Copy Macro URL 1.1.kmmacros
Create New “Open File” Macro and Copy Macro URL 1.1.kmmacros

```

第一个文件中的双引号被替换为下划线，但这可以接受，可能是某处的有意选择。

然而在 Safari 中，文件下载为：

```
Create New .dms
Create New âOpen Fileâ Macro and Copy Macro URL 1.1.kmmacros

```

第一个情况尤其令人担忧，因为文件名（包括扩展名）已被完全破坏。这可能带来安全隐患，因为引号的处理方式尚不清楚。

第二个问题似乎是 UTF 编码转换问题。

我怀疑这是 Discourse（我的服务器版本为 2.3.2）或 Safari 中的回归问题，因为我们此前应该已经遇到过类似情况。不过，我也未能找到任何明确在文件名中包含引号的旧案例以供核查。确实存在一些带有非 ASCII 字符的旧文件（例如 [此处](https://forum.keyboardmaestro.com/t/keyboard-maestro-8-0-3-macro/9002)），但我之前从未发现它们无法正常工作。

更奇怪的是，该问题在 Chrome 中不会出现，而在 Safari 中却存在，因此这很可能是某种客户端代码问题。

---

<div class="post-metadata">

**Author:** ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)\
**Post date:** [2019年七月23日 13:21 UTC](https://meta.discourse.org/t/issues-with-upload-files-with-complex-names-in-safari/123707/2 "2019-07-23T13:21:10Z")

</div>

我可以在本地复现此问题：使用最新版 Firefox 上传和下载文件时会出现该情况。Safari 也会在引号处截断下载文件名。Chrome 处理文件名中的引号的方式可能不同，它会将引号替换为下划线。

---

<div class="post-metadata">

**Author:** ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)\
**Post date:** [2019年七月23日 13:34 UTC](https://meta.discourse.org/t/issues-with-upload-files-with-complex-names-in-safari/123707/3 "2019-07-23T13:34:35Z")

</div>

@gerhard 你能看一下吗？

---

<div class="post-metadata">

**Author:** ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)\
**Post date:** [2019年七月28日 12:51 UTC](https://meta.discourse.org/t/issues-with-upload-files-with-complex-names-in-safari/123707/5 "2019-07-28T12:51:22Z")

</div>

`send_file` 来自 Rails，目前未在 `Content-Disposition` 标头中使用 `filename*` 参数。这就是为什么这些文件名在大多数浏览器中会被损坏的原因。

Rails 6 将修复此问题：[Encode Content-Disposition filenames on send\_data and send\_file · rails/rails@890485c · GitHub](https://github.com/rails/rails/commit/890485cfce4c361c03a41ec23b0ba187007818cc)

我现在是否应该应用一个猴子补丁？而且 S3 存储似乎也应该设置 `filename*`。

> <https://github.com/discourse/discourse/blob/fe7f0982af9577314d87001f35b7ea15e044f8b7/lib/file_store/s3_store.rb#L115-L115>

---

<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年七月28日 21:42 UTC](https://meta.discourse.org/t/issues-with-upload-files-with-complex-names-in-safari/123707/6 "2019-07-28T21:42:44Z")

</div>

可能吧，不确定我们什么时候能升级到 Rails 6？

---

<div class="post-metadata">

**Author:** ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)\
**Post date:** [2019年七月29日 08:29 UTC](https://meta.discourse.org/t/issues-with-upload-files-with-complex-names-in-safari/123707/7 "2019-07-29T08:29:59Z")

</div>

> [@gerhard](#):
>
> 我现在应该应用一个猴子补丁吗？

好的，请这样做。这段代码看起来已经有一年历史了，所以应该相当安全（不过请记得检查是否有修复 bug 的更新）。

---

<div class="post-metadata">

**Author:** ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)\
**Post date:** [2019年八月7日 17:18 UTC](https://meta.discourse.org/t/issues-with-upload-files-with-complex-names-in-safari/123707/8 "2019-08-07T17:18:55Z")

</div>

> [@peternlewis](#):
>
> Discourse（我的服务器版本为 2.3.2）

我刚提交了一个修复。你可以通过升级到或切换到 `tests-passed` 分支来尝试一下。

> <https://github.com/discourse/discourse/commit/24877a7b8c3aa445f0969b298ba8a34ac57c7bd6>
>
> Backport of fix from Rails 6: https://github.com/rails/rails/commit/890485cfce4c…361c03a41ec23b0ba187007818cc

---

<div class="post-metadata">

**Author:** ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)\
**Post date:** [2019年八月7日 17:19 UTC](https://meta.discourse.org/t/issues-with-upload-files-with-complex-names-in-safari/123707/9 "2019-08-07T17:19:05Z")

</div>


