CVE-2022-39207: Incorrect Permission Assignment for Critical Resource (CWE-732)
value: Onedev is an open source, self-hosted Git Server with CI/CD and Kanban. During CI/CD builds, it is possible to save build artifacts for later retrieval. They can be accessed through OneDev's web UI after the successful run of a build. These artifact files are served by the webserver in the same context as the UI without any further restrictions. This leads to Cross-Site Scripting (XSS) when a user creates a build artifact that contains HTML. When accessing the artifact, the content is rendered by the browser, including any JavaScript that it contains. Since all cookies (except for the rememberMe one) do not set the HttpOnly flag, an attacker could steal the session of a victim and use it to impersonate them. To exploit this issue, attackers need to be able to modify the content of artifacts, which usually means they need to be able to modify a project's build spec. The exploitation requires the victim to click on an attacker's link. It can be used to elevate privileges by targeting admins of a OneDev instance. In the worst case, this can lead to arbitrary code execution on the server, because admins can create Server Shell Executors and use them to run any command on the server. This issue has been patched in version 7.3.0. Users are advised to upgrade. There are no known workarounds for this issue.
CWE description: The product specifies permissions for a security-critical resource in a way that allows that resource to be read or modified by unintended actors.
CVSS v3 base score: 5.4
Published: 2022-09-13T19:15Z
Repository: https://github.com/theonedev/onedev
Language: Java
Files changed: server-core/src/main/java/io/onedev/server/web/resource/ArtifactResource.java
Commit date: 2022-05-31 11:27:47 +0800
Commit message: Fix XSS attach for published artifacts
Diff: diff --git a/server-core/src/main/java/io/onedev/server/web/resource/ArtifactResource.java b/server-core/src/main/java/io/onedev/server/web/resource/ArtifactResource.java
index 92f3af34439..449ac510855 100644
--- a/server-core/src/main/java/io/onedev/server/web/resource/ArtifactResource.java
+++ b/server-core/src/main/java/io/onedev/server/web/resource/ArtifactResource.java
@@ -1,8 +1,7 @@
 package io.onedev.server.web.resource;
 
-import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
@@ -14,10 +13,11 @@ import java.util.concurrent.Callable;
 
 import javax.persistence.EntityNotFoundException;
 
 import org.apache.shiro.authz.UnauthorizedException;
 import org.apache.tika.io.IOUtils;
+import org.apache.tika.mime.MimeTypes;
 import org.apache.wicket.request.mapper.parameter.PageParameters;
 import org.apache.wicket.request.resource.AbstractResource;
 
 import com.google.common.base.Joiner;
 import com.google.common.base.Splitter;
@@ -28,11 +28,10 @@ import io.onedev.server.OneDev;
 import io.onedev.server.entitymanager.BuildManager;
 import io.onedev.server.entitymanager.ProjectManager;
 import io.onedev.server.model.Build;
 import io.onedev.server.model.Project;
 import io.onedev.server.security.SecurityUtils;
-import io.onedev.server.util.ContentDetector;
 
 public class ArtifactResource extends AbstractResource {
 
 	private static final long serialVersionUID = 1L;
 
@@ -83,24 +82,19 @@ public class ArtifactResource extends AbstractResource {
 					project.getPath(), build.getNumber(), artifactPath);
 			throw new ExplicitException(message);
 		}
 			
 		ResourceResponse response = new ResourceResponse();
-		try (InputStream is = new BufferedInputStream(new FileInputStream(artifactFile))) {
-			response.setContentType(ContentDetector.detectMediaType(is, artifactPath).toString());
-		} catch (Exception e) {
-			throw new RuntimeException(e);
-		}
-		
+		response.getHeaders().addHeader("X-Content-Type-Options", "nosniff");
+		response.setContentType(MimeTypes.OCTET_STREAM);
 		response.disableCaching();
 		
 		try {
 			response.setFileName(URLEncoder.encode(artifactFile.getName(), StandardCharsets.UTF_8.name()));
 		} catch (UnsupportedEncodingException e) {
 			throw new RuntimeException(e);
 		}
-		
 		response.setContentLength(artifactFile.length());
 		
 		response.setWriteCallback(new WriteCallback() {
 
 			@Override

Vulnerable code: import java.io.BufferedInputStream;
import io.onedev.server.util.ContentDetector;
		try (InputStream is = new BufferedInputStream(new FileInputStream(artifactFile))) {
			response.setContentType(ContentDetector.detectMediaType(is, artifactPath).toString());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		
Fixed code: import org.apache.tika.mime.MimeTypes;
		response.getHeaders().addHeader("X-Content-Type-Options", "nosniff");
		response.setContentType(MimeTypes.OCTET_STREAM);