CVE-2026–60004 — RCE in Gitea via diffpatch Git Hook Injection
Gitea's diffpatch API applied a user-supplied patch to a temporary bare clone, and sending the same patch twice triggered Git's 3-way merge fallback, which wrote the file to disk despite the --cached flag. By targeting the hooks/post-index-change path, an attacker with write access to one repository could get arbitrary commands run as the Gitea service account.
Published on medium.com
Ubuntu
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Gitea exposes a diffpatch API that applies a user-supplied patch to a temporary internal repository. The temporary clone that diffpatch creates is a bare repository, so its root is $GIT_DIR: hooks/, objects/ and refs/ sit directly at the top level, with no working tree in between.
CVE-2026-60004 is the result of three ordinary Git and Gitea behaviours lining up: the bare clone, git apply's 3-way fallback, and Git's automatic execution of the post-index-change hook. Send the exact same patch twice and the merge conflict resolution writes the patched file to disk, inside the hooks directory of the bare clone. The next index update runs it as the Gitea service account.


Setup
The only precondition is write access to a single repository. On instances with public sign-up enabled, which the write-up describes as common on self-hosted deployments, an unauthenticated attacker can register an account, create a repository with auto_init, and proceed from there. No prior credentials are needed in that case.
The vulnerable logic lives in services/repository/files/patch.go. The relevant code is:
cmdApply := gitcmd.NewCommand("apply", "--index", "--recount", "--cached", "--binary")if git.DefaultFeatures().CheckVersionAtLeast("2.32") { cmdApply.AddArguments("-3")}
Two arguments matter here:
--cachedmeans "update the index only, never touch the working directory". This is the flag that makes the operation look safe.-3tells Git to fall back to a 3-way merge when a patch does not apply cleanly, for example because of a context mismatch. It is a usability improvement added in Git 2.32, and Gitea only adds it when the installed Git is at least that version.
Finding it
The researcher's observation is that this is not a single bug but a combination of three individually reasonable behaviours:
- Bare clones. The temporary clone
diffpatchcreates to apply the patch is--bare. A bare repository has no working tree, so the repository root is$GIT_DIR, andhooks/,objects/andrefs/all sit directly at the root. git apply's 3-way fallback. Since Git 2.32,git applycan retry with-3when a plain apply fails. That fallback is designed to check out the merge result into the working tree in order to resolve conflicts.- Automatic hook execution.
post-index-changeis a hook Git invokes on its own, with no human interaction, any time the index changes. If an executable file exists at that path, Git runs it.
Gitea's implementation did not account for what happens when the first and second collide. When the -3 fallback actually triggers, it does not honour the "nothing touches disk" guarantee that --cached is supposed to provide: the 3-way merge path resolves conflicts by checking the merged blob out to the real filesystem.
Exploitation
Step 1: register an account
The attacker registers an account. No prior credentials are needed if public sign-up is on.
Step 2: create a repository
The attacker creates a repository with auto_init. Write access to that one repository is the only precondition.
Step 3: send a patch adding the hook
The attacker sends a diffpatch request that adds a new file at hooks/post-index-change. The patch applies cleanly and lands in the index; thanks to --cached, nothing hits disk yet. If the patch's diff header specifies new file mode 100755, the file keeps its executable bit.
Step 4: resend the exact same patch
The attacker resends the identical patch. A file is now being "added" at a path that has already been added, which is an add/add conflict. On that conflict the -3 fallback kicks in and checks the merge result out to real disk. Because the clone is bare, the file that just landed at hooks/post-index-change is sitting inside the actual Git hooks directory.
Step 5: the hook runs
The next time git apply --index touches the index in that same clone, Git follows its normal hook-invocation path and runs post-index-change as a child process of the Gitea service, with the Gitea service account's privileges. That is arbitrary command execution.

The full attack flow, as the write-up lays it out:
- Attacker registers an account (no prior credentials needed if public sign-up is on).
- Attacker creates a repository with
auto_init; write access to that one repo is the only precondition. - Attacker sends a
diffpatchrequest addinghooks/post-index-change. - Attacker resends the exact same patch, forcing an add/add conflict.
- Git's 3-way fallback bypasses
--cachedand writes the hook file to the bare clone's realhooks/directory. - Git auto-executes
post-index-changeon the next index update, giving arbitrary command execution. - If the hook script stores command output as a Git object or a branch, the attacker retrieves it with a plain, authenticated
fetch, with no outbound connection from the server required.
One detail worth calling out: the hook's exit code is never surfaced in the diffpatch API response. A successful attack returns the same response as an ordinary, successful patch application, so there is no obvious signal in the HTTP layer that anything happened.
Every step rides on standard Git protocol and the standard Gitea REST API. From the outside it looks like an API client uploaded the same patch twice, which is why the write-up describes it as hard to catch with a WAF or network IDS.
Root cause
The mistake is the combination of --cached with -3 inside a bare temporary clone. --cached is a deliberate safety flag, the -3 fallback is a usability improvement, and automatic hook execution is core Git design rather than a bug. It is only when all three meet inside a temporary bare clone that the safety guarantee --cached was supposed to provide quietly breaks: the 3-way merge path resolves conflicts by checking the merged blob out to the real filesystem, and in a bare repository that filesystem location is the hooks directory itself.
The write-up's general lesson is that any feature which manipulates a temporary Git repository from untrusted input needs to account for the repository's shape (bare versus non-bare) and for the less-common fallback paths of the Git subcommands it invokes, not just their documented happy-path behaviour.
Impact
Anything the Gitea process can reach is in scope: app.ini secrets, process environment variables, every mounted repository, the database connection and its contents, OAuth and integration credentials, and any internal services reachable from that host.
On instances with public registration enabled this is effectively a pre-authentication RCE, with no stolen credentials required. The CVE has been added to CISA's Known Exploited Vulnerabilities (KEV) catalogue, with observed exploitation delivering cryptominer payloads.
Fix
Upgrade to Gitea 1.27.1 or later immediately. The fix shipped quietly, described in the release notes as "refactor: git patch apply" rather than flagged as a security fix, so check the version number itself rather than the changelog wording.
The write-up also lists operational measures:
- Disable public registration (
DISABLE_REGISTRATION = trueunder[service]inapp.ini) to remove the unauthenticated attack path entirely. - Run the Gitea process under a least-privilege dedicated account so a successful hook execution has a limited blast radius.
- Watch for repeated calls to
diffpatchagainst the same repository with the same patch body in a short window; that is the fingerprint of this attack. - Periodically audit repositories'
hooks/directories for recently created or modified executables (post-receive,pre-receive,update,post-index-change, and so on).
- Other hooks in the same bare clone. Try writing
post-receive,pre-receiveorupdatethrough the same double-patch technique. A positive result is an executable file appearing in the clone'shooks/directory and running on the next relevant Git operation. - Other Git subcommands with fallback paths. Look for features that invoke
git apply,git amor merge machinery with flags intended to keep writes off disk. A positive result is a file landing on the real filesystem despite a--cached-style flag. - Non-bare temporary clones. Check whether a feature's temporary repository has a working tree and where a checked-out file would land relative to
$GIT_DIR. A positive result is a written file inside a directory Git executes from. - Executable bit preservation. Confirm whether the patch header's
new file mode 100755survives the fallback path. A positive result is a hook that runs without a separatechmod. - Response indistinguishability. Compare the API response for a successful hook execution with an ordinary patch application. A positive result is identical output, meaning the attack leaves no HTTP-layer signal.
- Exfiltration without outbound traffic. Have the hook store command output as a Git object or branch and retrieve it with an authenticated
fetch. A positive result is data coming back over the normal Git protocol with no server-side outbound connection.
What happened, in plain words
Gitea is a piece of software you can install on a server to host Git repositories, a bit like running your own small version of GitHub. It has a feature called diffpatch that lets a user send a "patch" (a text file describing changes to files) and have Gitea apply it to a temporary copy of a repository.
A security researcher found that if you send the exact same patch twice, Gitea's temporary copy of the repository ends up writing a file to a place where Git (the version control tool underneath Gitea) automatically runs programs. The file the attacker writes is placed where Git looks for hooks, and Git runs it as the user account that Gitea runs under. That means the attacker can run any command on the server. This is called remote code execution, or RCE.
The only thing an attacker needs is the ability to write to one repository. On many self-hosted Gitea servers, anyone can sign up for an account, so "write access to one repository" is something a stranger can give themselves in a couple of minutes. That makes this effectively an attack that needs no stolen password at all.


The pieces you need to know first
Before walking through the attack, here are the basic ideas it relies on. None of them is exotic; each one exists for a good reason.
Git and repositories. Git is the tool programmers use to track changes to files. A repository (repo) is a folder that Git watches, plus a hidden folder called .git where Git stores its own bookkeeping: the history of changes, the list of files, and so on. When you "commit", you save a snapshot of your files into that history.
Bare repositories. Normally a repository has two parts: the files you actually edit (the working tree) and the hidden .git folder with the history. A bare repository has no working tree at all. It is just the bookkeeping. Servers use bare repositories because nobody is editing files directly on the server; people push changes to it from elsewhere. In a bare repository, the contents of what would normally be .git sit directly at the top level of the folder: hooks/, objects/, refs/ and so on. There is no .git subfolder to separate them from anything else.
Hooks. A hook is a script that Git runs automatically at certain moments. Think of it like the automatic reply you set on an email account: you do not have to be there, the system triggers it for you. Hooks live in a folder called hooks/ inside the repository. If a file with the right name exists there and is executable (meaning the operating system is allowed to run it as a program), Git runs it. One such hook is called post-index-change, and Git runs it every time the index changes.
The index. The index is Git's staging area: a list of what the next commit will contain. When Git updates that list, the post-index-change hook fires. No human clicks anything; it is automatic.
Patches and git apply. A patch is a plain text description of changes: "add this file with this content", "change line 5 of that file". The command git apply reads a patch and applies it. It has a flag --cached which means "only update the index, do not touch the files on disk". That sounds safe, and it is meant to be.
Three-way merge and the -3 flag. Sometimes a patch does not fit perfectly, for example because the file it refers to has changed since the patch was written. Git has a fallback: it can try a three-way merge, which compares three versions (yours, theirs, and their common ancestor) and works out how to combine them. The flag -3 turns this fallback on. It was added in Git version 2.32 as a usability improvement. To resolve conflicts, the merge process needs to write the result out to real files on disk.
API. An API (application programming interface) is a way for one program to talk to another over the network, usually by sending requests to URLs. When you use a website, your browser is talking to the server; when a program uses an API, it sends structured requests (often in JSON, a text format for data) and gets structured answers. Gitea's diffpatch feature is reachable through such an API.
Authentication, accounts and tokens. Authentication is how a server knows who you are. You log in with a username and password, and the server gives your browser (or your program) something like a session cookie or a token: a short string that proves you already logged in, so you do not have to send your password with every request. A cookie is a small piece of data the browser stores and sends back automatically; a token is similar but usually sent by a program in a header. If a server allows public registration, anyone can create an account without an invitation, which is common on self-hosted instances.
WAF and IDS. A WAF (web application firewall) inspects incoming web traffic and blocks requests that look malicious. An IDS (intrusion detection system) watches network traffic for signs of attacks. Both work by pattern matching, so an attack that looks like ordinary, legitimate traffic can slip past them.
CVE and KEV. A CVE is a public identifier for a known security flaw, so people can refer to it unambiguously. KEV is the US Cybersecurity and Infrastructure Security Agency's catalogue of vulnerabilities that are known to be exploited in the wild. Being on that list means real attackers have used it, not just researchers.
1. Three ordinary behaviours, one dangerous combination
The researcher's key observation is that this is not one broken piece of code. It is three behaviours that each make sense on their own, but which combine badly.
- Bare clones. The temporary clone that
diffpatchcreates to apply the patch is bare. Because a bare repository has no working tree, the repository root is the Git directory:hooks/,objects/andrefs/all sit directly at the root. There is no separation between "the files being edited" and "Git's internal machinery". git apply's 3-way fallback. Since Git 2.32,git applycan retry with-3when a plain apply fails. That fallback is designed to check out the merge result into the working tree so it can resolve conflicts. In other words, it writes files to disk on purpose.- Automatic hook execution.
post-index-changeis a hook Git invokes on its own, with no human interaction, any time the index changes. If an executable file exists at that path, Git just runs it.
Gitea's implementation did not account for what happens when the first and second collide. The --cached flag promises "nothing touches disk", but the -3 fallback does not keep that promise: when it resolves a conflict, it writes the merged file to the real filesystem. And in a bare clone, the place it writes to can be the hooks directory.
2. The vulnerable code
The relevant logic lives in services/repository/files/patch.go. Here is the code as the write-up shows it:
cmdApply := gitcmd.NewCommand("apply", "--index", "--recount", "--cached", "--binary")if git.DefaultFeatures().CheckVersionAtLeast("2.32") { cmdApply.AddArguments("-3")}
Line by line, in plain words:
cmdApply := gitcmd.NewCommand(...)builds the command Gitea will run. It is preparing to callgit applywith a set of options."apply"is the Git subcommand: the one that applies patches."--cached"is the safety flag: update the index only, never touch the working directory. This is what makes the whole operation look safe.if git.DefaultFeatures().CheckVersionAtLeast("2.32")checks whether the installed Git is at least version 2.32, because the next option only exists from that version onwards.cmdApply.AddArguments("-3")adds the three-way merge fallback. On its own this is a legitimate usability improvement: it lets patches apply even when the context has shifted slightly.
The problem is the combination: when the -3 fallback actually triggers, it does not honour the "nothing touches disk" guarantee that --cached is supposed to provide. The 3-way merge path resolves conflicts by checking the merged blob out to the real filesystem.
3. Full attack flow
The trick to reliably trigger that fallback is embarrassingly simple: send the same patch twice.

- The attacker registers an account. No prior credentials are needed if public sign-up is on.
- The attacker creates a repository with
auto_init. Write access to that one repository is the only precondition. - The attacker sends a
diffpatchrequest that adds a new file athooks/post-index-change. The patch applies cleanly and lands in the index; thanks to--cached, nothing hits disk yet. - The attacker resends the exact same patch. Now a file is being "added" at a path that has already been added, which is an add/add conflict. On that conflict the
-3fallback kicks in and checks the merge result out to real disk. - Because the clone is bare, the file that just landed at
hooks/post-index-changeis sitting inside the actual Git hooks directory. If the patch's diff header specifiesnew file mode 100755, the file even keeps its executable bit, so Git is allowed to run it. - The next time
git apply --indextouches the index in that same clone, Git follows its normal hook-invocation path and runspost-index-changeas a child process of the Gitea service, with the Gitea service account's privileges. That is arbitrary command execution. - If the hook script stores command output as a Git object or a branch, the attacker retrieves it with a plain, authenticated
fetch, with no outbound connection from the server required.
Why each step is needed: step 1 gets you an identity on the server; step 2 gives you a repository you are allowed to write to; step 3 plants the file in the index without touching disk; step 4 is the trigger that forces the fallback to write it to disk; step 5 is where the file lands in the hooks directory; step 6 is the moment Git runs it; step 7 is how the attacker gets results back without the server making any suspicious outbound connection.
One more detail worth calling out: the hook's exit code is never surfaced in the diffpatch API response. A successful attack returns the same response as an ordinary, successful patch application, so there is no obvious signal in the HTTP layer that anything happened.
Every one of these steps rides on standard Git protocol and the standard Gitea REST API. From the outside it just looks like an API client uploaded the same patch twice, which is exactly why the write-up describes this as hard to catch with a WAF or network IDS.
4. Impact
Anything the Gitea process can reach is in scope: app.ini secrets (Gitea's configuration file, which holds passwords and keys), process environment variables, every mounted repository, the database connection and its contents, OAuth and integration credentials, and any internal services reachable from that host.
On instances with public registration enabled, this is effectively a pre-authentication RCE: no stolen credentials are required. The CVE has been added to CISA's Known Exploited Vulnerabilities (KEV) catalogue, with observed exploitation delivering cryptominer payloads, meaning attackers have used it to install software that mines cryptocurrency on the victim's server.
5. Mitigation
- Upgrade to Gitea 1.27.1 or later immediately. The fix shipped quietly, described in the release notes as "refactor: git patch apply" rather than flagged as a security fix. Check the version number itself, not just the changelog wording.
- Disable public registration (
DISABLE_REGISTRATION = trueunder[service]inapp.ini) to remove the unauthenticated attack path entirely. This setting tells Gitea not to let strangers create accounts. - Run the Gitea process under a least-privilege dedicated account so a successful hook execution has a limited blast radius. In plain terms, do not run it as an administrator; give it only the permissions it actually needs.
- Watch for repeated calls to
diffpatchagainst the same repository with the same patch body in a short window. That is the fingerprint of this attack. - Periodically audit repositories'
hooks/directories for recently created or modified executables (post-receive,pre-receive,update,post-index-change, and so on).
6. Wrap-up
What stands out about this CVE is that every individual piece was added for a good reason. --cached was a deliberate safety flag. The -3 fallback was a usability improvement. Automatic hook execution is core Git design, not a bug. It is only when all three meet inside a temporary bare clone that the safety guarantee --cached was supposed to provide quietly breaks.
The general lesson is that any feature which manipulates a temporary Git repository from untrusted input needs to account for the repository's shape (bare versus non-bare) and for the less-common fallback paths of the Git subcommands it invokes, not just their documented happy-path behaviour.
Glossary
- API: a way for one program to talk to another over the network, usually by sending requests to URLs and getting structured answers back.
- Authentication: the process of proving who you are to a server, usually with a username and password.
- Bare repository: a Git repository with no working tree, containing only Git's internal bookkeeping (
hooks/,objects/,refs/) at the top level. - Blast radius: how much damage an attacker can do once they get in.
- Branch: a named line of development in Git, essentially a movable pointer to a commit.
- CISA: the US Cybersecurity and Infrastructure Security Agency.
- Clone: a copy of a repository.
- Commit: a saved snapshot of a repository's files at a point in time.
- Cookie: a small piece of data a browser stores for a website and sends back automatically with each request, often used to remember that you are logged in.
- CVE: a public identifier for a known security vulnerability.
- diffpatch: a Gitea API feature that applies a user-supplied patch to a temporary internal repository.
- Executable bit: a permission flag on a file that tells the operating system the file may be run as a program.
- Exit code: the number a program returns when it finishes, indicating success or failure.
- Exfiltration: getting stolen data out of a compromised system.
- Fetch: a Git command that downloads objects and branches from another repository.
- Git: the version control tool programmers use to track changes to files.
git apply: the Git command that reads a patch and applies it to a repository.- Hook: a script Git runs automatically at certain moments, such as when the index changes.
- IDS: an intrusion detection system, which watches network traffic for signs of attacks.
- Index: Git's staging area, the list of what the next commit will contain.
- JSON: a text format for structured data, commonly used in API requests and responses.
- KEV: CISA's Known Exploited Vulnerabilities catalogue, listing vulnerabilities that are known to be exploited in the wild.
- Least privilege: the principle of giving a program or user only the permissions it actually needs.
- OAuth: a standard way for applications to get limited access to an account without sharing the password.
- Patch: a plain text description of changes to files, which a tool like
git applycan apply. - Pre-authentication (pre-auth): an attack that works before the attacker has logged in, without any valid credentials.
- Public registration: a server setting that lets anyone create an account without an invitation.
- RCE (remote code execution): a vulnerability that lets an attacker run their own commands on a remote server.
- Repository (repo): a folder tracked by Git, containing files plus Git's internal history and bookkeeping.
- REST API: an API style that uses standard HTTP requests (GET, POST and so on) to URLs to perform operations.
- Service account: the user account under which a server program runs.
- Three-way merge: a way of combining two versions of a file by comparing both against their common ancestor, used to resolve conflicts.
- Token: a short string that proves you are authenticated, usually sent by a program with each request.
- WAF: a web application firewall, which inspects incoming web traffic and blocks requests that look malicious.
- Working tree: the actual files you edit in a repository, as opposed to Git's internal bookkeeping.
--cached: agit applyflag meaning "update the index only, never touch the working directory".-3: agit applyflag that enables the three-way merge fallback when a patch does not apply cleanly.auto_init: a Gitea option that creates a repository with an initial commit so it is not empty.post-index-change: a Git hook that runs automatically whenever the index changes.new file mode 100755: a line in a patch header that says the new file should be created with the executable bit set.app.ini: Gitea's configuration file, which holds settings and secrets.DISABLE_REGISTRATION: a Gitea setting that, when set totrue, stops strangers from creating accounts.