From 52ed520bb716d7150e46b1a0f8b18d2ee770c4e5 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 1 Oct 2018 20:52:59 -0400 Subject: [PATCH] Prevent 500 on missing refs --- gitsrht/blueprints/repo.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/gitsrht/blueprints/repo.py b/gitsrht/blueprints/repo.py index 31507f8..cc700b1 100644 --- a/gitsrht/blueprints/repo.py +++ b/gitsrht/blueprints/repo.py @@ -101,7 +101,10 @@ def tree(owner, repo, ref, path): abort(401) git_repo = CachedRepository(repo.path) ref = ref or git_repo.default_branch().name[len("refs/heads/"):] - commit = git_repo.revparse_single(ref) + try: + commit = git_repo.revparse_single(ref) + except KeyError: + abort(404) if isinstance(commit, pygit2.Tag): commit = git_repo.get(commit.target) @@ -147,7 +150,10 @@ def raw_blob(owner, repo, ref, path): abort(401) git_repo = CachedRepository(repo.path) ref = ref or git_repo.default_branch().name[len("refs/heads/"):] - commit = git_repo.revparse_single(ref) + try: + commit = git_repo.revparse_single(ref) + except KeyError: + abort(404) if isinstance(commit, pygit2.Tag): commit = git_repo.get(commit.target) @@ -183,7 +189,10 @@ def archive(owner, repo, ref): abort(401) git_repo = CachedRepository(repo.path) ref = ref or git_repo.default_branch().name[len("refs/heads/"):] - commit = git_repo.revparse_single(ref) + try: + commit = git_repo.revparse_single(ref) + except KeyError: + abort(404) if isinstance(commit, pygit2.Tag): commit = git_repo.get(commit.target) @@ -262,7 +271,10 @@ def log(owner, repo, ref, path): abort(401) git_repo = CachedRepository(repo.path) ref = ref or git_repo.default_branch().name[len("refs/heads/"):] - commit = git_repo.revparse_single(ref) + try: + commit = git_repo.revparse_single(ref) + except KeyError: + abort(404) if isinstance(commit, pygit2.Tag): commit = git_repo.get(commit.target) refs = collect_refs(git_repo) @@ -290,7 +302,10 @@ def commit(owner, repo, ref): if not has_access(repo, UserAccess.read): abort(401) git_repo = CachedRepository(repo.path) - commit = git_repo.revparse_single(ref) + try: + commit = git_repo.revparse_single(ref) + except KeyError: + abort(404) if isinstance(commit, pygit2.Tag): ref = git_repo.get(commit.target) try: @@ -313,7 +328,10 @@ def patch(owner, repo, ref): if not has_access(repo, UserAccess.read): abort(401) git_repo = CachedRepository(repo.path) - commit = git_repo.revparse_single(ref) + try: + commit = git_repo.revparse_single(ref) + except KeyError: + abort(404) if isinstance(commit, pygit2.Tag): ref = git_repo.get(commit.target) subp = subprocess.run([ @@ -381,7 +399,10 @@ def ref(owner, repo, ref): if not has_access(repo, UserAccess.read): abort(401) git_repo = CachedRepository(repo.path) - tag = git_repo.revparse_single(ref) + try: + tag = git_repo.revparse_single(ref) + except KeyError: + abort(404) if not isinstance(tag, pygit2.Tag): abort(404) return render_template("ref.html", view="refs", -- 2.38.4