forked from orbit-oss/flask
Make sure the attachment filename is text type.
If attachment filename is bytes type and contains non-ascii coded bytes, then the following ASCII encoding process will trigger UnicodeDecodeError exception. Fix issue #2933.
This commit is contained in:
parent
232e5c81bc
commit
40118e785f
3 changed files with 10 additions and 1 deletions
|
|
@ -13,13 +13,17 @@ Unreleased
|
||||||
(ISO-8859-1). This fixes compatibility with Gunicorn, which is
|
(ISO-8859-1). This fixes compatibility with Gunicorn, which is
|
||||||
stricter about header encodings than PEP 3333. (`#2766`_)
|
stricter about header encodings than PEP 3333. (`#2766`_)
|
||||||
- Allow custom CLIs using ``FlaskGroup`` to set the debug flag without
|
- Allow custom CLIs using ``FlaskGroup`` to set the debug flag without
|
||||||
it always being overwritten based on environment variables. (`#2765`_)
|
it always being overwritten based on environment variables.
|
||||||
|
(`#2765`_)
|
||||||
- ``flask --version`` outputs Werkzeug's version and simplifies the
|
- ``flask --version`` outputs Werkzeug's version and simplifies the
|
||||||
Python version. (`#2825`_)
|
Python version. (`#2825`_)
|
||||||
|
- :func:`send_file` handles an ``attachment_filename`` that is a
|
||||||
|
native Python 2 string (bytes) with UTF-8 coded bytes. (`#2933`_)
|
||||||
|
|
||||||
.. _#2766: https://github.com/pallets/flask/issues/2766
|
.. _#2766: https://github.com/pallets/flask/issues/2766
|
||||||
.. _#2765: https://github.com/pallets/flask/pull/2765
|
.. _#2765: https://github.com/pallets/flask/pull/2765
|
||||||
.. _#2825: https://github.com/pallets/flask/pull/2825
|
.. _#2825: https://github.com/pallets/flask/pull/2825
|
||||||
|
.. _#2933: https://github.com/pallets/flask/issues/2933
|
||||||
|
|
||||||
|
|
||||||
Version 1.0.2
|
Version 1.0.2
|
||||||
|
|
|
||||||
|
|
@ -567,6 +567,9 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
|
||||||
raise TypeError('filename unavailable, required for '
|
raise TypeError('filename unavailable, required for '
|
||||||
'sending as attachment')
|
'sending as attachment')
|
||||||
|
|
||||||
|
if not isinstance(attachment_filename, text_type):
|
||||||
|
attachment_filename = attachment_filename.decode('utf-8')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
attachment_filename = attachment_filename.encode('ascii')
|
attachment_filename = attachment_filename.encode('ascii')
|
||||||
except UnicodeEncodeError:
|
except UnicodeEncodeError:
|
||||||
|
|
|
||||||
|
|
@ -644,6 +644,8 @@ class TestSendfile(object):
|
||||||
(u'Ñandú/pingüino.txt', '"Nandu/pinguino.txt"',
|
(u'Ñandú/pingüino.txt', '"Nandu/pinguino.txt"',
|
||||||
'%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt'),
|
'%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt'),
|
||||||
(u'Vögel.txt', 'Vogel.txt', 'V%C3%B6gel.txt'),
|
(u'Vögel.txt', 'Vogel.txt', 'V%C3%B6gel.txt'),
|
||||||
|
# Native string not marked as Unicode on Python 2
|
||||||
|
('tést.txt', 'test.txt', 't%C3%A9st.txt'),
|
||||||
))
|
))
|
||||||
def test_attachment_filename_encoding(self, filename, ascii, utf8):
|
def test_attachment_filename_encoding(self, filename, ascii, utf8):
|
||||||
rv = flask.send_file('static/index.html', as_attachment=True, attachment_filename=filename)
|
rv = flask.send_file('static/index.html', as_attachment=True, attachment_filename=filename)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue