Objective: Restrict the replacement of picture files to those located within a subfolder of media_path.
# src/python_odt_template/jinja.py
def get_odt_renderer(media_path: str | Path, env: Environment = environment) -> ODTRenderer:
media_path = Path(media_path)
def image_filter(value):
file_path = media_path.joinpath(value).resolve()
file_path.relative_to(media_path) # validate subpath
return file_path
In this function, image_filter, the file_path is constructed by joining media_path with the provided value. The file_path is then resolved to an absolute path. To ensure that the file_path is a valid subpath of media_path, relative_to method is used for validation. If the file_path is not within a subfolder of media_path, an exception will be raised.
src/python_odt_template/django.py also similarly
Objective: Restrict the replacement of picture files to those located within a subfolder of
media_path.In this function,
image_filter, thefile_pathis constructed by joiningmedia_pathwith the providedvalue. Thefile_pathis then resolved to an absolute path. To ensure that thefile_pathis a valid subpath ofmedia_path,relative_tomethod is used for validation. If thefile_pathis not within a subfolder ofmedia_path, an exception will be raised.src/python_odt_template/django.pyalso similarly