Summary
A command injection flaw in Gogs allows an authenticated user to craft a malicious branch name that injects the --exec flag into an internal git rebase command during "Rebase before merging".
When this merge strategy is used, the attack can lead to remote code execution on the Gogs server, making any instance exposed to untrusted repository collaborators particularly risky.
Affected context
The vulnerability appears in Gogs instances that:
- Allow authenticated users to create branches and pull requests
- Offer the "Rebase before merging" option in the pull request workflow
- Construct and execute
git rebasecommands using unvalidated branch names or user-controlled parameters
It primarily impacts environments where users with repository access are not fully trusted and where the Gogs server process has significant privileges on the host.
Exploit preconditions
- Attack surface: Web application interface for pull requests and merge operations
- Required access: Authenticated user with permission to push branches and open pull requests on a target repository
- Trigger point: Performing a "Rebase before merging" operation on a pull request with a specially crafted branch name
- Why exploitation is possible: User-controlled branch names are embedded into the
git rebasecommand line without sufficient sanitization or escaping, allowing injection of additional flags (such as--exec) and shell-sensitive sequences.
Why this happens
The underlying issue is a classic command injection pattern where application logic composes shell or command-line invocations using untrusted user input.
In this case, Gogs builds a git rebase command that incorporates a branch name directly; because the branch name is not normalized or escaped, an attacker can embed special options (--exec) or other constructs that alter the behavior of the invoked tool.
This maps to CWE-77: Improper Neutralization of Special Elements used in a Command, as special characters and arguments in the branch name are not safely handled before being passed to git, enabling arbitrary command execution in the context of the server.
Potential impact
- Remote execution of arbitrary commands with the privileges of the Gogs server process
- Compromise of the host running Gogs, including data theft or destruction
- Tampering with Git repositories and CI/CD pipelines connected to Gogs
- Establishment of persistent backdoors or lateral movement within the infrastructure
- Abuse of server resources for further attacks or cryptomining
General mitigation guidance
Effective mitigation focuses on removing direct command-line composition with untrusted inputs and tightening process privileges.
Common defensive patterns include:
- Constructing
gitoperations using safe APIs or argument vectors rather than concatenated command strings - Rigorously validating and normalizing branch names and other user-controlled repository identifiers to allowed character sets and formats
- Escaping or rejecting any data that could introduce flags, separators, or shell metacharacters into command invocations
- Running Gogs with least-privilege accounts and restricted filesystem access to reduce blast radius if a flaw is exploited
- Applying defense-in-depth controls such as process-level restrictions (e.g., seccomp, AppArmor) and isolating the Git service from sensitive infrastructure components
How teams detect issues like this
Organizations can detect similar vulnerabilities by:
- Using static analysis or linters to flag code paths that build shell commands via string concatenation or interpolation with user input
- Reviewing integrations with external tools (like
git) to confirm that arguments are passed as discrete parameters, not through raw shell command lines - Establishing secure coding guidelines that prohibit unsanitized user input in command execution utilities and enforcing them via pull request templates or automated checks
- Logging and periodically analyzing command invocation patterns and merge operations for anomalous flags or unexpected parameters
- Employing security scanners focused on web application command injection and unsafe process execution constructs in server-side code

