Skip to content

Preventing External File References Using Relative Paths in Image Tags #3

@jp-96

Description

@jp-96

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions