Skip to content
Myr

Deprecated send_file etag support and mimetype guessing for file-like objects. This fixes #104

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

authored by Armin Ronacher · committed by Armin Ronacher · parent faa1c71 · Browse files · 4 files+116 added−32 removed
This commit

State

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

Diff stats

4 files+116 added−32 removed

  • tests:+69 added−31 removed
  • other:+47 added−1 removed

4 files+116 added−32 removed

modified:docs/upgrading.rst+17 added
25 unmodified lines
26raise a :exc:`RuntimeError` instead of an :exc:`AttributeError` when they27are unbound. If you cought these exceptions with :exc:`AttributeError`28before, you should catch them with :exc:`RuntimeError` now.2930Additionally the :func:`~flask.send_file` function is now issuing31deprecation warnings if you depend on functionality that will be removed32in Flask 1.0. Previously it was possible to use etags and mimetypes33when file objects were passed. This was unreliable and caused issues34for a few setups. If you get a deprecation warning, make sure to35update your application to work with either filenames there or disable36etag attaching and attach them yourself.3738Old code::3940 return send_file(my_file_object)41 return send_file(my_file_object)4243New code::4445 return send_file(my_file_object, add_etags=False)4647Version 0.648-----------4950Flask 0.6 comes with a backwards incompatible change which affects the
87 unmodified lines
modified:flask/helpers.py+26 added-1 removed
257 unmodified lines
258 requires support of the underlying webserver for `X-Sendfile`.259260 By default it will try to guess the mimetype for you, but you can261 also explicitly provide one. For extra security you probably want262 to sent certain files as attachment (HTML for instance).262 to sent certain files as attachment (HTML for instance). The mimetype263 guessing requires a `filename` or an `attachment_filename` to be264 provided.265266 Please never pass filenames to this function from user sources without267 checking them first. Something like this is usually sufficient to268 avoid security problems::
6 unmodified lines
275 .. versionadded:: 0.5276 The `add_etags`, `cache_timeout` and `conditional` parameters were277 added. The default behaviour is now to attach etags.278279 .. versionchanged:: 0.7280 mimetype guessing and etag support for file objects was281 deprecated because it was unreliable. Pass a filename if you are282 able to, otherwise attach an etag yourself. This functionality283 will be removed in Flask 1.0284285 :param filename_or_fp: the filename of the file to send. This is286 relative to the :attr:`~Flask.root_path` if a287 relative path is specified.288 Alternatively a file object might be provided
13 unmodified lines
302 if isinstance(filename_or_fp, basestring):303 filename = filename_or_fp304 file = None305 else:306 from warnings import warn307 file = filename_or_fp308 filename = getattr(file, 'name', None)309310 # XXX: this behaviour is now deprecated because it was unreliable.311 # removed in Flask 1.0312 if not attachment_filename and not mimetype \313 and isinstance(filename, basestring):314 warn(DeprecationWarning('The filename support for file objects '315 'passed to send_file is not deprecated. Pass an '316 'attach_filename if you want mimetypes to be guessed.'),317 stacklevel=2)318 if add_etags:319 warn(DeprecationWarning('In future flask releases etags will no '320 'longer be generated for file objects passed to the send_file '321 'function because this behaviour was unreliable. Pass '322 'filenames instead if possible, otherwise attach an etag '323 'yourself based on another value'), stacklevel=2)324325 if filename is not None:326 if not os.path.isabs(filename):327 filename = os.path.join(current_app.root_path, filename)328 if mimetype is None and (filename or attachment_filename):
160 unmodified lines
modified:tests/flask_tests.py+69 added-31 removed
32 unmodified lines
33SECRET_KEY = 'devkey'343536@contextmanager37def catch_warnings():38 """Catch warnings in a with block in a list"""39 import warnings40 filters = warnings.filters41 warnings.filters = filters[:]42 old_showwarning = warnings.showwarning43 log = []44 def showwarning(message, category, filename, lineno, file=None, line=None):45 log.append(locals())46 try:47 warnings.showwarning = showwarning48 yield log49 finally:50 warnings.filters = filters51 warnings.showwarning = old_showwarning525354@contextmanager55def catch_stderr():56 """Catch stderr in a StringIO"""57 old_stderr = sys.stderr58 sys.stderr = rv = StringIO()59 try:60 yield rv
791 unmodified lines
852 assert rv.mimetype == 'text/html'853854 def test_send_file_object(self):855 app = flask.Flask(__name__)856 with catch_warnings() as captured:837 with app.test_request_context():838 f = open(os.path.join(app.root_path, 'static/index.html'))839 rv = flask.send_file(f)840 with app.open_resource('static/index.html') as f:841 assert rv.data == f.read()842 assert rv.mimetype == 'text/html'857 with app.test_request_context():858 f = open(os.path.join(app.root_path, 'static/index.html'))859 rv = flask.send_file(f)860 with app.open_resource('static/index.html') as f:861 assert rv.data == f.read()862 assert rv.mimetype == 'text/html'863 # mimetypes + etag864 assert len(captured) == 2865866 app.use_x_sendfile = True867 with catch_warnings() as captured:845 with app.test_request_context():846 f = open(os.path.join(app.root_path, 'static/index.html'))847 rv = flask.send_file(f)848 assert rv.mimetype == 'text/html'849 assert 'x-sendfile' in rv.headers850 assert rv.headers['x-sendfile'] == \851 os.path.join(app.root_path, 'static/index.html')868 with app.test_request_context():869 f = open(os.path.join(app.root_path, 'static/index.html'))870 rv = flask.send_file(f)871 assert rv.mimetype == 'text/html'872 assert 'x-sendfile' in rv.headers873 assert rv.headers['x-sendfile'] == \874 os.path.join(app.root_path, 'static/index.html')875 # mimetypes + etag876 assert len(captured) == 2877878 app.use_x_sendfile = False879 with app.test_request_context():855 f = StringIO('Test')856 rv = flask.send_file(f)857 assert rv.data == 'Test'858 assert rv.mimetype == 'application/octet-stream'859 f = StringIO('Test')860 rv = flask.send_file(f, mimetype='text/plain')861 assert rv.data == 'Test'862 assert rv.mimetype == 'text/plain'880 with catch_warnings() as captured:881 f = StringIO('Test')882 rv = flask.send_file(f)883 assert rv.data == 'Test'884 assert rv.mimetype == 'application/octet-stream'885 # etags886 assert len(captured) == 1887 with catch_warnings() as captured:888 f = StringIO('Test')889 rv = flask.send_file(f, mimetype='text/plain')890 assert rv.data == 'Test'891 assert rv.mimetype == 'text/plain'892 # etags893 assert len(captured) == 1894895 app.use_x_sendfile = True896 with catch_warnings() as captured:865 with app.test_request_context():866 f = StringIO('Test')867 rv = flask.send_file(f)868 assert 'x-sendfile' not in rv.headers897 with app.test_request_context():898 f = StringIO('Test')899 rv = flask.send_file(f)900 assert 'x-sendfile' not in rv.headers901 # etags902 assert len(captured) == 1903904 def test_attachment(self):905 app = flask.Flask(__name__)906 with catch_warnings() as captured:872 with app.test_request_context():873 f = open(os.path.join(app.root_path, 'static/index.html'))874 rv = flask.send_file(f, as_attachment=True)875 value, options = parse_options_header(rv.headers['Content-Disposition'])876 assert value == 'attachment'907 with app.test_request_context():908 f = open(os.path.join(app.root_path, 'static/index.html'))909 rv = flask.send_file(f, as_attachment=True)910 value, options = parse_options_header(rv.headers['Content-Disposition'])911 assert value == 'attachment'912 # mimetypes + etag913 assert len(captured) == 2914915 with app.test_request_context():916 assert options['filename'] == 'index.html'917 rv = flask.send_file('static/index.html', as_attachment=True)
2 unmodified lines
920 assert options['filename'] == 'index.html'921922 with app.test_request_context():923 rv = flask.send_file(StringIO('Test'), as_attachment=True,887 attachment_filename='index.txt')924 attachment_filename='index.txt',925 add_etags=False)926 assert rv.mimetype == 'text/plain'927 value, options = parse_options_header(rv.headers['Content-Disposition'])928 assert value == 'attachment'929 assert options['filename'] == 'index.txt'
303 unmodified lines
modified:CHANGES+4 added
11 unmodified lines
12 which can be used by subclasses to alter the default13 behaviour for `OPTIONS` responses.14- Unbound locals now raise a proper :exc:`RuntimeError` instead15 of an :exc:`AttributeError`.16- Mimetype guessing and etag support based on file objects is now17 deprecated for :func:`flask.send_file` because it was unreliable.18 Pass filenames instead or attach your own etags and provide a19 proper mimetype by hand.2021Version 0.6.122-------------23
151 unmodified lines