Skip to content
Myr

Merge pull request #2804 from pallets/ascii-header

Before adoption: this commit came in with the history the project adopted, so no landing records it.

encode filenames as ascii instead of latin-1

authored by David Lord · committed by GitHub · parents 401d7f9, b51ab3f · 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

3 files+27 added−10 removed

modified:flask/helpers.py+6 added-2 removed
504 unmodified lines
505 UTF-8 filenames, as specified in `RFC 2231`_, are supported.506507 .. _RFC 2231: https://tools.ietf.org/html/rfc2231#section-4508509 .. versionchanged:: 1.0.3510 Filenames are encoded with ASCII instead of Latin-1 for broader511 compatibility with WSGI servers.512513 :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 in
50 unmodified lines
567 raise TypeError('filename unavailable, required for '568 'sending as attachment')569570 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 lines
modified:tests/test_helpers.py+15 added-8 removed
636 unmodified lines
637 assert value == 'attachment'638 assert options['filename'] == 'index.txt'639 rv.close()640641 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_disposition657658 def test_static_file(self, app, req_ctx):659 # default cache timeout is 12 hours660
294 unmodified lines
modified:CHANGES.rst+6 added
7 unmodified lines
8-------------910Unreleased1112- :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`_)1516.. _#2766: https://github.com/pallets/flask/issues/2766171819Version 1.0.220-------------2122Released on May 2nd 2018
894 unmodified lines