GitHub Security Advisory: GHSA-2c6v-8r3v-gh6p
Package: gogs.io/gogs (GO)
Severity: HIGH (CVSS 0.0)
CWE: CWE-863: Incorrect Authorization
Vulnerable versions: < 0.14.1
Patched in: 0.14.1

Summary: Gogs has a Protected Branch Deletion Bypass in Web Interface

Details:
## Summary

An access control bypass vulnerability in Gogs web interface allows any repository collaborator with Write permissions to delete protected branches (including the default branch) by sending a direct POST request, completely bypassing the branch protection mechanism. This vulnerability enables privilege escalation from Write to Admin level, allowing low-privilege users to perform dangerous operations that should be restricted to administrators only.

Although Git Hook layer correctly prevents protected branch deletion via SSH push, the web interface deletion operation does not trigger Git Hooks, resulting in complete bypass of protection mechanisms.

## Details

### Affected Component

- **File**: `internal/route/repo/branch.go`
- **Function**: `DeleteBranchPost` (lines 110-155)
- **Route Configuration**: `internal/cmd/web.go:589`
  ```go
  m.Post("/delete/*", reqSignIn, reqRepoWriter, repo.DeleteBranchPost)
  ```

### Root Cause

The `DeleteBranchPost` function performs the following checks when deleting a branch:
1. ✅ User authentication (`reqSignIn`)
2. ✅ Write permission check (`reqRepoWriter`)
3. ✅ Branch existence verification
4. ✅ CommitID matching (optional parameter)
5. ❌ **Missing protected branch check**
6. ❌ **Missing default branch check**

While the UI layer (`internal/route/repo/issue.go:646-658`) correctly checks protected branch status and hides the delete button, attackers can directly construct POST requests to bypass UI restrictions.

### Vulnerable Code

**Vulnerable implementation** (`internal/route/repo/branch.go:110-155`):

```110:155:internal/route/repo/branch.go
func DeleteBranchPost(c *context.Context) {
	branchName := c.Params("*")
	commitID := c.Query("commit")

	defer func() {
		redirectTo := c.Query("redirect_to")
		if !tool.IsSameSiteURLPath(redirectTo) {
			redirectTo = c.Repo.RepoLink
		}
		c.Redirect(redirectTo)
	}()

	if !c.Repo.GitRepo.HasBranch(branchName) {
		return
	}
	if len(commitID) > 0 {
		branchCommitID, err := c.Repo.GitRepo.BranchCommitID(branchName)
		if err != nil {
			log.Error("Failed to get commit ID of branch %q: %v", branchName, err)
			return
		}

		if branchCommitID != commitID {
			c.Flash.Error(c.Tr("repo.pulls.delete_branch_has_new_commits"))
			return
		}
	}

	// 🔴 Vulnerability: Missing protected branch check here
	// Should add check like:
	// protectBranch, err := database.GetProtectBranchOfRepoByName(c.Repo.Repository.ID, branchName)
	// if protectBranch != nil && protectBranch.Protected { ... }

	if err := c.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
		Force: true,
	}); err != nil {
		log.Error("Failed to delete branch %q: %v", branchName, err)
		return
	}

	if err := database.PrepareWebhooks(c.Repo.Repository, database.HookEventTypeDelete, &api.DeletePayload{
		Ref:        branchName,
		RefType:    "branch",
		PusherType: api.PUSHER_TYPE_USER,
		Repo:       c.Repo.Repository.APIFormatLegacy(nil),
		Sender:     c.User.APIFormat(),
	}); err != nil {
		l