From 06d89a7e5e5ef56e8186d0dd02b5b7ef1951fe6e Mon Sep 17 00:00:00 2001 From: Armin Weigl Date: Sat, 20 Mar 2021 15:47:06 +0100 Subject: [PATCH] Fix 500 on invalid file or directory name This fixes server errors occuring when displaying a commit or tree, which contains an non decodeable file or directory name --- gitsrht/blueprints/api/porcelain.py | 2 +- gitsrht/git.py | 23 ++++++++++++----------- gitsrht/templates/utils.html | 20 ++++++++++---------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/gitsrht/blueprints/api/porcelain.py b/gitsrht/blueprints/api/porcelain.py index 7be92c1..ad45c1a 100644 --- a/gitsrht/blueprints/api/porcelain.py +++ b/gitsrht/blueprints/api/porcelain.py @@ -48,7 +48,7 @@ def tree_to_dict(t): "short_id": t.short_id, "entries": [ { - "name": e.name, + "name": e.raw_name.decode("utf-8", "replace"), "id": str(e.id), "type": (e.type_str if hasattr(e, "type_str") else e.type), "mode": e.filemode, diff --git a/gitsrht/git.py b/gitsrht/git.py index 669913d..1a4f03e 100644 --- a/gitsrht/git.py +++ b/gitsrht/git.py @@ -95,7 +95,7 @@ class AnnotatedTreeEntry: self.commit = None if entry is not None: self.id = entry.id.hex - self.name = entry.name + self.name = entry.raw_name.decode("utf-8", "replace") self.type = (entry.type_str if hasattr(entry, "type_str") else entry.type) self.filemode = entry.filemode @@ -143,28 +143,29 @@ def annotate_tree(repo, tree, commit): def _diffstat_name(delta, anchor): if delta.status == pygit2.GIT_DELTA_DELETED: return Markup(escape(delta.old_file.path)) - if delta.old_file.path == delta.new_file.path: + if delta.old_file.raw_path == delta.new_file.raw_path: return Markup( - f"" + - f"{escape(delta.old_file.path)}" + + f"" + + f"{escape(delta.old_file.raw_path.decode('utf-8', 'replace'))}" + f"") # Based on git/diff.c pfx_length = 0 - old_path = delta.old_file.path - new_path = delta.new_file.path + old_path = delta.old_file.raw_path + new_path = delta.new_file.raw_path for i in range(max(len(old_path), len(new_path))): if i >= len(old_path) or i >= len(new_path): break if old_path[i] != new_path[i]: break - if old_path[i] == '/': + if old_path[i] == b'/'[0]: pfx_length = i + 1 # TODO: detect common suffix if pfx_length != 0: - return (f"{delta.old_file.path[:pfx_length]}{{" + - f"{delta.old_file.path[pfx_length:]} => {delta.new_file.path[pfx_length:]}" + - f"}}") - return f"{delta.old_file.path} => {delta.new_file.path}" + return (f"{delta.old_file.raw_path[:pfx_length].decode('utf-8', 'replace')}{{" + + f"{delta.old_file.raw_path[pfx_length:].decode('utf-8', 'replace')} => " + + f"{delta.new_file.raw_path[pfx_length:].decode('utf-8', 'replace')}}}") + return (f"{delta.old_file.raw_path.decode('utf-8', 'replace')} => " + + f"{delta.new_file.raw_path.decode('utf-8', 'replace')}") def _diffstat_line(delta, patch, anchor): name = _diffstat_name(delta, anchor) diff --git a/gitsrht/templates/utils.html b/gitsrht/templates/utils.html index 3ca36c7..1e064eb 100644 --- a/gitsrht/templates/utils.html +++ b/gitsrht/templates/utils.html @@ -164,24 +164,24 @@ endif %}{% endfor %} owner=repo.owner.canonical_name, repo=repo.name, ref=parent.id.hex, - path=patch.delta.old_file.path)}}" - id="{{anchor}}{{patch.delta.old_file.path}}" + path=patch.delta.old_file.raw_path.decode('utf-8', 'replace'))}}" + id="{{anchor}}{{patch.delta.old_file.raw_path.decode('utf-8', 'replace')}}" {% if target_blank %} target="_blank" {% endif %} - >{{patch.delta.old_file.path}}{# + >{{patch.delta.old_file.raw_path.decode('utf-8', 'replace')}}{# #}{% endif %} => {# #}{{patch.delta.new_file.path}}{# + >{{patch.delta.new_file.raw_path.decode('utf-8', 'replace')}}{# #} +{{patch.line_stats[1]}}{# #} -{{patch.line_stats[2]}}{% if patch.delta.old_file.mode != patch.delta.new_file.mode %}{# @@ -198,7 +198,7 @@ endif %}{% endfor %} owner=repo.owner.canonical_name, repo=repo.name, ref=parent.id.hex, - path=patch.delta.old_file.path)}}#L{{hunk.old_start}}" + path=patch.delta.old_file.raw_path.decode('utf-8', 'replace'))}}#L{{hunk.old_start}}" {% if target_blank %} target="_blank" {% endif %} @@ -209,7 +209,7 @@ endif %}{% endfor %} owner=repo.owner.canonical_name, repo=repo.name, ref=commit.id.hex, - path=patch.delta.new_file.path)}}#L{{hunk.new_start}}" + path=patch.delta.new_file.raw_path.decode('utf-8', 'replace'))}}#L{{hunk.new_start}}" {% if target_blank %} target="_blank" {% endif %} @@ -220,8 +220,8 @@ endif %}{% endfor %} "+":"text-success", "-":"text-danger", }).get(line.origin) or ""}}">