History

Report honest removed flag from oauth.clients.remove (#1603)

The tool mapped an idempotent storage no-op to removed: true, so a typo'd slug, an already-deleted client and the wrong owner all read as a real deletion. Clients are keyed by (owner, slug), so a sweep under one hardcoded owner silently skipped the other scope's copies. The tool now checks the visible client set first; the service-level removeClient stays idempotent.

Rhys Sullivan committed 3 months ago

Commit 624e85f1 parent 1b5f9313 files · +94 −6Browse files

Unified · Split

3 files
  1. .changeset/oauth-clients-remove-honest.md +11 −0
  2. packages/core/sdk/src/core-tools.ts +17 −5
  3. packages/core/sdk/src/oauth-remove-client.test.ts +66 −1
.changeset/oauth-clients-remove-honest.md+11−0
@@ -0,0 +1,11 @@
1---
2"executor": patch
3---
4
5**Fix: `oauth.clients.remove` reported success for clients it never removed**
6
7The tool returned `{ removed: true }` unconditionally. `oauth.removeClient` is idempotent by design at the storage layer — `deleteMany` on a missing row is a no-op, which is the right behaviour for a delete — but the tool mapped that silence to success, so a typo'd slug, an already-deleted client, and the wrong owner were all indistinguishable from a real deletion.
8
9This bites hardest because clients are keyed by BOTH owner and slug, so the same slug can exist separately under `org` and `user`. An agent sweeping a list of slugs under one hardcoded owner would delete only half of them and report every call as a success, leaving org-owned OAuth apps registered after everything they authorized was gone.
10
11The tool now checks the caller-visible client set first and returns `removed: false` when nothing matched that `(owner, slug)` pair. The service-level `removeClient` is unchanged and stays idempotent.
packages/core/sdk/src/core-tools.ts+17−5
@@ -819,7 +819,7 @@ export const coreToolsPlugin = definePlugin((options: CoreToolsPluginOptions = { · 818 unmodified lines
819 tool({819 tool({
820 name: "oauth.clients.remove",820 name: "oauth.clients.remove",
821 description:821 description:
822 "Remove an owner-scoped OAuth client by owner and slug. Existing connections are not cascaded.",822 "Remove an owner-scoped OAuth client by owner and slug. `removed: false` means no client matched that owner and slug — clients are keyed by BOTH, so the same slug can exist separately under `org` and `user`. Existing connections are not cascaded.",
823 inputSchema: OAuthRemoveClientInputStd,823 inputSchema: OAuthRemoveClientInputStd,
824 outputSchema: RemovedOutputStd,824 outputSchema: RemovedOutputStd,
825 // Removing a client breaks token refresh for every connection that825 // Removing a client breaks token refresh for every connection that
@@ -828,10 +828,22 @@ export const coreToolsPlugin = definePlugin((options: CoreToolsPluginOptions = { · 2 unmodified lines
828 // `sources.bindings.remove`.828 // `sources.bindings.remove`.
829 annotations: { requiresApproval: true },829 annotations: { requiresApproval: true },
830 execute: (input: typeof OAuthRemoveClientInput.Type, { ctx }) =>830 execute: (input: typeof OAuthRemoveClientInput.Type, { ctx }) =>
831 Effect.map(831 Effect.gen(function* () {
832 ctx.oauth.removeClient(input.owner as Owner, OAuthClientSlug.make(input.slug)),832 const owner = input.owner as Owner;
833 () => ({ removed: true }),833 const slug = OAuthClientSlug.make(input.slug);
834 ),834 // `removeClient` is idempotent by design at the storage layer, so
835 // on its own it cannot distinguish a real deletion from a typo'd
836 // slug or the wrong owner — and a caller sweeping a list of slugs
837 // under one hardcoded owner would read every no-op as success.
838 // Checking the visible set first is what keeps `removed` honest.
839 const clients = yield* ctx.oauth.listClients();
840 const matched = clients.some(
841 (client) => client.owner === owner && String(client.slug) === String(slug),
842 );
843 if (!matched) return { removed: false };
844 yield* ctx.oauth.removeClient(owner, slug);
845 return { removed: true };
846 }),
835 }),847 }),
836 tool({848 tool({
837 name: "oauth.probe",849 name: "oauth.probe",
packages/core/sdk/src/oauth-remove-client.test.ts+66−1
@@ -5,7 +5,7 @@ import { join } from "node:path"; · 4 unmodified lines
5import { describe, expect, it } from "@effect/vitest";5import { describe, expect, it } from "@effect/vitest";
6import { Effect } from "effect";6import { Effect } from "effect";
7 7
8import { OAuthClientSlug } from "./ids";8import { OAuthClientSlug, ToolAddress } from "./ids";
9import { makeTestWorkspaceHarness, memoryCredentialsPlugin } from "./test-config";9import { makeTestWorkspaceHarness, memoryCredentialsPlugin } from "./test-config";
10 10
11// removeClient permanently deletes an owner-scoped oauth_client row, keyed by11// removeClient permanently deletes an owner-scoped oauth_client row, keyed by
@@ -175,4 +175,69 @@ describe("oauth.removeClient", () => { · 163 unmodified lines
175 }),175 }),
176 ),176 ),
177 );177 );
178
179 // The `oauth.clients.remove` TOOL reports `removed` honestly on top of the
180 // idempotent service call above, so an agent sweeping a list of slugs cannot
181 // read a no-op as a deletion.
182 it.effect("the remove tool distinguishes a real deletion from a no-op", () =>
183 Effect.scoped(
184 Effect.gen(function* () {
185 const { executor } = yield* makeTestWorkspaceHarness({
186 plugins,
187 coreTools: {},
188 });
189 const remove = ToolAddress.make("executor.coreTools.oauth.clients.remove");
190
191 // The same slug registered under BOTH owners — the shape that made a
192 // hardcoded `owner: "user"` sweep silently skip the org copy.
193 for (const owner of ["org", "user"] as const) {
194 yield* executor.oauth.createClient({
195 owner,
196 slug: ORG_CLIENT,
197 authorizationUrl: "https://acme.test/authorize",
198 tokenUrl: "https://acme.test/token",
199 grant: "authorization_code",
200 clientId: `${owner}-client-id`,
201 clientSecret: `${owner}-secret`,
202 });
203 }
204
205 // A slug that never existed is not a removal.
206 expect(
207 yield* executor.execute(
208 remove,
209 { owner: "user", slug: "never-existed" },
210 { onElicitation: "accept-all" },
211 ),
212 ).toEqual({ removed: false });
213
214 // Removing the user copy leaves the org copy, which still reports as a
215 // real removal of its own rather than as already-gone.
216 expect(
217 yield* executor.execute(
218 remove,
219 { owner: "user", slug: String(ORG_CLIENT) },
220 { onElicitation: "accept-all" },
221 ),
222 ).toEqual({ removed: true });
223 expect(
224 yield* executor.execute(
225 remove,
226 { owner: "org", slug: String(ORG_CLIENT) },
227 { onElicitation: "accept-all" },
228 ),
229 ).toEqual({ removed: true });
230
231 // Both are gone, and a repeat of either is now a no-op.
232 expect(yield* executor.oauth.listClients()).toEqual([]);
233 expect(
234 yield* executor.execute(
235 remove,
236 { owner: "org", slug: String(ORG_CLIENT) },
237 { onElicitation: "accept-all" },
238 ),
239 ).toEqual({ removed: false });
240 }),
241 ),
242 );
178});243});

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t