CVE-2022-39205 (Java, published 2022-09-13T19:15Z)
Description: value: Onedev is an open source, self-hosted Git Server with CI/CD and Kanban. In versions of Onedev prior to 7.3.0 unauthenticated users can take over a OneDev instance if there is no properly configured reverse proxy. The /git-prereceive-callback endpoint is used by the pre-receive git hook on the server to check for branch protections during a push event. It is only intended to be accessed from localhost, but the check relies on the X-Forwarded-For header. Invoking this endpoint leads to the execution of one of various git commands. The environment variables of this command execution can be controlled via query parameters. This allows attackers to write to arbitrary files, which can in turn lead to the execution of arbitrary code. Such an attack would be very hard to detect, which increases the potential impact even more. Users are advised to upgrade. There are no known workarounds for this issue.
Severity: nan (CVSS v3 base score: 9.8)
Weakness: CWE-287 \u2014 Improper Authentication
When an actor claims to have a given identity, the product does not prove or insufficiently proves that the claim is correct.
Repository: https://github.com/theonedev/onedev
Files changed:
server-core/src/main/java/io/onedev/server/entitymanager/impl/DefaultProjectManager.java
server-core/src/main/java/io/onedev/server/git/GitFilter.java
server-core/src/main/java/io/onedev/server/git/GitSshCommand.java
server-core/src/main/java/io/onedev/server/git/GitUtils.java
server-core/src/main/java/io/onedev/server/git/hookcallback/GitPostReceiveCallback.java
server-core/src/main/java/io/onedev/server/git/hookcallback/GitPreReceiveCallback.java
server-core/src/main/resources/git-receive-hook
Commit message: Fix security vulnerability related to Git pre/post receive callback

Vulnerable code:
			if (!content.contains("ENV_GIT_ALTERNATE_OBJECT_DIRECTORIES"))
import java.net.InetAddress;
        String clientIp = request.getHeader("X-Forwarded-For");
        if (clientIp == null) clientIp = request.getRemoteAddr();

        if (!InetAddress.getByName(clientIp).isLoopbackAddress()) {
                    "Git hook callbacks can only be accessed from localhost.");

        List<String> fields = StringUtils.splitAndTrim(request.getPathInfo(), "/");
        Preconditions.checkState(fields.size() == 2);
import java.net.InetAddress;
        String clientIp = request.getHeader("X-Forwarded-For");
        if (clientIp == null) clientIp = request.getRemoteAddr();

        if (!InetAddress.getByName(clientIp).isLoopbackAddress()) {
                    "Git hook callbacks can only be accessed from localhost.");
        List<String> fields = StringUtils.splitAndTrim(request.getPathInfo(), "/");
        Preconditions.checkState(fields.size() == 2);
        
lines=($(${ONEDEV_CURL} -k -s -S -f -X POST --data-urlencode "ENV_GIT_ALTERNATE_OBJECT_DIRECTORIES=${GIT_ALTERNATE_OBJECT_DIRECTORIES}" --data-urlencode "ENV_GIT_OBJECT_DIRECTORY=${GIT_OBJECT_DIRECTORY}" --data-urlencode "ENV_GIT_QUARANTINE_PATH=${GIT_QUARANTINE_PATH}" -d @- ${ONEDEV_URL}/%s/${ONEDEV_REPOSITORY_ID}/${ONEDEV_USER_ID} 2>&1))

Fixed code:
			if (!content.contains("ONEDEV_HOOK_TOKEN"))
			environments.put("ONEDEV_HOOK_TOKEN", GitUtils.HOOK_TOKEN);
		environments.put("ONEDEV_HOOK_TOKEN", GitUtils.HOOK_TOKEN);
import org.apache.commons.lang3.RandomStringUtils;
	public static final String HOOK_TOKEN = RandomStringUtils.randomAlphanumeric(20); 
	
        List<String> fields = StringUtils.splitAndTrim(request.getPathInfo(), "/");
        Preconditions.checkState(fields.size() == 3);
        
        if (!fields.get(2).equals(GitUtils.HOOK_TOKEN)) {
                    "Git hook callbacks can only be accessed by OneDev itself");
        List<String> fields = StringUtils.splitAndTrim(request.getPathInfo(), "/");
        Preconditions.checkState(fields.size() == 3);
        
        if (!fields.get(2).equals(GitUtils.HOOK_TOKEN)) {
                    "Git hook callbacks can only be accessed by OneDev itself");
lines=($(${ONEDEV_CURL} -k -s -S -f -X POST --data-urlencode "ENV_GIT_ALTERNATE_OBJECT_DIRECTORIES=${GIT_ALTERNATE_OBJECT_DIRECTORIES}" --data-urlencode "ENV_GIT_OBJECT_DIRECTORY=${GIT_OBJECT_DIRECTORY}" --data-urlencode "ENV_GIT_QUARANTINE_PATH=${GIT_QUARANTINE_PATH}" -d @- ${ONEDEV_URL}/%s/${ONEDEV_REPOSITORY_ID}/${ONEDEV_USER_ID}/${ONEDEV_HOOK_TOKEN} 2>&1))