我当时正在读“唐氏万维网”(一本我非常喜欢的书),我对以下摘录有疑问:
When Handling User-Controlled Filenames in Content-Disposition Headers
If you do not need non-Latin characters: Strip or substitute any characters except for alpha- numerics, “.”, “-”, and “_”. To protect your users against potentially harmful or deceptive filenames, you may also want to confirm that at least the first character is alphanumeric and substitute all but the rightmost period with something else (e.g., an underscore).
Keep in mind that allowing quotes, semicolons, backslashes, and control characters (0x00–0x1F) will introduce vulnerabilities.
If you need non-Latin names: You must use RFC 2047, RFC 2231, or URL-style percent encoding in a browser-dependent manner. Make sure to filter out control characters (0x00–0x1F) and escape any semicolons, backslashes, and quotes.发布于 2012-10-29 16:04:35
在HTTP报头中允许用户控制输入是非常危险的。我会有一个严格的白名单可能的价值。
而且,转义并不总是用反斜杠来完成。反斜杠有时适用于引用的数据,但始终如此。在这种情况下,HTML实体或URL编码是安全的。
发布于 2012-10-30 10:05:14
如果您需要非拉丁名称:您必须使用RFC 2047、RFC 2231或URL样式百分比编码的浏览器依赖方式。确保筛选出控制字符(0x00-0x1F)并转义任何分号、反斜杠和引号。
甚至不要去那里。隐藏在“浏览器依赖”这个词后面的是一组不一致和不完整的转义规则,这些规则在不同的浏览器中有很大差异。嗅探浏览器类型并为不同情况提供不同的编码是不可行的.即使您这样做了(并且正确地处理了cache/cache以匹配),也有一些字符是不可能为每个浏览器编码的。
哪些字符应该是URL百分比编码的?
在IE,;,"和任何非ASCII上。在任何其他浏览器上,Content-Disposition中的百分比编码都是无效的。
通过“转义分号、反斜杠和引号”,使用反斜杠是否意味着转义?
是的,但是只有Opera才支持这个IIRC。
最终,RFC 6266的规则将解决这个可怕的混乱,但目前还没有得到支持。如果您想要下载文件名中任意的Unicode字符,唯一可靠的方法是将假文件名放在URL路径的后面部分,UTF-8-URL编码(IRI-样式)例如:
http://www.example.com/download.cgi/h%c3%a9llo%20%e6%97%a5%e6%9c%ac.dat下载文件名héllo 日本.dat的结果。
https://security.stackexchange.com/questions/23343
复制相似问题