Sfoglia il codice sorgente

security: prevent deletion of protected and default branches via web UI (#8124)

https://github.com/gogs/gogs/security/advisories/GHSA-2c6v-8r3v-gh6p

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
ᴊᴏᴇ ᴄʜᴇɴ 1 settimana fa
parent
commit
7b7e38c880
2 ha cambiato i file con 17 aggiunte e 0 eliminazioni
  1. 2 0
      conf/locale/locale_en-US.ini
  2. 15 0
      internal/route/repo/branch.go

+ 2 - 0
conf/locale/locale_en-US.ini

@@ -494,6 +494,8 @@ branches.stale_branches = Stale Branches
 branches.all = All Branches
 branches.all = All Branches
 branches.updated_by = Updated %[1]s by %[2]s
 branches.updated_by = Updated %[1]s by %[2]s
 branches.change_default_branch = Change Default Branch
 branches.change_default_branch = Change Default Branch
+branches.default_deletion_not_allowed = Cannot delete the default branch.
+branches.protected_deletion_not_allowed = Cannot delete a protected branch.
 
 
 editor.new_file = New file
 editor.new_file = New file
 editor.upload_file = Upload file
 editor.upload_file = Upload file

+ 15 - 0
internal/route/repo/branch.go

@@ -118,6 +118,21 @@ func DeleteBranchPost(c *context.Context) {
 	if !c.Repo.GitRepo.HasBranch(branchName) {
 	if !c.Repo.GitRepo.HasBranch(branchName) {
 		return
 		return
 	}
 	}
+	if branchName == c.Repo.Repository.DefaultBranch {
+		c.Flash.Error(c.Tr("repo.branches.default_deletion_not_allowed"))
+		return
+	}
+
+	protectBranch, err := database.GetProtectBranchOfRepoByName(c.Repo.Repository.ID, branchName)
+	if err != nil && !database.IsErrBranchNotExist(err) {
+		log.Error("Failed to get protected branch %q: %v", branchName, err)
+		return
+	}
+	if protectBranch != nil && protectBranch.Protected {
+		c.Flash.Error(c.Tr("repo.branches.protected_deletion_not_allowed"))
+		return
+	}
+
 	if len(commitID) > 0 {
 	if len(commitID) > 0 {
 		branchCommitID, err := c.Repo.GitRepo.BranchCommitID(branchName)
 		branchCommitID, err := c.Repo.GitRepo.BranchCommitID(branchName)
 		if err != nil {
 		if err != nil {