Skip to content

The Unified and Autonomous Development Platform

fix: Arbitrary file read via crafted LFS oid (OD-2749)

Robin Shen244b72467594
diff --git a/server-core/src/main/java/io/onedev/server/git/Blob.java b/server-core/src/main/java/io/onedev/server/git/Blob.java
index 6f385984c8..6256ed3a96 100644
--- a/server-core/src/main/java/io/onedev/server/git/Blob.java
+++ b/server-core/src/main/java/io/onedev/server/git/Blob.java
@@ -138,11 +138,19 @@ public class Blob implements Serializable {
if (lfsPointer == null) {
Text text = getText();
if (text != null) {
- if (!text.getLines().isEmpty()
+ if (text.getLines().size() >= 3
&& text.getLines().get(0).startsWith("version https://git-lfs.github.com/spec/")) {
String objectId = StringUtils.substringAfter(text.getLines().get(1), ":");
- long objectSize = Long.parseLong(StringUtils.substringAfter(text.getLines().get(2), " "));
- lfsPointer = Optional.of(new LfsPointer(objectId, objectSize));
+ if (LfsObject.isValidObjectId(objectId)) {
+ try {
+ long objectSize = Long.parseLong(StringUtils.substringAfter(text.getLines().get(2), " "));
+ lfsPointer = Optional.of(new LfsPointer(objectId, objectSize));
+ } catch (NumberFormatException e) {
+ lfsPointer = Optional.absent();
+ }
+ } else {
+ lfsPointer = Optional.absent();
+ }
} else {
lfsPointer = Optional.absent();
}
diff --git a/server-core/src/main/java/io/onedev/server/git/LfsObject.java b/server-core/src/main/java/io/onedev/server/git/LfsObject.java
index 10fb0ccd5d..1cb4692120 100644
--- a/server-core/src/main/java/io/onedev/server/git/LfsObject.java
+++ b/server-core/src/main/java/io/onedev/server/git/LfsObject.java
@@ -1,34 +1,60 @@
package io.onedev.server.git;
-import java.io.*;
+import static io.onedev.server.util.IOUtils.BUFFER_SIZE;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FilterInputStream;
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
+import java.util.regex.Pattern;
import org.apache.tika.mime.MediaType;
+import org.jetbrains.annotations.NotNull;
+import org.jspecify.annotations.Nullable;
+import io.onedev.commons.utils.ExplicitException;
import io.onedev.commons.utils.FileUtils;
import io.onedev.commons.utils.LockUtils;
import io.onedev.server.OneDev;
import io.onedev.server.cluster.ClusterTask;
import io.onedev.server.service.ProjectService;
import io.onedev.server.util.ContentDetector;
-import org.jetbrains.annotations.NotNull;
-
-import static io.onedev.server.util.IOUtils.BUFFER_SIZE;
public class LfsObject implements Serializable {
private static final long serialVersionUID = 1L;
+ /**
+ * A Git LFS oid is a lowercase hex SHA-256 digest (64 characters). Anything
+ * else is invalid and must never be used as a filesystem path component, as
+ * the on-disk layout below derives the storage path directly from it.
+ */
+ private static final Pattern OBJECT_ID_PATTERN = Pattern.compile("[0-9a-f]{64}");
+
private final Long projectId;
private final String objectId;
public LfsObject(Long projectId, String objectId) {
+ if (!isValidObjectId(objectId))
+ throw new ExplicitException("Invalid LFS object id");
this.projectId = projectId;
this.objectId = objectId;
}
+ public static boolean isValidObjectId(@Nullable String objectId) {
+ return objectId != null && OBJECT_ID_PATTERN.matcher(objectId).matches();
+ }
+
public Long getProjectId() {
return projectId;
}