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
4 changes: 2 additions & 2 deletions web/app/api/vm/[id]/attach-endpoint/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function POST(
// An opted-in machine's agent-update exec runs after the response.
deferAfterResponse: (work) => runAfterResponse(() => Effect.runPromise(work)),
modelPlane: vmModelPlaneRevoker(),
// vmKind: owner for the creator's machine on a team whose only member is them.
// vmKind: owner for the creator's machine while they remain a team member.
teamDirectory: vmTeamDirectory(),
}), { request, deadline: { operation: "attach", continuationKey: `attach:${id}` } });
if (!run.ok) return run.response;
Expand All @@ -107,7 +107,7 @@ export async function POST(
sessionTitle,
options: { requireDaemon, sessionId, attachmentId },
modelPlane: vmModelPlaneRevoker(),
// vmKind: owner for the creator's machine on a team whose only member is them.
// vmKind: owner for the creator's machine while they remain a team member.
teamDirectory: vmTeamDirectory(),
}), { request, deadline: { operation: "attach", continuationKey: `attach:${id}` } });
if (!run.ok) return run.response;
Expand Down
27 changes: 11 additions & 16 deletions web/services/vms/vmKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,15 @@ export async function resolveVmGuestKind(

/**
* The kind the attach endpoint tells the signed-in caller (`vmKind`). The Mac
* app gives up the local sandbox for its own session only on `owner`, so
* `owner` means the caller created the machine and nobody else can reach it:
* app gives up the local sandbox for its own session only on `owner`: the
* caller created the machine and still belongs to its owner team, regardless
* of that team's member count. Personal-account machines need no team lookup.
*
* - a machine on the caller's own personal account (owner_team_id is the
* caller's user id); or
* - a machine on a Stack team whose member list, read for this request
* (`members`, the same bounded read resolveVmGuestKind uses), is exactly the
* caller. A second member makes it `team` on the next attach.
*
* Everything else is `team`: a machine the caller reaches but did not create,
* a team with any other member, a team that no longer exists, and a member
* list that was not read, failed or timed out (fail closed: never `owner` on
* an unknown list). A row with no usable billing owner is null. Ids compare
* exactly. Computed per request, never stored.
* A different caller, a missing team, a creator who left the team, or a member
* list that was not read, failed or timed out gets `team`. A row with no usable
* billing owner is null. Ids compare exactly. Computed per request, never stored.
* This caller-specific answer does not change teammates' existing access or
* the guest-wide kind returned by resolveVmGuestKind.
*/
export function vmKindForCaller(
row: VmGuestKindRow,
Expand All @@ -89,7 +84,7 @@ export function vmKindForCaller(
// Exact ids, no trimming: a padded id is not the caller.
if (callerUserId === "" || row.userId !== callerUserId) return "team";
if (row.ownerTeamId === callerUserId) return "owner";
return members !== undefined && onlyMemberIs(members, callerUserId) ? "owner" : "team";
return members !== undefined && callerIsMember(members, callerUserId) ? "owner" : "team";
}

/**
Expand All @@ -104,7 +99,7 @@ export function vmKindForCallerNeedsMembers(row: VmGuestKindRow, callerUserId: s
&& row.ownerTeamId !== callerUserId;
}

function onlyMemberIs(lookup: VmTeamMemberLookup, callerUserId: string): boolean {
function callerIsMember(lookup: VmTeamMemberLookup, callerUserId: string): boolean {
if ("error" in lookup || lookup.memberIds === null) return false;
return lookup.memberIds.length === 1 && lookup.memberIds[0] === callerUserId;
return lookup.memberIds.includes(callerUserId);
}
27 changes: 17 additions & 10 deletions web/tests/vm-attach-vm-kind.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// attach-endpoint's `vmKind`: the Mac app gives up its local sandbox for its
// own session only when this reads "owner", so "owner" must mean a machine the
// signed-in caller created that nobody else can reach: their personal account,
// or a Stack team whose member list, read for this attach, is only them.
// signed-in caller created: their personal account, or a Stack team whose
// member list, read for this attach, still contains them, whatever its size.
import { describe, expect, test } from "bun:test";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("attach-endpoint vmKind", () => {
expect(await websocketKind(personalVm, CREATOR, CREATOR)).toBe("owner");
});

test("a team machine is team, for its creator too", async () => {
test("a team machine with no membership answer is team, for its creator too", async () => {
expect(await cmuxRemoteKind(teamVm, CREATOR, TEAM)).toBe("team");
expect(await websocketKind(teamVm, CREATOR, TEAM)).toBe("team");
});
Expand Down Expand Up @@ -149,14 +149,19 @@ describe("attach-endpoint vmKind", () => {
expect(solo.asked).toEqual(["team-solo", "team-solo"]);
});

test("a second member makes the same machine team on the next attach", async () => {
test("the creator stays owner when their team grows to three members", async () => {
const answer: { members: readonly string[] } = { members: [CREATOR] };
const { teamDirectory } = directory(answer);
expect(await cmuxRemoteKind(teamVm, CREATOR, TEAM, teamDirectory)).toBe("owner");
answer.members = [CREATOR, MEMBER];
answer.members = [CREATOR, MEMBER, "user-third"];
expect(await cmuxRemoteKind(teamVm, CREATOR, TEAM, teamDirectory)).toBe("owner");
expect(await websocketKind(teamVm, CREATOR, TEAM, teamDirectory)).toBe("owner");
expect(await websocketKind(teamVm, CREATOR, TEAM, directory({ members: [MEMBER, CREATOR] }).teamDirectory)).toBe("owner");
expect(await cmuxRemoteKind(teamVm, MEMBER, TEAM, teamDirectory)).toBe("team");
expect(await websocketKind(teamVm, MEMBER, TEAM, teamDirectory)).toBe("team");
answer.members = [MEMBER, "user-third"];
expect(await cmuxRemoteKind(teamVm, CREATOR, TEAM, teamDirectory)).toBe("team");
expect(await websocketKind(teamVm, CREATOR, TEAM, teamDirectory)).toBe("team");
expect(await websocketKind(teamVm, CREATOR, TEAM, directory({ members: [MEMBER, CREATOR] }).teamDirectory)).toBe("team");
});

test("a teammate on the creator's one-member team is never owner, and Stack is not asked", async () => {
Expand Down Expand Up @@ -207,7 +212,7 @@ describe("vmKindForCaller", () => {
expect(vmKindForCaller({ userId: CREATOR, ownerTeamId: CREATOR }, CREATOR)).toBe("owner");
});

test("anyone else, any team, and a blank caller get team", () => {
test("anyone else, an unread team, and a blank caller get team", () => {
expect(vmKindForCaller({ userId: CREATOR, ownerTeamId: CREATOR }, MEMBER)).toBe("team");
expect(vmKindForCaller({ userId: CREATOR, ownerTeamId: TEAM }, CREATOR)).toBe("team");
expect(vmKindForCaller({ userId: CREATOR, ownerTeamId: TEAM }, MEMBER)).toBe("team");
Expand All @@ -228,11 +233,13 @@ describe("vmKindForCaller", () => {
expect(vmKindForCaller({ userId: CREATOR, ownerTeamId: "" }, CREATOR, { memberIds: [CREATOR] })).toBeNull();
});

test("a team machine is owner only for its creator, on a member list of exactly them", () => {
test("a team machine is owner only for its creator on the member list, at any team size", () => {
const solo = { userId: CREATOR, ownerTeamId: TEAM };
expect(vmKindForCaller(solo, CREATOR, { memberIds: [CREATOR] })).toBe("owner");
expect(vmKindForCaller(solo, CREATOR, { memberIds: [CREATOR, MEMBER] })).toBe("team");
expect(vmKindForCaller(solo, CREATOR, { memberIds: [CREATOR, CREATOR] })).toBe("team");
expect(vmKindForCaller(solo, CREATOR, { memberIds: [CREATOR, MEMBER, "user-third"] })).toBe("owner");
expect(vmKindForCaller(solo, MEMBER, { memberIds: [CREATOR, MEMBER, "user-third"] })).toBe("team");
expect(vmKindForCaller(solo, CREATOR, { memberIds: [CREATOR, CREATOR] })).toBe("owner");
expect(vmKindForCaller(solo, CREATOR, { memberIds: [MEMBER, "user-third"] })).toBe("team");
expect(vmKindForCaller(solo, CREATOR, { memberIds: [] })).toBe("team");
expect(vmKindForCaller(solo, CREATOR, { memberIds: null })).toBe("team");
expect(vmKindForCaller(solo, CREATOR, { error: "error" })).toBe("team");
Expand Down