From 54661c7d653cc22623bab22ac1e316cfd4f96c9a Mon Sep 17 00:00:00 2001 From: shuv1337 Date: Thu, 22 Oct 2026 19:21:48 -0700 Subject: [PATCH] Accept the signed-in organization's slug in executor apps commands A device-code session saves its organization's ID, but sign-in prints the slug. Naming it with --organization failed every command, including executor apps call, with a client-made Forbidden (HTTP 403) before any request. The CLI now accepts the session's ID or slug, and sends any other organization to the host, which refuses one the grant does not cover. Adds an E2E scenario that signs the CLI in with a device code and calls a tool as the owner (by slug, by ID, refused for another organization) and as a member (refused until the app is shared with everyone). --- .changeset/cli-organization-slug.md | 8 ++ e2e/support/apps-cli.ts | 14 ++-- e2e/test-plan.ts | 13 +++ e2e/tests/apps-cli-call.spec.ts | 120 ++++++++++++++++++++++++++++ packages/app-management/src/cli.ts | 16 +++- 5 files changed, 160 insertions(+), 11 deletions(-) create mode 100644 .changeset/cli-organization-slug.md diff --git a/.changeset/cli-organization-slug.md b/.changeset/cli-organization-slug.md new file mode 100644 index 0000000000..c50d596b61 --- /dev/null +++ b/.changeset/cli-organization-slug.md @@ -0,0 +1,8 @@ +--- +"@executor-js/app-management": patch +--- + +`executor apps` commands signed in with `executor apps login` accept the signed-in organization by +its slug as well as its ID. Sign-in prints the slug, but `--organization ` failed every +command, including `executor apps call`, with "Forbidden (HTTP 403)" before any request was sent. +Any other organization now goes to the host, which refuses one the session cannot use. diff --git a/e2e/support/apps-cli.ts b/e2e/support/apps-cli.ts index 5700fee0bd..c51950122a 100644 --- a/e2e/support/apps-cli.ts +++ b/e2e/support/apps-cli.ts @@ -265,14 +265,14 @@ export const appsCli = Effect.gen(function* () { return { ...printed, finished: running }; }); /** - * `executor apps login --host `, approved for the test organization by the synthetic - * owner. The grant is revoked with the caller's scope. + * `executor apps login --host `, approved for the test organization by a synthetic actor, + * the owner unless another is named. The grant is revoked with the caller's scope. */ - const login = (host: string) => + const login = (host: string, actor: typeof actors.owner = actors.owner) => Effect.gen(function* () { const started = yield* deviceLogin(host, "opens"); const lookup = yield* api.request( - actors.owner, + actor, "GET", `/api/auth/device/request?user_code=${encodeURIComponent(started.userCode)}`, ); @@ -281,16 +281,16 @@ export const appsCli = Effect.gen(function* () { Effect.gen(function* () { const grants = yield* body( Schema.Array(Schema.Struct({ id: Schema.String, clientId: Schema.String })), - yield* api.request(actors.owner, "GET", "/api/auth/oauth2/get-consents"), + yield* api.request(actor, "GET", "/api/auth/oauth2/get-consents"), ); for (const grant of grants.filter((item) => item.clientId === clientId)) - yield* api.request(actors.owner, "POST", "/api/auth/oauth2/delete-consent", { + yield* api.request(actor, "POST", "/api/auth/oauth2/delete-consent", { id: grant.id, }); }).pipe(Effect.orDie), ); const decided = yield* api.request( - actors.owner, + actor, "POST", "/api/auth/device/decide", { user_code: started.userCode, accept: true }, diff --git a/e2e/test-plan.ts b/e2e/test-plan.ts index 4ac22bfd97..b6b52560f1 100644 --- a/e2e/test-plan.ts +++ b/e2e/test-plan.ts @@ -317,6 +317,19 @@ export const scenarios = plan({ ), }, }, + appsCliCallDeviceLogin: { + fixtures: "actors", + file: "apps-cli-call.spec.ts", + title: + "executor apps call signed in with a device code runs an app tool for a person who can use the app, with the signed-in organization named by slug, by ID or not at all, and the host refuses it for another organization and for a person who cannot use the app until it is shared with them", + targets: { + "self-host": scheduled, + cloud: na( + "Cloud serves the same hosted server's tool-call route and device sign-in; self-host runs this scenario against it.", + ), + local: na("Local signs the CLI in with an API key, not a saved session."), + }, + }, appGitKeptCredentials: { fixtures: "actors", file: "app-git-remotes.spec.ts", diff --git a/e2e/tests/apps-cli-call.spec.ts b/e2e/tests/apps-cli-call.spec.ts index 2f01d81587..f81fe58094 100644 --- a/e2e/tests/apps-cli-call.spec.ts +++ b/e2e/tests/apps-cli-call.spec.ts @@ -1,6 +1,7 @@ /** * `executor apps call` runs one app tool from a shell with an API key, as an import script does: * input from a file or stdin, the tool's value on stdout, and the host's refusals as exit codes. + * A CLI signed in with a device code calls tools under its person's own access the same way. */ import { expect, layer } from "@effect/vitest"; import { Effect, Schema } from "effect"; @@ -231,4 +232,123 @@ layer(HostedLive, { excludeTestServices: true })("Apps CLI call", (it) => { }), ), ); + + it.effect(scenarios.appsCliCallDeviceLogin.title, (context) => + withHostedCase( + context, + Effect.gen(function* () { + const hosts = targetHosts(yield* Target); + const api = yield* Api, + actors = yield* Actors, + evidence = yield* Evidence; + const prefix = `/api/organizations/${actors.organization.id}`; + const deployed = yield* api.request(actors.owner, "POST", `${prefix}/apps/deploy`, { + name: `CLI device call ${randomUUID().slice(0, 8)}`, + files, + }); + expect(deployed.status).toBe(200); + const app = yield* body(App, deployed); + const path = `${prefix}/apps/${app.id}`; + yield* Effect.addFinalizer(() => + api.request(actors.owner, "DELETE", path).pipe(Effect.orDie), + ); + const input = JSON.stringify(records(2, "hello")); + /** Sign one actor's CLI in with a device code, approved for the test organization. */ + const signedIn = (actor: typeof actors.owner) => + Effect.gen(function* () { + const cli = yield* appsCli; + const login = yield* cli.login(hosts.api, actor); + expect(login.finished.code, login.finished.stderr).toBe(0); + return cli; + }); + const call = ( + cli: Effect.Success, + target: string, + selection: ReadonlyArray = [], + ) => + cli.cli([ + "call", + "--host", + hosts.api, + ...selection, + "--app", + target, + "--tool", + "import.records", + "--input", + input, + ]); + + // Both people approve their CLI's code at once; each approval waits for the next poll. + const [owner, member] = yield* Effect.all( + [signedIn(actors.owner), signedIn(actors.member)], + { concurrency: 2 }, + ); + + // The owner, who can use their own app, calls it. Sign-in prints the organization's slug, + // so people name it by slug as often as by ID; both select the signed-in organization. + const bySlug = ["--organization", actors.organization.slug]; + const listed = yield* owner.cli(["list", "--host", hosts.api, ...bySlug]); + expect(listed.code, listed.stderr).toBe(0); + expect(listed.stdout).toContain(app.id); + for (const [label, selection] of [ + ["slug", bySlug], + ["id", ["--organization", actors.organization.id]], + ] as const) { + const called = yield* call(owner, app.slug, selection); + yield* evidence.json(`owner-device-call-by-${label}.json`, { + code: called.code, + stderr: called.stderr, + }); + expect(called.code, called.stderr).toBe(0); + expect(yield* Schema.decodeUnknownEffect(Imported)(called.stdout)).toEqual({ + count: 2, + first: "r-1", + last: "r-2", + noteChars: 10, + }); + } + // An organization the session was not signed in to is the host's to refuse. + const elsewhere = yield* call(owner, app.id, [ + "--organization", + `elsewhere-${randomUUID().slice(0, 8)}`, + ]); + expect(elsewhere.code).toBe(1); + expect(elsewhere.stdout).not.toContain("r-2"); + expect((yield* Schema.decodeUnknownEffect(Failure)(elsewhere.stdout)).error).toMatchObject({ + code: "OrganizationForbidden", + status: 403, + }); + + // A member, calling without --organization, cannot use the owner's private app: the host + // refuses the call and the tool never runs. + const refused = yield* call(member, app.id); + yield* evidence.json("member-device-refused.json", refused); + expect(refused.code).toBe(1); + expect(refused.stdout).not.toContain("r-2"); + expect((yield* Schema.decodeUnknownEffect(Failure)(refused.stdout)).error).toMatchObject({ + code: "OrganizationForbidden", + status: 403, + }); + + // Once the owner shares the app with everyone, the member's same session may call it. + const access = yield* body( + Schema.Struct({ revision: Schema.String }), + yield* api.request(actors.owner, "GET", `${path}/access`), + ); + const shared = yield* api.request(actors.owner, "PATCH", `${path}/access`, { + revision: access.revision, + audience: { kind: "everyone" }, + }); + expect(shared.status).toBe(200); + const allowed = yield* call(member, app.slug); + yield* evidence.json("member-device-call.json", { + code: allowed.code, + stderr: allowed.stderr, + }); + expect(allowed.code, allowed.stderr).toBe(0); + expect((yield* Schema.decodeUnknownEffect(Imported)(allowed.stdout)).count).toBe(2); + }), + ), + ); }); diff --git a/packages/app-management/src/cli.ts b/packages/app-management/src/cli.ts index 4f5152c18b..57546dcc23 100644 --- a/packages/app-management/src/cli.ts +++ b/packages/app-management/src/cli.ts @@ -103,7 +103,7 @@ const host = Flag.String("host").pipe( ); const organization = Flag.String("organization").pipe( Flag.withDescription( - "Hosted organization. Required with EXECUTOR_ACCESS_TOKEN; after executor apps login it defaults to the signed-in organization", + "Hosted organization ID or slug. Required with EXECUTOR_ACCESS_TOKEN; after executor apps login it defaults to the signed-in organization", ), Flag.optional, ); @@ -153,9 +153,17 @@ const access = (host: string, organization: Option.Option) => ) return { token: local.value, organization: undefined }; const session = Redacted.value(yield* registrySession(host)); - if (Option.isSome(organization) && organization.value !== session.organization) - return yield* new AppClientError({ reason: "forbidden" }); - return { token: Redacted.make(session.accessToken), organization: session.organization }; + // Sign-in prints the organization's slug and saves its ID, so either names the session's own + // organization. Any other reference goes to the host, which refuses an organization the + // session's grant does not cover; the CLI never reports a refusal the host did not send. + const named = Option.getOrUndefined(organization); + return { + token: Redacted.make(session.accessToken), + organization: + named === undefined || named === session.organization || named === session.namespace + ? session.organization + : named, + }; }); /** Authenticated requests to the host; redirects are refused so credentials never follow them. */ const transport = (host: string, token: Redacted.Redacted, path = (url: string) => url) =>