Skip to content

Commit

Permalink
Merge pull request #10 from seibert-media/utf8-encode
Browse files Browse the repository at this point in the history
filename: encode utf-8 filenames with RFC 5987
  • Loading branch information
phosei authored Jan 13, 2025
2 parents 49dc534 + 83db6e2 commit 71d7638
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion fluesterfix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from shutil import rmtree
from string import ascii_letters, digits
from subprocess import run
import unicodedata
from urllib.parse import quote

from flask import Flask, jsonify, make_response, redirect, request, url_for
from markupsafe import escape
Expand Down Expand Up @@ -414,8 +416,24 @@ def reveal(sid, key):
# "downloads", it confuses some browsers and they don't download
# anything -- and it doesn't make a lot of sense anyway to use
# 410 here.

# HTTP Headers can only be ascii, to encode UTF-8 filenames a special header tag an encoding is needed:
# https://stackoverflow.com/a/20933751
# Code for Flask specifically was taken from
# https://stackoverflow.com/a/43376150
try:
filename.encode("ascii")
except UnicodeEncodeError:
simple = unicodedata.normalize("NFKD", filename)
simple = simple.encode("ascii", "ignore").decode("ascii")
# safe = RFC 5987 attr-char
quoted = quote(filename, safe="!#$&+-.^_`|~")
names = {"filename": simple, "filename*": f"UTF-8''{quoted}"}
else:
names = {"filename": filename}

response = make_response(secret_bytes, 200)
response.headers.set('Content-Disposition', 'attachment', filename=filename)
response.headers.set('Content-Disposition', 'attachment', **names)
response.headers.set('Content-Type', 'application/octet-stream')
return response
else:
Expand Down

0 comments on commit 71d7638

Please sign in to comment.