GHSA-649p-mmhf-85c7 Gitea: Cached Per-Branch Permission Check in Pre-Receive Hook Allows Full Repository Write
Vulnerability Header| Field               | Value                                                                               |
| ------------------- | ----------------------------------------------------------------------------------- |
| Vulnerability Title | Cached Per-Branch Permission Check in Pre-Receive Hook Allows Full Repository Write |
| Severity Rating     | High                                                                                |
| Bug Category        | Authorization Bypass                                                                |
| Location            | `routers/private/hook_pre_receive.go:55-64`, `CanWriteCode()`                       |
| Affected Versions   | 1.25.5                                                                              |Executive SummaryThe pre-receive hook in Gitea evaluates the `CanMaintainerWriteToBranch` permission only once per `git push` session and caches the result for all subsequent refs in the same batch. An attacker who has a legitimate per-branch write grant (e.g., via an open pull request with "Allow edits from maintainers" enabled) can batch-push that branch together with any other ref. The cached `true` from the first ref is reused for all following refs, allowing the attacker to overwrite protected branches (including `main`), create arbitrary new branches, and push tags. This effectively escalates a single-branch maintainer-edit grant into full repository write access.Root Cause AnalysisTechnical DescriptionWhen processing a multi-ref `git push`, the `HookPreReceive` handler at `hook_pre_receive.go:107` iterates over all incoming refs. For each branch ref, `preReceiveBranch` (`:140`) updates `ctx.branchName` to the current branch (`:142`) and then calls `AssertCanWriteCode()` (`:144`).`CanWriteCode()` (`:55-64`) checks whether the user can write to the repository. On the first call, it evaluates `issues_model.CanMaintainerWriteToBranch(ctx, userPerm, ctx.branchName, user)` and stores the result in a boolean flag (`canWriteCode`) with a guard (`checkedCanWriteCode`). On all subsequent calls within the same batch, it returns the cached boolean without re-evaluating against the now-different `ctx.branchName`.This means the permission check is branch-specific in its inputs but session-scoped in its caching — a classic check-vs-use divergence.A second contributing factor is the AGit-flow relaxation at `routers/web/repo/githttp.go:190-192` (and `routers/private/serv.go:337-338`), which downgrades the outer `receive-pack` access gate from `Write` to `Read` when `git.DefaultFeatures().SupportProcReceive` is true (git ≥ 2.29). This allows a user with only Read access on a repository to initiate a `receive-pack` session, deferring all authorization to the pre-receive hook — which contains the caching bug described above.First Faulty Condition| File      | `routers/private/hook_pre_receive.go` |
| --------- | ------------------------------------- |
| Line      | 55-64                                 |
| Condition | `CanWriteCode()` evaluates the branch-specific `CanMaintainerWriteToBranch` check only on the first invocation and caches the result, reusing it for all subsequent refs in the batch regardless of which branch they target. |Trace AnalysisThe following is the path from the attacker's `git push` to the authorization fault:`POST /{owner}/{repo}.git/git-receive-pack` → `routers/web/repo/githttp.go:437` (`ServiceReceivePack`) → `httpBase()` (`:60`)Access gate is downgraded from Write to Read at `:190-192` due to AGit-flow support.`git receive-pack` invokes the pre-receive hook → `cmd/hook.go:184` (`runHookPreReceive`) → `modules/private/hook.go:96` (`HookPreReceive`) → internal API → `routers/private/hook_pre_receive.go:107` (`HookPreReceive`)Loop at `:117` iterates over all refs in the batch. For each branch ref, `preReceiveBranch` (`:140`) sets `ctx.branchName` at `:142`.**Fault**: `AssertCanWriteCode()` (`:144`) → `CanWriteCode()` (`:55-64`).First ref (`feature-branch`): `checkedCanWriteCode` is false → evaluates `CanMaintainerWriteToBranch(ctx, userPerm, "feature-branch", user)` → returns `true` (legitimate grant) → caches result.Second ref (`main`): `checkedCanWriteCode` is already true → returns cached `true` **without re-evaluating** against `"main"`.Hook returns 200 → `git receive-pack` accepts all refs → `main` is overwritten in the victim's repository.Exploitability AssessmentAttack Vector & Reachability| Attack vector               | Network                                                                             |
| --------------------------- | ----------------------------------------------------------------------------------- |
| Authentication required     | Low                                                                                 |
| User interaction required   | Required. Victim must enable "Allow edits from maintainers" on their PR             |
| Reachable in default config | Yes                                                                                 |
| Entry point                 | `git push` over smart-HTTP or SSH with multiple refs in a single operation          |The attacker gains full write access to the victim's repository — equivalent to having push permissions on all refs. By controlling the order of refs in the batch (e.g., naming the granted branch so it sorts first), the attacker reliably ensures the legitimate ref is evaluated before the target. This is not a race condition; it is deterministic.Reproduction Steps**Environment**
The issue was reproduced using Gitea v1.25.5 on Ubuntu 24.04.4 LTS.**Prerequisites:**a Gitea instance with two users, `attacker` and `victim`.At this point, the attacker (as maintainer of the base repo `attacker/project`) has a per-branch write grant on the victim's fork, scoped to the `feature-branch` branch only.**Attack**The attacker works from their own repo (`attacker/project`)**Expected result**: 
`main` should be rejected ("User permission denied for writing").**Actual result**: 
Both refs are accepted. `victim/project:main` now contains the attacker's malicious commit.The same technique also works for pushing arbitrary tags (`refs/tags/*`) and creating new branches.Recommended FixRemove the caching in `CanWriteCode()` — the `CanMaintainerWriteToBranch` check must be evaluated for every ref in the batch, not cached after the first call. The `checkedCanWriteCode` / `canWriteCode` fields on `preReceiveContext` and the guard in `CanWriteCode()` at `hook_pre_receive.go:55-64` should be removed, so the permission is evaluated fresh each time `preReceiveBranch` or `preReceiveTag` calls it. `loadPusherAndPermission()` already has its own caching (`loadedPusher`), so the per-call cost is limited to the `CanMaintainerWriteToBranch` query.See [diff.patch](https://github.com/user-attachments/files/28831842/diff.patch) for the proposed fix.Patch provenance: AI-generated, human-reviewed.AttributionThis vulnerability was discovered by Claude, Anthropic's AI assistant, and triaged by **Adrian Denkiewicz** at **Doyensec** in collaboration with Anthropic Research.For CVE credits and public acknowledgments: **Doyensec in collaboration with Claude and Anthropic Research**
CVSS v3.1 score: 8.8