The Unified and Autonomous Development Platform
feat: Validate workspace data keys and enhance user data handling
Robin Shen43fc209e68cc
diff --git a/server-core/src/main/java/io/onedev/server/model/User.java b/server-core/src/main/java/io/onedev/server/model/User.javaindex cd92d194af..4ae3de6d63 100644--- a/server-core/src/main/java/io/onedev/server/model/User.java+++ b/server-core/src/main/java/io/onedev/server/model/User.java@@ -1347,6 +1347,8 @@ public class User extends AbstractEntity implements AuthenticationInfo {}public static String encodeWorkspaceDataKey(String dataKey) {+ if (dataKey.equals(".") || dataKey.equals(".."))+ throw new ExplicitException("Invalid workspace data key: " + dataKey);try {return URLEncoder.encode(dataKey, StandardCharsets.UTF_8.name());} catch (UnsupportedEncodingException e) {diff --git a/server-core/src/main/java/io/onedev/server/rest/resource/WorkerResource.java b/server-core/src/main/java/io/onedev/server/rest/resource/WorkerResource.javaindex d02038114e..3586d2956b 100644--- a/server-core/src/main/java/io/onedev/server/rest/resource/WorkerResource.java+++ b/server-core/src/main/java/io/onedev/server/rest/resource/WorkerResource.java@@ -18,6 +18,7 @@ import java.util.Map;import jakarta.inject.Inject;import jakarta.inject.Singleton;+import jakarta.validation.constraints.NotEmpty;import jakarta.ws.rs.Consumes;import jakarta.ws.rs.GET;import jakarta.ws.rs.HEAD;@@ -94,8 +95,7 @@ public class WorkerResource {@Path("job-data")@Produces(MediaType.APPLICATION_OCTET_STREAM)@GET- public byte[] getJobData(@QueryParam("token") String token,- @QueryParam("workDir") @Nullable String workDir) {+ public byte[] getJobData(@QueryParam("token") String token, @QueryParam("workDir") String workDir) {JobContext jobContext = jobService.getJobContext(token, true);if (StringUtils.isNotBlank(workDir))jobService.reportJobWorkDir(jobContext, workDir);@@ -110,7 +110,7 @@ public class WorkerResource {@Path("run-server-step")@Consumes(MediaType.APPLICATION_OCTET_STREAM)@POST- public StreamingOutput runServerStep(@QueryParam("token") String token, InputStream is) {+ public StreamingOutput runServerStep(@QueryParam("token") @NotEmpty String token, InputStream is) {JobContext jobContext = jobService.getJobContext(token, true);return os -> {File filesDir = FileUtils.createTempDir();@@ -161,7 +161,7 @@ public class WorkerResource {@Path("dependencies")@Produces(MediaType.APPLICATION_OCTET_STREAM)@GET- public StreamingOutput downloadDependencies(@QueryParam("token") String token) {+ public StreamingOutput downloadDependencies(@QueryParam("token") @NotEmpty String token) {return os -> {JobContext jobContext = jobService.getJobContext(token, true);File tempDir = FileUtils.createTempDir();@@ -180,10 +180,10 @@ public class WorkerResource {@Produces(MediaType.APPLICATION_OCTET_STREAM)@GETpublic StreamingOutput downloadJobCache(- @QueryParam("token") String token,- @QueryParam("key") String key,- @QueryParam("checksum") @Nullable String checksum,- @QueryParam("path") String path) {+ @QueryParam("token") @NotEmpty String token,+ @QueryParam("key") @NotEmpty String key,+ @QueryParam("checksum") String checksum,+ @QueryParam("path") @NotEmpty String path) {var context = jobService.getJobContext(token, true);return downloadCache(context.getProjectId(), key, checksum, path);}@@ -192,10 +192,10 @@ public class WorkerResource {@Produces(MediaType.APPLICATION_OCTET_STREAM)@GETpublic StreamingOutput downloadWorkspaceCache(- @QueryParam("token") String token,- @QueryParam("key") String key,- @QueryParam("checksum") @Nullable String checksum,- @QueryParam("path") String path) {+ @QueryParam("token") @NotEmpty String token,+ @QueryParam("key") @NotEmpty String key,+ @QueryParam("checksum") String checksum,+ @QueryParam("path") @NotEmpty String path) {var context = workspaceService.getWorkspaceContext(token, true);return downloadCache(context.getProjectId(), key, checksum, path);}@@ -203,16 +203,16 @@ public class WorkerResource {@Path("job-cache")@HEADpublic void checkUploadJobCache(- @QueryParam("token") String token,- @QueryParam("projectPath") @Nullable String projectPath) {+ @QueryParam("token") @NotEmpty String token,+ @QueryParam("projectPath") String projectPath) {checkUploadCache(jobService.getJobContext(token, true), projectPath);}@Path("workspace-cache")@HEADpublic void checkUploadWorkspaceCache(- @QueryParam("token") String token,- @QueryParam("projectPath") @Nullable String projectPath) {+ @QueryParam("token") @NotEmpty String token,+ @QueryParam("projectPath") String projectPath) {checkUploadCache(workspaceService.getWorkspaceContext(token, true), projectPath);}@@ -239,11 +239,11 @@ public class WorkerResource {@Consumes(MediaType.APPLICATION_OCTET_STREAM)@POSTpublic Response uploadJobCache(- @QueryParam("token") String token,- @QueryParam("projectPath") @Nullable String projectPath,- @QueryParam("key") String key,- @QueryParam("checksum") @Nullable String checksum,- @QueryParam("path") String path,+ @QueryParam("token") @NotEmpty String token,+ @QueryParam("projectPath") String projectPath,+ @QueryParam("key") @NotEmpty String key,+ @QueryParam("checksum") String checksum,+ @QueryParam("path") @NotEmpty String path,InputStream is) {var projectId = checkUploadCache(jobService.getJobContext(token, true), projectPath);return uploadCache(projectId, key, checksum, path, is);@@ -253,11 +253,11 @@ public class WorkerResource {@Consumes(MediaType.APPLICATION_OCTET_STREAM)@POSTpublic Response uploadWorkspaceCache(- @QueryParam("token") String token,- @QueryParam("projectPath") @Nullable String projectPath,- @QueryParam("key") String key,- @QueryParam("checksum") @Nullable String checksum,- @QueryParam("path") String path,+ @QueryParam("token") @NotEmpty String token,+ @QueryParam("projectPath") String projectPath,+ @QueryParam("key") @NotEmpty String key,+ @QueryParam("checksum") String checksum,+ @QueryParam("path") @NotEmpty String path,InputStream is) {var projectId = checkUploadCache(workspaceService.getWorkspaceContext(token, true), projectPath);return uploadCache(projectId, key, checksum, path, is);@@ -275,7 +275,7 @@ public class WorkerResource {@Path("workspace-data")@Produces(MediaType.APPLICATION_OCTET_STREAM)@GET- public byte[] getWorkspaceData(@QueryParam("token") String token) {+ public byte[] getWorkspaceData(@QueryParam("token") @NotEmpty String token) {WorkspaceContext context = workspaceService.getWorkspaceContext(token, true);var data = new KubernetesWorkspaceData(context.getUserName(),@@ -296,9 +296,9 @@ public class WorkerResource {@Produces(MediaType.APPLICATION_OCTET_STREAM)@GETpublic Response downloadWorkspaceUserData(- @QueryParam("token") String token,- @QueryParam("key") String key,- @QueryParam("path") String path) {+ @QueryParam("token") @NotEmpty String token,+ @QueryParam("key") @NotEmpty String key,+ @QueryParam("path") @NotEmpty String path) {WorkspaceContext context = workspaceService.getWorkspaceContext(token, true);Long userId = context.getUserId();@@ -326,9 +326,9 @@ public class WorkerResource {@Consumes(MediaType.APPLICATION_OCTET_STREAM)@POSTpublic Response uploadWorkspaceUserData(- @QueryParam("token") String token,- @QueryParam("key") String key,- @QueryParam("path") String path,+ @QueryParam("token") @NotEmpty String token,+ @QueryParam("key") @NotEmpty String key,+ @QueryParam("path") @NotEmpty String path,InputStream is) {WorkspaceContext context = workspaceService.getWorkspaceContext(token, true);Long userId = context.getUserId();@@ -345,21 +345,21 @@ public class WorkerResource {@Path("job-running")@GET- public boolean isJobRunning(@QueryParam("token") String token) {+ public boolean isJobRunning(@QueryParam("token") @NotEmpty String token) {return jobService.getJobContext(token, false) != null;}@Path("workspace-active")@GET- public boolean isWorkspaceActive(@QueryParam("token") String token) {+ public boolean isWorkspaceActive(@QueryParam("token") @NotEmpty String token) {return workspaceService.getWorkspaceContext(token, false) != null;}@Path("workspace-user-data")@PUTpublic Response notifyWorkspaceUserDataUploaded(- @QueryParam("token") String token,- @QueryParam("key") String key) {+ @QueryParam("token") @NotEmpty String token,+ @QueryParam("key") @NotEmpty String key) {WorkspaceContext context = workspaceService.getWorkspaceContext(token, true);Long userId = context.getUserId();userService.notifyWorkspaceDataUploaded(userId, key);diff --git a/server-core/src/main/java/io/onedev/server/workspace/DefaultWorkspaceService.java b/server-core/src/main/java/io/onedev/server/workspace/DefaultWorkspaceService.javaindex 2c69cf87fb..0111bb13cb 100644--- a/server-core/src/main/java/io/onedev/server/workspace/DefaultWorkspaceService.java+++ b/server-core/src/main/java/io/onedev/server/workspace/DefaultWorkspaceService.java@@ -31,20 +31,9 @@ import java.util.concurrent.atomic.AtomicBoolean;import java.util.concurrent.atomic.AtomicReference;import java.util.stream.Collectors;-import jakarta.inject.Inject;-import jakarta.inject.Singleton;-import jakarta.persistence.criteria.CriteriaBuilder;-import jakarta.persistence.criteria.CriteriaQuery;-import jakarta.persistence.criteria.From;-import jakarta.persistence.criteria.Order;-import jakarta.persistence.criteria.Path;-import jakarta.persistence.criteria.Predicate;-import jakarta.persistence.criteria.Root;-import org.apache.shiro.subject.Subject;import org.apache.wicket.protocol.ws.api.IWebSocketConnection;import org.eclipse.jgit.lib.ObjectId;-import io.onedev.server.persistence.dao.Restrictions;import org.hibernate.query.Query;import org.jspecify.annotations.Nullable;import org.quartz.ScheduleBuilder;@@ -62,6 +51,7 @@ import io.onedev.commons.loader.ManagedSerializedForm;import io.onedev.commons.utils.ExceptionUtils;import io.onedev.commons.utils.ExplicitException;import io.onedev.commons.utils.FileUtils;+import io.onedev.commons.utils.StringUtils;import io.onedev.commons.utils.TaskLogger;import io.onedev.k8shelper.DefaultCloneInfo;import io.onedev.server.OneDev;@@ -102,6 +92,7 @@ import io.onedev.server.persistence.TransactionService;import io.onedev.server.persistence.annotation.Sessional;import io.onedev.server.persistence.annotation.Transactional;import io.onedev.server.persistence.dao.EntityCriteria;+import io.onedev.server.persistence.dao.Restrictions;import io.onedev.server.search.entity.EntityQuery;import io.onedev.server.search.entity.EntitySort;import io.onedev.server.search.entity.workspace.WorkspaceQuery;@@ -120,6 +111,15 @@ import io.onedev.server.util.criteria.Criteria;import io.onedev.server.util.interpolative.WorkspaceVariableInterpolator;import io.onedev.server.web.component.terminal.ShellExit;import io.onedev.server.web.editable.EditableUtils;+import jakarta.inject.Inject;+import jakarta.inject.Singleton;+import jakarta.persistence.criteria.CriteriaBuilder;+import jakarta.persistence.criteria.CriteriaQuery;+import jakarta.persistence.criteria.From;+import jakarta.persistence.criteria.Order;+import jakarta.persistence.criteria.Path;+import jakarta.persistence.criteria.Predicate;+import jakarta.persistence.criteria.Root;@Singletonpublic class DefaultWorkspaceService extends BaseEntityService<Workspace>@@ -788,6 +788,11 @@ public class DefaultWorkspaceService extends BaseEntityService<Workspace>var interpolator = new WorkspaceVariableInterpolator(workspace);spec = interpolator.interpolateProperties(spec);+ for (int i = 0; i < spec.getUserDatas().size(); i++) {+ var key = spec.getUserDatas().get(i).getKey();+ if (StringUtils.isBlank(key))+ throw new ExplicitException("User data #" + (i + 1) + ": data key must not be empty after interpolation");+ }String token = workspace.getToken();var project = workspace.getProject();diff --git a/server-core/src/test/java/io/onedev/server/model/WorkspaceDataKeyTest.java b/server-core/src/test/java/io/onedev/server/model/WorkspaceDataKeyTest.javanew file mode 100644index 0000000000..d7d445a2e9--- /dev/null+++ b/server-core/src/test/java/io/onedev/server/model/WorkspaceDataKeyTest.java@@ -0,0 +1,44 @@+package io.onedev.server.model;++import static org.junit.jupiter.api.Assertions.*;++import java.io.File;+import java.nio.file.Path;+import java.util.List;++import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.io.TempDir;++import io.onedev.commons.utils.ExplicitException;+import io.onedev.server.service.impl.DefaultUserService;++class WorkspaceDataKeyTest {++ @TempDir+ Path temp;++ @Test+ void rejectsKeysThatResolveToStorageRootOrParent() {+ var service = new DefaultUserService() {+ @Override+ public File getWorkspaceDataBaseDir(Long userId) {+ return temp.toFile();+ }+ };+ for (var key: List.of(".", "..")) {+ assertThrows(ExplicitException.class, () -> service.getWorkspaceDataDir(1L, key, false));+ assertThrows(ExplicitException.class, () -> service.downloadWorkspaceData(1L, key, "/data", is -> fail()));+ assertThrows(ExplicitException.class, () -> service.uploadWorkspaceData(1L, key, "/data", os -> fail()));+ }+ assertArrayEquals(new String[0], temp.toFile().list());+ }++ @Test+ void encodesOtherKeysAsSingleDirectoryNamesWithoutChangingIdentity() {+ for (var key: List.of("data", "data..backup", "../outside", "/absolute", "..\\outside", "%2e%2e", "a/b")) {+ var encoded = User.encodeWorkspaceDataKey(key);+ assertEquals(temp, temp.resolve(encoded).normalize().getParent());+ assertEquals(key, User.decodeWorkspaceDataKey(encoded));+ }+ }+}diff --git a/server-core/src/test/java/io/onedev/server/workspace/WorkspaceUserDataValidationTest.java b/server-core/src/test/java/io/onedev/server/workspace/WorkspaceUserDataValidationTest.javanew file mode 100644index 0000000000..26dd3e516e--- /dev/null+++ b/server-core/src/test/java/io/onedev/server/workspace/WorkspaceUserDataValidationTest.java@@ -0,0 +1,57 @@+package io.onedev.server.workspace;++import static org.junit.jupiter.api.Assertions.*;++import java.lang.reflect.InvocationTargetException;+import java.util.Arrays;+import java.util.List;++import org.junit.jupiter.api.Test;++import io.onedev.commons.utils.ExplicitException;+import io.onedev.server.model.Workspace;+import io.onedev.server.model.support.workspace.spec.UserData;+import io.onedev.server.model.support.workspace.spec.UserDataEntry;+import io.onedev.server.model.support.workspace.spec.WorkspaceSpec;++class WorkspaceUserDataValidationTest {++ @Test+ void rejectsEmptyResolvedKeysBeforeProvisioning() throws Exception {+ var run = DefaultWorkspaceService.class.getDeclaredMethod("run", Workspace.class);+ run.setAccessible(true);+ for (var key: Arrays.asList(null, "", "@branch@")) {+ var data = new UserData();+ data.setKey(key);+ data.setEntries(List.of(UserDataEntry.of("/home/user/data", null)));+ var spec = new WorkspaceSpec();+ spec.setName("test");+ spec.setRunInContainer(true);+ spec.setImage("image");+ spec.setUserDatas(List.of(data));+ var workspace = new Workspace() {+ @Override+ public WorkspaceSpec getSpec() {+ return spec;+ }++ @Override+ public String getBranch() {+ // A workspace without a branch resolves @branch@ to an empty string.+ return null;+ }++ @Override+ public String getToken() {+ return fail("Invalid user data must be rejected before provisioning proceeds");+ }+ };++ var exception = assertThrows(InvocationTargetException.class,+ () -> run.invoke(new DefaultWorkspaceService(), workspace));+ var cause = assertInstanceOf(ExplicitException.class, exception.getCause());+ assertEquals("User data #1: data key must not be empty after interpolation", cause.getMessage());+ assertEquals(key, data.getKey());+ }+ }+}