Skip to content

Commit

Permalink
fix: Check and sanitize filename before passing to file.write (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneberth authored Nov 22, 2024
1 parent f0620e1 commit d8f28f9
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/viur/toolkit/importer/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import mimetypes
import numbers
import re
import string
import time
import typing as t

Expand Down Expand Up @@ -262,6 +263,13 @@ def import_file(self, info: dict[str, t.Any]) -> None | db.Key:

logger.debug(f"{name=} has {len(content)!r} bytes")

if not conf.main_app.vi.file.is_valid_filename(name):
logger.error(f"file {name=} is invalid")
# simplify name
name = "".join(char for char in name if char in string.ascii_letters + string.digits + " _-.")
name = name[:conf.main_app.vi.file.MAX_FILENAME_LEN]
logger.info(f"Use sanitized {name=}")

if "import_key" in dir(file_skel_cls):
return conf.main_app.vi.file.write(name, content, mimetype, import_key=info["key"])

Expand Down

0 comments on commit d8f28f9

Please sign in to comment.