Skip to content

Commit

Permalink
files: set url-escaped filename content disposition
Browse files Browse the repository at this point in the history
  • Loading branch information
kpsherva committed Nov 2, 2023
1 parent c9a885c commit 7e6b9a5
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions site/zenodo_rdm/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import mimetypes
import unicodedata
from urllib.parse import urlsplit, urlunsplit
from urllib.parse import quote, urlsplit, urlunsplit

import requests
from flask import current_app, make_response, request
Expand Down Expand Up @@ -104,15 +104,18 @@ def send_file(
try:
filenames = {"filename": filename.encode("latin-1")}
except UnicodeEncodeError:
filenames = {"filename*": "UTF-8''%s" % url_quote(filename)}
# safe = RFC 5987 attr-char
quoted = quote(filename, safe="!#$&+-.^_`|~")

filenames = {"filename*": "UTF-8''%s" % quoted}
encoded_filename = unicodedata.normalize("NFKD", filename).encode(
"latin-1", "ignore"
)
if encoded_filename:
filenames["filename"] = encoded_filename
response.headers.add("Content-Disposition", "attachment", **filenames)
response.headers.set("Content-Disposition", "attachment", **filenames)
else:
response.headers.add("Content-Disposition", "inline")
response.headers.set("Content-Disposition", "inline")

# Security-related headers for the download (from invenio-files-rest)
response.headers["Content-Security-Policy"] = "default-src 'none';"
Expand Down

0 comments on commit 7e6b9a5

Please sign in to comment.