diff --git a/pagure/__init__.py b/pagure/__init__.py index 695aec8..f462636 100644 --- a/pagure/__init__.py +++ b/pagure/__init__.py @@ -310,7 +310,7 @@ def auth_logout(): # pragma: no cover return flask.redirect(return_point) -def __get_file_in_tree(repo_obj, tree, filepath): +def __get_file_in_tree(repo_obj, tree, filepath, bail_on_tree=False): ''' Retrieve the entry corresponding to the provided filename in a given tree. ''' @@ -322,6 +322,9 @@ def __get_file_in_tree(repo_obj, tree, filepath): if entry.name == filename: if len(filepath) == 1: blob = repo_obj[entry.oid] + # If we get a tree instead of a blob, let's escape + if isinstance(tree, pygit2.Tree) and bail_on_tree: + return blob content = blob.data # If it's a (sane) symlink, we try a single-level dereference if entry.filemode == pygit2.GIT_FILEMODE_LINK \ diff --git a/pagure/ui/issues.py b/pagure/ui/issues.py index 909b524..bde3335 100644 --- a/pagure/ui/issues.py +++ b/pagure/ui/issues.py @@ -722,7 +722,7 @@ def view_issue_raw_file(repo, filename=None, username=None): encoding = None content = __get_file_in_tree( - repo_obj, commit.tree, filename.split('/')) + repo_obj, commit.tree, filename.split('/'), bail_on_tree=True) if not content or isinstance(content, pygit2.Tree): flask.abort(404, 'File not found') diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 4efb6db..ba46e52 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -373,7 +373,7 @@ def view_file(repo, identifier, filename, username=None): if commit and not isinstance(commit, pygit2.Blob): content = __get_file_in_tree( - repo_obj, commit.tree, filename.split('/')) + repo_obj, commit.tree, filename.split('/'), bail_on_tree=True) if not content: flask.abort(404, 'File not found') content = repo_obj[content.oid] @@ -472,7 +472,7 @@ def view_raw_file(repo, identifier, filename=None, username=None): encoding = None if filename: content = __get_file_in_tree( - repo_obj, commit.tree, filename.split('/')) + repo_obj, commit.tree, filename.split('/'), bail_on_tree=True) if not content or isinstance(content, pygit2.Tree): flask.abort(404, 'File not found')