[v10.0/forgejo] fix: disallow blame on directories (#6720)

**Backport:** https://codeberg.org/forgejo/forgejo/pulls/6716

- Don't allow the blame operation on directories.
- Added integration test.
- Resolves forgejo/forgejo#6533

Co-authored-by: Gusted <postmaster@gusted.xyz>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6720
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
This commit is contained in:
forgejo-backport-action 2025-01-29 09:34:03 +00:00 committed by Gusted
parent c198cb6e65
commit 4016f2890d
2 changed files with 17 additions and 0 deletions

View file

@ -56,6 +56,11 @@ func RefBlame(ctx *context.Context) {
HandleGitError(ctx, "Repo.Commit.GetTreeEntryByPath", err)
return
}
if entry.IsDir() {
ctx.NotFound("Cannot blame directory", nil)
return
}
blob := entry.Blob()
ctx.Data["PageIsViewCode"] = true

View file

@ -1462,3 +1462,15 @@ func TestRepoSubmoduleView(t *testing.T) {
htmlDoc.AssertElement(t, fmt.Sprintf(`tr[data-entryname="repo1"] a[href="%s"]`, u.JoinPath("/user2/repo1").String()), true)
})
}
func TestBlameDirectory(t *testing.T) {
defer tests.PrepareTestEnv(t)()
// Ensure directory exists.
req := NewRequest(t, "GET", "/user2/repo59/src/branch/master/deep")
MakeRequest(t, req, http.StatusOK)
// Blame is not allowed
req = NewRequest(t, "GET", "/user2/repo59/blame/branch/master/deep")
MakeRequest(t, req, http.StatusNotFound)
}