forked from EduCraft/curriculum-project-hub
fix: restore bounded agent sandbox execution
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { mkdir, rm } from "node:fs/promises";
|
||||
import { dirname, relative, resolve } from "node:path";
|
||||
import type { Folder, OrganizationMemberRole, PermissionRole, Prisma, PrismaClient } from "@prisma/client";
|
||||
@@ -595,7 +595,11 @@ function projectWorkspaceDir(input: {
|
||||
readonly projectId: string;
|
||||
}): string {
|
||||
const root = resolve(requireNonEmpty(input.workspaceRoot, "workspace root"));
|
||||
const dir = resolve(root, safePathSegment(input.organizationSlug, "organization slug"), safePathSegment(input.projectId, "project id"));
|
||||
const dir = resolve(
|
||||
root,
|
||||
compactWorkspaceSegment("o", input.organizationSlug, "organization slug"),
|
||||
compactWorkspaceSegment("p", input.projectId, "project id"),
|
||||
);
|
||||
const rel = relative(root, dir);
|
||||
if (rel === "" || rel.startsWith("..")) {
|
||||
throw new Error(`allocated workspace escapes root: ${dir}`);
|
||||
@@ -603,6 +607,12 @@ function projectWorkspaceDir(input: {
|
||||
return dir;
|
||||
}
|
||||
|
||||
function compactWorkspaceSegment(prefix: "o" | "p", value: string, label: string): string {
|
||||
const normalized = safePathSegment(value, label);
|
||||
const digest = createHash("sha256").update(normalized).digest("base64url").slice(0, 16);
|
||||
return `${prefix}_${digest}`;
|
||||
}
|
||||
|
||||
function safePathSegment(value: string, label: string): string {
|
||||
const segment = requireNonEmpty(value, label).replace(/[^A-Za-z0-9._-]+/g, "_");
|
||||
if (segment === "." || segment === ".." || segment === "") {
|
||||
|
||||
Reference in New Issue
Block a user