Archived
fix: harden machine federation boundaries
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { sanitizedGitEnv } from "./gitEnv.js";
|
||||
|
||||
describe("sanitizedGitEnv", () => {
|
||||
it("removes repository-local Git variables inherited from hooks", () => {
|
||||
const env = sanitizedGitEnv({
|
||||
PATH: "/bin",
|
||||
GIT_DIR: "/repo/.git",
|
||||
GIT_WORK_TREE: "/repo",
|
||||
GIT_INDEX_FILE: "/repo/.git/index.lock",
|
||||
GIT_PREFIX: "src/",
|
||||
GIT_COMMON_DIR: "/repo/.git",
|
||||
});
|
||||
|
||||
expect(env).toEqual({ PATH: "/bin" });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
const GIT_LOCAL_ENV_VARS = [
|
||||
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
|
||||
"GIT_COMMON_DIR",
|
||||
"GIT_DIR",
|
||||
"GIT_INDEX_FILE",
|
||||
"GIT_OBJECT_DIRECTORY",
|
||||
"GIT_PREFIX",
|
||||
"GIT_QUARANTINE_PATH",
|
||||
"GIT_WORK_TREE",
|
||||
];
|
||||
|
||||
export function sanitizedGitEnv(env: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {
|
||||
const blocked = new Set<string>(GIT_LOCAL_ENV_VARS);
|
||||
return Object.fromEntries(Object.entries(env).filter(([key]) => !blocked.has(key)));
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
|
||||
import { spawn } from "node:child_process";
|
||||
import type { GitDiffResponse, GitFileState, GitStatusFile, GitStatusResponse } from "../../shared/apiTypes.js";
|
||||
import { normalizeRelativePath } from "../workspaces/pathSafety.js";
|
||||
import { sanitizedGitEnv } from "./gitEnv.js";
|
||||
|
||||
const MAX_OUTPUT = 2 * 1024 * 1024;
|
||||
|
||||
@@ -95,7 +96,7 @@ function hash(value: string): string {
|
||||
|
||||
async function runGit(cwd: string, args: string[]): Promise<{ code: number; stdout: string; stderr: string; truncated: boolean }> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn("git", args, { cwd, stdio: ["ignore", "pipe", "pipe"] });
|
||||
const child = spawn("git", args, { cwd, env: sanitizedGitEnv(), stdio: ["ignore", "pipe", "pipe"] });
|
||||
const timer = setTimeout(() => { child.kill("SIGKILL"); }, 10000);
|
||||
let stdout = Buffer.alloc(0);
|
||||
let stderr = Buffer.alloc(0);
|
||||
|
||||
Reference in New Issue
Block a user