encode filenames as ascii instead of latin-1
Before adoption: this commit came in with the history the project adopted, so no landing records it.
latin-1 is allowed by pep3333, but that breaks gunicorn
authored by David Lord · committed by David Lord · parent 401d7f9 · Browse files · 3 files·+27 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+27 added−10 removed
- tests:+15 added−8 removed
- other:+12 added−2 removed
Provenance
- Commit
b51ab3f- Tree
5c35e8e- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
401d7f9
3 files changed+27 added−10 removed
modified:flask/helpers.py+6 added-2 removed
504 unmodified lines505 UTF-8 filenames, as specified in `RFC 2231`_, are supported.506 507 .. _RFC 2231: https://tools.ietf.org/html/rfc2231#section-4508 509+ .. versionchanged:: 1.0.3510+ Filenames are encoded with ASCII instead of Latin-1 for broader511+ compatibility with WSGI servers.512+513 :param filename_or_fp: the filename of the file to send.514 This is relative to the :attr:`~Flask.root_path`515 if a relative path is specified.516 Alternatively a file object might be provided in50 unmodified lines567 raise TypeError('filename unavailable, required for '568 'sending as attachment')569 570 try:567- attachment_filename = attachment_filename.encode('latin-1')571+ attachment_filename = attachment_filename.encode('ascii')572 except UnicodeEncodeError:573 filenames = {574 'filename': unicodedata.normalize(571- 'NFKD', attachment_filename).encode('latin-1', 'ignore'),575+ 'NFKD', attachment_filename).encode('ascii', 'ignore'),576 'filename*': "UTF-8''%s" % url_quote(attachment_filename),577 }578 else:579 filenames = {'filename': attachment_filename}469 unmodified linesmodified:tests/test_helpers.py+15 added-8 removed
636 unmodified lines637 assert value == 'attachment'638 assert options['filename'] == 'index.txt'639 rv.close()640 641- def test_attachment_with_utf8_filename(self, app, req_ctx):642- rv = flask.send_file('static/index.html', as_attachment=True, attachment_filename=u'Ñandú/pingüino.txt')643- content_disposition = set(rv.headers['Content-Disposition'].split('; '))644- assert content_disposition == set((645- 'attachment',646- 'filename="Nandu/pinguino.txt"',647- "filename*=UTF-8''%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt"648- ))641+ @pytest.mark.usefixtures('req_ctx')642+ @pytest.mark.parametrize(('filename', 'ascii', 'utf8'), (643+ ('index.html', 'index.html', False),644+ (u'Ñandú/pingüino.txt', '"Nandu/pinguino.txt"',645+ '%C3%91and%C3%BA%EF%BC%8Fping%C3%BCino.txt'),646+ (u'Vögel.txt', 'Vogel.txt', 'V%C3%B6gel.txt'),647+ ))648+ def test_attachment_filename_encoding(self, filename, ascii, utf8):649+ rv = flask.send_file('static/index.html', as_attachment=True, attachment_filename=filename)650 rv.close()651+ content_disposition = rv.headers['Content-Disposition']652+ assert 'filename=%s' % ascii in content_disposition653+ if utf8:654+ assert "filename*=UTF-8''" + utf8 in content_disposition655+ else:656+ assert "filename*=UTF-8''" not in content_disposition657 658 def test_static_file(self, app, req_ctx):659 # default cache timeout is 12 hours660 294 unmodified linesmodified:CHANGES.rst+6 added
7 unmodified lines8 -------------9 10 Unreleased11 12+- :func:`send_file` encodes filenames as ASCII instead of Latin-113+ (ISO-8859-1). This fixes compatibility with Gunicorn, which is14+ stricter about header encodings than PEP 3333. (`#2766`_)15 16+.. _#2766: https://github.com/pallets/flask/issues/276617+18+19 Version 1.0.220 -------------21 22 Released on May 2nd 2018894 unmodified lines