From 36288d9fceb2408e316d9f07abedf44f8974c9f8 Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Apr 24 2025 21:17:26 +0000 Subject: Avoid crash resolving symlink in a tree that's not a Tree There's a codepath in `ui.repo.view_file` where we call `__get_file_in_tree()` and pass it a second argument that is not a pygit2 `Tree`, it's just a list: content = sorted(content, key=lambda x: x.filemode) ... readme_file = __get_file_in_tree( repo_obj, content, [i.name] ).data `sorted()` always produces a list, so the `content` we pass to `__get_file_in_tree` here is a list. But if the file we're trying to get is a symlink, we wind up in this section of `__get_file_in_tree` which implicitly assumes `tree` (the second arg) must be a pygit2 `Tree`, because it tries to index it using a bytestring. If `tree` is actually a list, trying to index it with a bytestring produces a `TypeError` not a `KeyError`, so the simplest fix is to just catch that exception too. Signed-off-by: Adam Williamson --- diff --git a/pagure/utils.py b/pagure/utils.py index e15fed0..33a6e0b 100644 --- a/pagure/utils.py +++ b/pagure/utils.py @@ -429,7 +429,7 @@ def __get_file_in_tree(repo_obj, tree, filepath, bail_on_tree=False): ): try: dereferenced = tree[content] - except KeyError: + except (KeyError, TypeError): pass else: if dereferenced.filemode == pygit2.GIT_FILEMODE_BLOB: