Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flask.send_file can't handle some attachment_filename with unicode characters #2933

Closed
remyroy opened this issue Oct 3, 2018 · 0 comments
Closed
Milestone

Comments

@remyroy
Copy link
Contributor

remyroy commented Oct 3, 2018

Expected Behavior

Using flask.send_file should just work no matter what the attachment_filename string parameter contains including unicode characters.

# -*- coding: utf-8 -*-
from flask import Flask, send_file
app = Flask(__name__)

@app.route("/")
def hello():
    fn = 'é.py'
    # alternate value
    # fn = '\xc3\xa9.py'
    return send_file('hello.py', as_attachment=True, attachment_filename=fn)

Actual Behavior

Exception is thrown. UnicodeDecodeError is not handled with a proper fallback.

Traceback (most recent call last):
  File "/home/remyroy/VirtualEnvs/TestingFlask/local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/remyroy/VirtualEnvs/TestingFlask/local/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/remyroy/VirtualEnvs/TestingFlask/local/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/remyroy/VirtualEnvs/TestingFlask/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/remyroy/VirtualEnvs/TestingFlask/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/remyroy/Projects/TestingFlask/hello.py", line 10, in hello
    return send_file('hello.py', as_attachment=True, attachment_filename=fn)
  File "/home/remyroy/VirtualEnvs/TestingFlask/local/lib/python2.7/site-packages/flask/helpers.py", line 567, in send_file
    attachment_filename = attachment_filename.encode('latin-1')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)

Environment

  • Python version: 2.7.12
  • Flask version: 1.0.2
  • Werkzeug version: 0.14.1
garenchan added a commit to garenchan/flask that referenced this issue Oct 4, 2018
If attachment filename is bytes type and contains non-ascii coded bytes,
then the following ASCII encoding process will trigger
UnicodeDecodeError exception.

Fix issue pallets#2933.
@davidism davidism added this to the 1.0.3 milestone Jan 7, 2019
davidism pushed a commit to garenchan/flask that referenced this issue Jan 7, 2019
If attachment filename is bytes type and contains non-ascii coded bytes,
then the following ASCII encoding process will trigger
UnicodeDecodeError exception.

Fix issue pallets#2933.
davidism added a commit that referenced this issue Jan 7, 2019
Fix issue #2933: Make sure the attachment filename is text type.
@davidism davidism closed this as completed Jan 7, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants