diff --git a/.github/workflows/fork-ci.yml b/.github/workflows/fork-ci.yml index 275e904a48..9e4045e3ba 100644 --- a/.github/workflows/fork-ci.yml +++ b/.github/workflows/fork-ci.yml @@ -91,6 +91,8 @@ jobs: bun run e2e:apps-published -- --testNamePattern 'self-host gate' - name: Tool error contract scenarios run: bun run e2e:self-host --test-name 'Every error tools/call and tools/run declare|Every error a write reports after its app code received the call' + - name: Reused app account scenario + run: bun run e2e:self-host --test-name '^a reused hosted app account (stops serving|refuses a call)' --workers 1 - name: Operator feedback scenarios run: bun run e2e:self-host --test-name 'self-host feedback webhook|self-host with DO_NOT_TRACK|self-host analytics pseudonymize|Hosted feedback enforces' # Run the large cold catalog alone, not beside the functional scenarios' servers. diff --git a/apps/docs/content/concepts/providers-and-accounts.md b/apps/docs/content/concepts/providers-and-accounts.md index e8b6644efb..b2851edb34 100644 --- a/apps/docs/content/concepts/providers-and-accounts.md +++ b/apps/docs/content/concepts/providers-and-accounts.md @@ -116,15 +116,32 @@ with your links. People who can edit an app link its app accounts for everyone, in the **App accounts** section of the app's **Accounts** tab. Linking needs edit access to -the app, and the account must be your own: accounts are never shared, so this is -how one account serves everyone. It stops serving if its owner leaves the -organization. Callers use the account through the app without +the app, and the account must be your own: accounts are never shared, so this +is how one account serves everyone. An organization owner or admin may also +reuse an account already linked for everyone to an app they can read and edit +with the same calling credential. A credential limited to selected apps cannot +reuse accounts from apps outside that selection. Reuse is not allowed +into an app a member created, unless that member owns the account, nor into one +created by someone who has since left the organization or an app without a +creator. The same rule applies when the link is used. If a creator who does not +own the account is demoted or leaves, or the app loses its creator, the reused +link stops serving with `AppLinkCreatorNotAuthorized` (409). Deleting a user +also removes reused links from the apps they created. +If that creator becomes an admin or owner again, the stored link can serve +again. An admin can relink an eligible account, or the account's owner can link +it directly if they can edit the app. A link made by the account owner does not +depend on the creator's role. The account stays its owner's, and its owner may +unlink it from any app. It stops serving if its owner leaves the organization. +Callers use the account through the app without access to the account itself, and they cannot choose or replace it; everyone else sees the section read-only, without the account's details from the provider. Linking another account to a plain slot replaces the one before; a `.many()` slot uses every linked account. Unlinking stops the app using the account, which stays saved. An account linked to an app for everyone cannot be -deleted until it is unlinked. The same account may also be linked for yourself. +deleted until it is unlinked. Its owner can call `appAccounts.appUnlinkOwned` with +the account and app ids to remove all its app links there, even when the app is +private and they cannot read its requirement names. This returns only those ids, +not the app's contents. The same account may also be linked for yourself. When an app account needs reconnecting, the people who can edit the app are told, and a call that needs it fails saying that one of them must reconnect it. diff --git a/apps/docs/content/connect-an-account.mdx b/apps/docs/content/connect-an-account.mdx index d81c9d712c..2f5459492f 100644 --- a/apps/docs/content/connect-an-account.mdx +++ b/apps/docs/content/connect-an-account.mdx @@ -178,7 +178,11 @@ linked to a single-account requirement and every account you linked to a `.many()` requirement; for each of the app's own requirements, the accounts linked for everyone. A single-account requirement with no account fails with `AccountRequired`, which says whether you need to link one or someone who can -edit the app does. A tool input that passes `$accounts` fails with +edit the app does. A reused app link whose creator is missing or no longer an +organization admin or owner fails with `AppLinkCreatorNotAuthorized` (409), unless the +creator owns the account or the account's owner made the link. Ask an admin to +relink an eligible account, or ask the account's owner to link it directly. +A tool input that passes `$accounts` fails with `AccountChoiceUnsupported`. To use a different account, link it on the app's **Accounts** tab. To use two @@ -193,7 +197,9 @@ from an app that uses the account: open its menu on the app's **Accounts** tab a linked to it keeps working. Removing an account removes your links to it. An account linked to an app for -everyone cannot be removed until someone who can edit the app unlinks it -(`AccountLinkedToApp`). An app left with no account +everyone cannot be removed until it is unlinked (`AccountLinkedToApp`). As its +owner, call `appAccounts.appUnlinkOwned` with the account and each app id named by +that error. This removes all its app links there without needing access to the +app or its requirement names. An app left with no account for a requirement exposes no tools until you link or connect another. Executor does not substitute a different account. diff --git a/apps/hosted/server/src/contracts/accounts.ts b/apps/hosted/server/src/contracts/accounts.ts index 0303c7cbd5..fe13a8338a 100644 --- a/apps/hosted/server/src/contracts/accounts.ts +++ b/apps/hosted/server/src/contracts/accounts.ts @@ -58,9 +58,9 @@ export const AccountLinkedToApp = UserFacingError.define({ description: "An app uses this account for everyone who uses the app, so it can't be deleted while it is linked.", recovery: { - action: "Ask someone who can edit each app to unlink the account, then delete it.", + action: "Unlink your account from each named app, then delete it.", instructions: - "Read the app accounts of each named app with appAccounts.appList and unlink this account with appAccounts.appUnlink, or ask someone who can edit the app to. Link a replacement first if the app still needs one. Then delete the account.", + "Call appAccounts.appUnlinkOwned with this account and each named app id to remove all of your account's app links there. You do not need access to the app or its requirement names. Ask an editor to link a replacement first if the app still needs one. Then delete the account.", }, }); /** Parsed AccountLinkedToApp failure. */ diff --git a/apps/hosted/server/src/contracts/app-accounts.ts b/apps/hosted/server/src/contracts/app-accounts.ts index e448efa259..5dd02ea51c 100644 --- a/apps/hosted/server/src/contracts/app-accounts.ts +++ b/apps/hosted/server/src/contracts/app-accounts.ts @@ -193,7 +193,7 @@ const scopeEndpoints = (scope: S) => { .annotate( OpenApi.Description, forEveryone - ? "Read one of this app's own requirements: the accounts linked to it, then the other accounts of its provider you can use and could link, each marked linked or not. Needs edit access to the app." + ? "Read one of this app's own requirements: the accounts linked to it, then the other accounts of its provider you could link, each marked linked or not: your own and, for an organization owner or admin, accounts already linked for everyone to an app they can edit, unless a member other than the account's owner, or someone who has since left the organization, created this app. Needs edit access to the app." : "Read one of this app's user requirements: the accounts you linked to it, then the other accounts of its provider you can use and could link, each marked linked or not. Choose from these accounts before linking.", ), HttpApiEndpoint.put(`${scope}Link`, `${path}/:requirement/:account`, { @@ -205,7 +205,7 @@ const scopeEndpoints = (scope: S) => { .annotate( OpenApi.Description, forEveryone - ? "Link an account to one of this app's own requirements, so every caller and the app's background work use it through the app, without access to the account itself. Needs edit access to the app and use of the account. Linking an account to a plain requirement replaces the account linked to it before; for many() it adds one. Linking again changes nothing. The replaced account stays saved." + ? "Link an account to one of this app's own requirements, so every caller and the app's background work use it through the app, without access to the account itself. Needs edit access to the app, and the account must be your own, or, for an organization owner or admin, already linked for everyone to an app they can edit and this app not created by a member other than the account's owner or by someone who has since left the organization; it stays its owner's. Linking an account to a plain requirement replaces the account linked to it before; for many() it adds one. Linking again changes nothing. The replaced account stays saved." : "Link one of your accounts to one of this app's user requirements. Needs use of the app and of the account. Linking an account to a plain requirement replaces the account you linked to it before; for many() it adds one. Linking again changes nothing. The replaced account stays saved.", ), HttpApiEndpoint.delete(`${scope}Unlink`, `${path}/:requirement/:account`, { @@ -217,7 +217,7 @@ const scopeEndpoints = (scope: S) => { .annotate( OpenApi.Description, forEveryone - ? "Stop this app using an account for one of its own requirements. Needs edit access to the app, not use of the account. Calls and background work that need the requirement fail until another account is linked, and the app's webhooks registered with the account are removed. The account stays saved." + ? "Stop this app using an account for one of its own requirements. Needs edit access to the app, not use of the account, or ownership of the account. Calls and background work that need the requirement fail until another account is linked, and the app's webhooks registered with the account are removed. The account stays saved." : "Stop using an account for one of this app's user requirements. The account and its other links stay unchanged.", ), HttpApiEndpoint.post(`${scope}Connect`, `${path}/:requirement/connections`, { @@ -251,6 +251,18 @@ export const HostedAppAccounts = HttpApiGroup.make("appAccounts") .add(appGet) .add(appLink) .add(appUnlink) + .add( + HttpApiEndpoint.delete("appUnlinkOwned", `${prefix}/app/:account`, { + params: { ...app, account: AccountId }, + success: Schema.Struct({ app: AppId, account: AccountId }), + error: [StorageError, OrganizationForbidden], + }) + .annotate(RequiredAction, "manage") + .annotate( + OpenApi.Description, + "As the account's owner, stop one app using it for everyone across all requirements. Needs only the app id, not access to the app or its requirement names. Returns only the app and account ids. An app without one of your account's app links is forbidden, whether it exists or not. The account stays saved.", + ), + ) .add(appConnect) .add(userList) .add(userGet) diff --git a/apps/hosted/server/src/contracts/security-audit.ts b/apps/hosted/server/src/contracts/security-audit.ts index 7cbc88cf2c..15623efe4c 100644 --- a/apps/hosted/server/src/contracts/security-audit.ts +++ b/apps/hosted/server/src/contracts/security-audit.ts @@ -28,6 +28,9 @@ export const SecurityAuditAction = Schema.Literals([ "organization.delete", "user.delete", "credential.read", + "app_account.link", + "app_account.unlink", + "app_account.use_refused", ]); export type SecurityAuditAction = typeof SecurityAuditAction.Type; diff --git a/apps/hosted/server/src/contracts/tools.ts b/apps/hosted/server/src/contracts/tools.ts index aec53ef0b7..ada688b73f 100644 --- a/apps/hosted/server/src/contracts/tools.ts +++ b/apps/hosted/server/src/contracts/tools.ts @@ -6,6 +6,7 @@ import { AccountChoiceUnsupported, AccountNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountSelectionInvalid, AppEvaluationFailed, AppProviderFailed, @@ -67,6 +68,7 @@ const discoveryErrors = [ AppProviderFailed, AccountNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, OAuthReconnectRequired, diff --git a/apps/hosted/server/src/implementation/access.ts b/apps/hosted/server/src/implementation/access.ts index 9408588917..4bd41930b7 100644 --- a/apps/hosted/server/src/implementation/access.ts +++ b/apps/hosted/server/src/implementation/access.ts @@ -40,8 +40,9 @@ import { import { refuse } from "./auth-telemetry.ts"; import { appAccountReads, - appLinkedAccounts, + checkedAppLinkedAccounts, callerAccounts, + refuseAppLinkCreators, versionAccounts, } from "./app-accounts.ts"; import { @@ -405,7 +406,10 @@ const checkAppLinked = (app: AppId, linked: AppAccounts["app"]) => ids.map((id) => ({ requirement, id })), ); if (used.length === 0) return; - const current = yield* appLinkedAccounts(yield* policyDatabase, [app]); + const current = (yield* checkedAppLinkedAccounts(yield* policyDatabase, app)).filter((link) => + used.some(({ requirement, id }) => link.requirement === requirement && link.id === id), + ); + yield* refuseAppLinkCreators(current); for (const { requirement, id } of used) if (!current.some((link) => link.requirement === requirement && link.id === id)) return yield* new OrganizationForbidden(); diff --git a/apps/hosted/server/src/implementation/accounts.ts b/apps/hosted/server/src/implementation/accounts.ts index f00f9dac7b..0491f061bd 100644 --- a/apps/hosted/server/src/implementation/accounts.ts +++ b/apps/hosted/server/src/implementation/accounts.ts @@ -1,5 +1,5 @@ import { accountOAuthRedirectUri } from "./auth.ts"; -import { audited } from "./security-audit.ts"; +import { audited, withCommittedAudits } from "./security-audit.ts"; import { ScheduleWakeup } from "../contracts/schedules.ts"; import { CurrentPolicy } from "../contracts/authorization.ts"; import { permittedAppIds } from "@executor-js/authorization"; @@ -270,9 +270,10 @@ export const submitConnection = ( Effect.gen(function* () { const executor = yield* Effect.flatten(HostedExecutor); yield* checkConnection(yield* ownedConnection(executor, owner, input.connection)); - return yield* executor.accountConnections - .submit({ ...input, owner }) - .pipe(Effect.tap(() => Effect.flatten(ScheduleWakeup))); + return yield* executor.accountConnections.submit({ ...input, owner }).pipe( + withCommittedAudits, + Effect.tap(() => Effect.flatten(ScheduleWakeup)), + ); }); /** * The member's client choice is checked against its audience here, then named to the SDK, which @@ -289,11 +290,14 @@ export const startOAuth = ( yield* checkConnection(yield* ownedConnection(executor, owner, input.connection)); const client = yield* connectionClient(executor, owner, input); const { client: _selected, ...rest } = input; - return yield* executor.accountConnections.startOAuth({ - ...rest, - owner, - ...(client === undefined ? {} : { client }), - }); + // A sign-in that needs no redirect completes here, linking the account as completion does. + return yield* executor.accountConnections + .startOAuth({ + ...rest, + owner, + ...(client === undefined ? {} : { client }), + }) + .pipe(withCommittedAudits); }); /** Completion rechecks connection and target ownership before saving provider credentials. */ export const completeOAuth = ( @@ -303,9 +307,10 @@ export const completeOAuth = ( Effect.gen(function* () { const executor = yield* Effect.flatten(HostedExecutor); yield* checkConnection(yield* ownedConnection(executor, owner, input.connection)); - return yield* executor.accountConnections - .completeOAuth({ ...input, owner }) - .pipe(Effect.tap(() => Effect.flatten(ScheduleWakeup))); + return yield* executor.accountConnections.completeOAuth({ ...input, owner }).pipe( + withCommittedAudits, + Effect.tap(() => Effect.flatten(ScheduleWakeup)), + ); }); /** Account policy and persisted connection ownership protect management and OAuth return requests. */ diff --git a/apps/hosted/server/src/implementation/app-account-links.ts b/apps/hosted/server/src/implementation/app-account-links.ts index fee0f10992..9133b0fa2e 100644 --- a/apps/hosted/server/src/implementation/app-account-links.ts +++ b/apps/hosted/server/src/implementation/app-account-links.ts @@ -1,7 +1,10 @@ /** * The routes that link accounts to an app's requirements, in either scope. App links need edit - * access to the app and, to link, use of the account; reading them needs use of the app. User links - * are the caller's own: linking another person's account is never possible. + * access to the app and, to link, the caller's own account, or for an administrator one already + * linked for everyone that no member but its owner would come to edit (see + * `appLinkableAccountsAs`); unlinking needs edit access or ownership of the account; reading them + * needs use of the app. User links are the caller's own: linking another person's account is never + * possible. */ import { AccountNotFound, @@ -26,18 +29,31 @@ import { } from "../contracts/app-accounts.ts"; import { BearerAuthentication, SessionAuthentication, CurrentUserId } from "../contracts/auth.ts"; import { HostedExecutor } from "../contracts/executor.ts"; -import { OrganizationForbidden } from "../contracts/organization.ts"; +import { CurrentOrganization, OrganizationForbidden } from "../contracts/organization.ts"; import { ScheduleWakeup } from "../contracts/schedules.ts"; import { appManagerOwner, currentOwner, usableApp } from "./access.ts"; import { connectAccount, connectionUrl } from "./accounts.ts"; import { accountOAuthRedirectUri } from "./auth.ts"; -import { addLink, callerLinkedAccounts, lockedChange, removeLink } from "./app-accounts.ts"; import { + addLink, + callerLinkedAccounts, + lockedChange, + removeLink, + removeAppLinksOf, + appLinkApps, +} from "./app-accounts.ts"; +import { + appLinkableAccounts, + currentResourceAuthority, + ownedAccountIds, policyDatabase, requireAccountOwner, + requireAccountOwnerAs, + requireAppLinkableAs, requireCurrentAppAccess, visibleAccounts, } from "./resource-policy.ts"; +import { audited } from "./security-audit.ts"; const caller = Effect.gen(function* () { const user = yield* CurrentUserId; @@ -181,7 +197,11 @@ const requirementView = ( return account === undefined ? [] : [account]; }); const linkedIds = new Set(linkedAccounts.map((account) => account.id)); - const usable = (yield* visibleAccounts(all)).filter((account) => !linkedIds.has(account.id)); + // App requirements also take accounts an administrator may reuse from another app's link. + const candidates = yield* scope === "app" + ? appLinkableAccounts(app.id, all) + : visibleAccounts(all); + const usable = candidates.filter((account) => !linkedIds.has(account.id)); return { requirement, scope, @@ -269,60 +289,155 @@ export const hostedAppAccountHandlers = HttpApiBuilder.group(HostedApi, "appAcco return yield* requirementView(executor, owner, app, "app", params.requirement, linked); }), ) - .handle("appLink", ({ params }) => - Effect.gen(function* () { + .handle("appLink", ({ params }) => { + // The app the audit record names: only once it was found, never the request's text. + let resolved: AppId | undefined; + return Effect.gen(function* () { const user = yield* caller; const owner = yield* appManagerOwner(params.app); const executor = yield* Effect.flatten(HostedExecutor); - yield* executor.apps.get({ owner, app: params.app }); - yield* requireAccountOwner(params.account); + resolved = (yield* executor.apps.get({ owner, app: params.app })).id; + // The caller's own account, or one an administrator reuses from another app's link. + const checked = yield* requireAppLinkableAs( + yield* currentResourceAuthority, + params.app, + params.account, + ); yield* executor.accounts.get({ owner, account: params.account }); - const changed = yield* lockedChange( + const result = yield* lockedChange( executor, - { app: params.app, access: "manage", account: params.account }, - (sql) => - addLink(sql, executor.webhooks, { - app: params.app, - requirement: params.requirement, - account: params.account, - subject: null, - linkedBy: user, + { app: params.app, access: "manage", reuse: checked === "app_link" }, + (sql, actor) => + Effect.gen(function* () { + // Checked again under the lock, holding what a reuse relies on until commit. + const authority = yield* requireAppLinkableAs( + actor, + params.app, + params.account, + true, + ); + const changed = yield* addLink(sql, executor.webhooks, { + app: params.app, + requirement: params.requirement, + account: params.account, + subject: null, + linkedBy: user, + reused: authority === "app_link", + }); + return { changed, authority }; }), ); - if (changed) yield* wake; - return yield* savedLink( - executor, - owner, - params.app, - null, - params.requirement, - params.account, - ); - }), - ) - .handle("appUnlink", ({ params }) => - Effect.gen(function* () { + if (result.changed) yield* wake; + return result; + }).pipe( + audited( + "app_account.link", + { kind: "account", id: () => params.account }, + () => (resolved === undefined ? {} : { app: resolved }), + ({ changed, authority }) => ({ authority, changed: String(changed) }), + ), + Effect.andThen( + Effect.gen(function* () { + const owner = yield* currentOwner; + const executor = yield* Effect.flatten(HostedExecutor); + return yield* savedLink( + executor, + owner, + params.app, + null, + params.requirement, + params.account, + ); + }), + ), + ); + }) + .handle("appUnlink", ({ params }) => { + let resolved: AppId | undefined; + return Effect.gen(function* () { // Unlinking needs edit access to the app, not use of the account: an editor can stop the - // app using an account they cannot use. - const owner = yield* appManagerOwner(params.app); + // app using an account they cannot use. An account's owner may also stop any app using + // it, since an administrator may have linked it to one they cannot edit. + const owns = (yield* ownedAccountIds( + [params.account], + yield* currentResourceAuthority, + )).has(params.account); + const owner = owns ? yield* currentOwner : yield* appManagerOwner(params.app); const executor = yield* Effect.flatten(HostedExecutor); - yield* executor.apps.get({ owner, app: params.app }); - const removed = yield* lockedChange( + // An owner must already have a link here; do not disclose whether arbitrary apps exist. + if ( + owns && + !(yield* appLinkApps(yield* policyDatabase, params.account)).includes(params.app) + ) + return yield* new OrganizationForbidden(); + resolved = (yield* executor.apps.get({ owner, app: params.app })).id; + const { removed, authority } = yield* lockedChange( executor, - { app: params.app, access: "manage" }, - (sql) => - removeLink(sql, executor.webhooks, { - app: params.app, - requirement: params.requirement, - account: params.account, - subject: null, + { app: params.app, access: "unlink", account: params.account }, + (sql, actor) => + Effect.gen(function* () { + const authority = (yield* ownedAccountIds([params.account], actor)).has( + params.account, + ) + ? "owner" + : "app_manager"; + const removed = yield* removeLink(sql, executor.webhooks, { + app: params.app, + requirement: params.requirement, + account: params.account, + subject: null, + }); + return { removed, authority }; }), ); if (!removed) return yield* new AccountNotFound({ account: params.account }); yield* wake; - return { requirement: params.requirement, account: params.account }; - }), - ) + return { + authority, + unlinked: { requirement: params.requirement, account: params.account }, + }; + }).pipe( + audited( + "app_account.unlink", + { kind: "account", id: () => params.account }, + () => (resolved === undefined ? {} : { app: resolved }), + ({ authority }) => ({ authority }), + ), + Effect.map(({ unlinked }) => unlinked), + ); + }) + .handle("appUnlinkOwned", ({ params }) => { + // Named only once one of the owner's links shows the app exists. + let resolved: AppId | undefined; + return Effect.gen(function* () { + yield* requireAccountOwner(params.account); + const executor = yield* Effect.flatten(HostedExecutor); + const { organization } = yield* CurrentOrganization; + // Removing app links takes the organization's reuse lock, so take it first here too, + // before the app's link lock, in the order a reuse takes them. + yield* lockedChange( + executor, + { app: params.app, access: "unlink", account: params.account, reuse: true }, + (sql, actor) => + Effect.gen(function* () { + yield* requireAccountOwnerAs(actor, params.account); + if (!(yield* appLinkApps(sql, params.account)).includes(params.app)) + return yield* new OrganizationForbidden(); + resolved = params.app; + yield* removeAppLinksOf(executor, sql, organization, params.account, params.app); + }), + ); + yield* wake; + return { app: params.app, account: params.account }; + }).pipe( + audited( + "app_account.unlink", + { kind: "account", id: () => params.account }, + () => (resolved === undefined ? {} : { app: resolved }), + () => ({ authority: "owner" }), + ), + ); + }) .handle("appConnect", ({ params, payload }) => connect(services, "app", params, payload)) .handle("userList", ({ params }) => Effect.gen(function* () { diff --git a/apps/hosted/server/src/implementation/app-account-schema.ts b/apps/hosted/server/src/implementation/app-account-schema.ts index c0ccbf596c..a359d40118 100644 --- a/apps/hosted/server/src/implementation/app-account-schema.ts +++ b/apps/hosted/server/src/implementation/app-account-schema.ts @@ -111,7 +111,8 @@ const namesProfile = (apps: string, targets: string) => `exists ( * one link per app (and subject), kept by the link routes under the app's lock; many() any number. * Removing the app or the member removes their links. Deleting an account removes its links, but * an account with an app link is refused first (`AccountLinkedToApp`, and the account removal hook - * under the account's row lock). `linked_by` is the member who made the link, kept as history. + * under the account's row lock). `linked_by` records the member who made the link. An explicit + * app-link reuse prefixes that ID with `reuse:`; unprefixed owner and legacy links are unchanged. * * - One user link per app, requirement, account and member from every enabled profile that is not * being removed and whose account set stays with the app, the requirement taken from the diff --git a/apps/hosted/server/src/implementation/app-accounts.ts b/apps/hosted/server/src/implementation/app-accounts.ts index de5569a742..1078c4b10d 100644 --- a/apps/hosted/server/src/implementation/app-accounts.ts +++ b/apps/hosted/server/src/implementation/app-accounts.ts @@ -10,6 +10,7 @@ import { CurrentCaller, type LinkedAccounts, AccountNotFound, + AppLinkCreatorNotAuthorized, AppId, StorageError, StorageHost, @@ -32,20 +33,27 @@ import { CurrentUserId } from "../contracts/auth.ts"; import { CurrentPolicy } from "../contracts/authorization.ts"; import { ConnectionAccountUnlinked, RequirementMismatch } from "../contracts/app-accounts.ts"; -import { OrganizationForbidden } from "../contracts/organization.ts"; +import { CurrentOrganization, OrganizationForbidden } from "../contracts/organization.ts"; import { AppAccountReads, + appLinkRows, + checkedAppLinkJson, + CheckedAppLink, + creatorRefusal, LinkedAccount, linkedAccountJson, lockCurrentActor, + ownedAccountIds, policyDatabase, requireAccountOwnerAs, requireAppAccessAs, + reusedAppLinkPrefix, usableAppLinks, usableLinks, type ResourceAuthority, } from "./resource-policy.ts"; import { userAccountIds } from "./authorization.ts"; +import { recordAppLinkCreatorRefusal } from "./security-audit.ts"; const sqlFailures = { SqlError: () => new StorageError(), @@ -75,6 +83,55 @@ export const appLinkedAccounts = (sql: SqlClient.SqlClient, apps: readonly AppId return yield* Schema.decodeUnknownEffect(Schema.Array(LinkedAccount))(rows); }).pipe(Effect.catchTags(sqlFailures)); +export const checkedAppLinkedAccounts = (sql: SqlClient.SqlClient, app: AppId) => + Effect.gen(function* () { + const rows = yield* sql`select l.app, l.requirement, a.id, a.provider, a.label, a.description, + ${creatorRefusal(sql)} as "creatorRefusal" + from ${appLinkRows(sql, [app])} + order by l.created_at, a.id`; + return yield* Schema.decodeUnknownEffect(Schema.Array(CheckedAppLink))(rows); + }).pipe(Effect.catchTags(sqlFailures)); + +/** Report every affected link, but surface one actionable error in selection order. */ +export const refuseAppLinkCreators = ( + links: ReadonlyArray<{ + readonly app: AppId; + readonly id: AccountId; + readonly requirement: string; + readonly creatorRefusal: string | null; + }>, +) => + Effect.gen(function* () { + let refusal: AppLinkCreatorNotAuthorized | undefined; + for (const link of links) { + if (link.creatorRefusal === null) continue; + yield* recordAppLinkCreatorRefusal({ ...link, creatorRefusal: link.creatorRefusal }); + refusal ??= new AppLinkCreatorNotAuthorized({ + app: link.app, + account: link.id, + requirement: link.requirement, + }); + } + if (refusal !== undefined) return yield* refusal; + }); + +/** An operation only needs the newest link of a plain requirement and every link of a many(). */ +export const checkRequirementAppLinks = ( + app: AppId, + requirements: AppRequirements["accounts"], + links: ReadonlyArray, + source?: AccountId, +) => + refuseAppLinkCreators( + Object.entries(requirements).flatMap(([slot, required]) => { + if (required.scope !== "app") return []; + const candidates = links.filter((link) => link.app === app && link.requirement === slot); + if (required.cardinality === "many") return candidates; + const selected = candidates.find((link) => link.id === source) ?? candidates.at(-1); + return selected === undefined ? [] : [selected]; + }), + ); + const AccountReadsRow = Schema.Struct({ user: Schema.fromJsonString(AppAccountReads.fields.user), app: Schema.fromJsonString(AppAccountReads.fields.app), @@ -92,8 +149,8 @@ export const appAccountReads = ( const rows = yield* sql`select (select coalesce(json_agg(${json} order by l.created_at, a.id), '[]'::json)::text from ${usableLinks(sql, { subject: input.subject, apps: [input.app] })}) as "user", - (select coalesce(json_agg(${json} order by l.created_at, a.id), '[]'::json)::text - from ${usableAppLinks(sql, [input.app])}) as app`; + (select coalesce(json_agg(${checkedAppLinkJson(sql)} order by l.created_at, a.id), + '[]'::json)::text from ${appLinkRows(sql, [input.app])}) as app`; const read = (yield* Schema.decodeUnknownEffect(Schema.Array(AccountReadsRow))(rows))[0]; if (read === undefined) return yield* new StorageError(); return read; @@ -154,12 +211,17 @@ const callerRequirements = (version: AppVersion, links?: CallerLinks) => return yield* appAccountReads(yield* policyDatabase, { subject, app: version.app }); }); const reads = yield* read; + yield* checkRequirementAppLinks(version.app, version.requirements.accounts, reads.app); const own = (links: ReadonlyArray) => links .filter((link) => link.app === version.app) .map((link) => ({ requirement: link.requirement, account: link })); return { - appLinks: linkedRequirements(version.requirements.accounts, "app", own(reads.app)), + appLinks: linkedRequirements( + version.requirements.accounts, + "app", + own(reads.app.filter((link) => link.creatorRefusal === null)), + ), userLinks: linkedRequirements( version.requirements.accounts, "user", @@ -266,6 +328,17 @@ const lockLinks = (sql: SqlClient.SqlClient, app: AppId) => export const lockAppLinks = (sql: SqlClient.SqlClient, app: AppId) => lockLinks(sql, app).pipe(Effect.catchTag("SqlError", () => new StorageError())); +/** + * Serialize, in the caller's transaction, with every reuse of an app link in the organization (see + * {@link lockedChange}) and every removal of an account's app links ({@link removeAppLinksOf}). + * Take it before any app's link lock: both orders the same way, so neither deadlocks the other. + */ +export const lockAppLinkReuse = (sql: SqlClient.SqlClient, organization: string) => + sql`select pg_advisory_xact_lock(hashtextextended(${`executor-app-link-reuse:${organization}`}, 0))`.pipe( + Effect.asVoid, + Effect.catchTag("SqlError", () => new StorageError()), + ); + /** * Run a link change in one transaction under the app's link lock, after checking the caller's * access again there. The request checked it before, but the transaction can wait for the lock @@ -273,14 +346,17 @@ export const lockAppLinks = (sql: SqlClient.SqlClient, app: AppId) => * what committed meanwhile. The caller's membership row stays share-locked until the change * commits, so a role change or removal waits for it. `access` is the access the change needs to the * app; `account`, when set, the account the change links, which the caller must still be able to - * use. The change gets the checked actor, for any further check of its own under the lock. + * use. With `unlink`, `account` is the account the change unlinks, and owning it stands in for edit + * access. The change gets the checked actor, for any further check of its own under the lock. */ export const lockedChange = ( executor: Executor, input: { readonly app: AppId; - readonly access: "manage" | "use"; + readonly access: "manage" | "use" | "unlink"; readonly account?: AccountId | undefined; + /** Reuses another app's link: serialize with every other reuse in the organization. */ + readonly reuse?: boolean | undefined; }, change: (sql: SqlClient.SqlClient, actor: ResourceAuthority) => Effect.Effect, ) => @@ -288,8 +364,18 @@ export const lockedChange = ( const sql = yield* policyDatabase; return yield* executor[StorageHost].transaction( Effect.gen(function* () { + // A reuse share-locks its source links and may replace a link another reuse relies on; + // one at a time per organization, taken before any app lock, so two cannot deadlock. + if (input.reuse === true) + yield* lockAppLinkReuse(sql, (yield* CurrentOrganization).organization); yield* lockAppLinks(sql, input.app); const actor = yield* lockCurrentActor; + if (input.access === "unlink") { + // An account's owner may stop any app using it; anyone else needs edit access. + if (input.account === undefined || !(yield* ownsAccountAs(actor, input.account))) + yield* requireAppAccessAs(actor, input.app, "manage"); + return yield* change(sql, actor); + } yield* requireAppAccessAs(actor, input.app, input.access); if (input.account !== undefined) yield* requireAccountOwnerAs(actor, input.account); return yield* change(sql, actor); @@ -297,6 +383,9 @@ export const lockedChange = ( ); }); +const ownsAccountAs = (actor: ResourceAuthority, account: AccountId) => + Effect.map(ownedAccountIds([account], actor), (owned) => owned.has(account)); + /** One link: `subject` null for an app link, else the member it is linked for. */ export interface LinkKey { readonly app: AppId; @@ -427,6 +516,7 @@ export const addLink = ( webhooks: WebhookSources, input: LinkKey & { readonly linkedBy: string; + readonly reused?: boolean; readonly config?: typeof WebhookSourceConfig.Type | undefined; }, ) => @@ -463,11 +553,21 @@ export const addLink = ( ); for (const { account } of replaced) yield* removeLink(sql, webhooks, { ...input, account }); // Each link is dated when it is made, so links made in one transaction keep their order. + // An owner can explicitly take over a reused link; a reuse cannot overwrite owner consent. const inserted = yield* sql`insert into hosted_app_account_links (app, requirement, subject, account, linked_by, created_at) values (${input.app}, ${input.requirement}, ${input.subject}, ${input.account}, - ${input.linkedBy}, clock_timestamp()) - on conflict do nothing returning account`; + ${input.reused === true ? `${reusedAppLinkPrefix}${input.linkedBy}` : input.linkedBy}, + clock_timestamp()) + ${ + input.subject === null && input.reused !== true + ? sql`on conflict (app, requirement, account) where subject is null + do update set linked_by = excluded.linked_by, created_at = excluded.created_at + where starts_with(hosted_app_account_links.linked_by, ${reusedAppLinkPrefix}) + and exists(select 1 from hosted_account_access p + where p.account_id = excluded.account and p.personal_user_id = excluded.linked_by)` + : sql`on conflict do nothing` + } returning account`; if (inserted.length === 0) return replaced.length > 0; yield* syncSource(sql, webhooks, { app: input.app, @@ -524,26 +624,75 @@ export const sourceActorLost = ( const AppLinkRow = Schema.Struct({ app: AppId, requirement: Schema.String }); /** * Unlink an account from every app it serves for everyone, in one transaction under each app's - * lock, bringing each webhook source in line as unlinking it by hand does. Only for an account - * whose owner is deleting their user: leaving the organization already stops their personal - * account serving its apps, so its owner may remove those links without managing the apps. + * lock, bringing each webhook source in line as unlinking it by hand does. The caller must check + * ownership. With `app`, remove only that app's links, without disclosing its requirements. * Returns whether any link was removed. + * + * It first takes the organization's reuse lock ({@link lockAppLinkReuse}), so no reuse of one of + * these links commits a copy elsewhere while this runs: a reuse in progress finishes first and its + * link is read here, and one that starts later finds no link to reuse. The owner may still link + * the account themselves meanwhile, from another tab, so the links are read again until none is + * left; if some remain after a bounded number of passes, this fails rather than report success + * with links left behind, and the caller can try again. */ export const removeAppLinksOf = ( executor: Executor, sql: SqlClient.SqlClient, + organization: string, account: AccountId, + app?: AppId, ) => executor[StorageHost].transaction( Effect.gen(function* () { - const rows = yield* Schema.decodeUnknownEffect(Schema.Array(AppLinkRow))( - yield* sql`select distinct app, requirement from hosted_app_account_links - where account = ${account} and subject is null order by app, requirement`, - ); + yield* lockAppLinkReuse(sql, organization); let removed = false; + for (let pass = 0; pass < 8; pass++) { + const rows = yield* Schema.decodeUnknownEffect(Schema.Array(AppLinkRow))( + yield* sql`select distinct app, requirement from hosted_app_account_links + where account = ${account} and subject is null + and ${app === undefined ? sql`true` : sql`app = ${app}`} order by app, requirement`, + ); + if (rows.length === 0) return removed; + for (const row of rows) + if (yield* removeLink(sql, executor.webhooks, { ...row, account, subject: null })) + removed = true; + } + return yield* Effect.logWarning("App links kept reappearing while removing them", { + account, + }).pipe(Effect.andThen(Effect.fail(new StorageError()))); + }).pipe(Effect.catchTags(sqlFailures)), + ); + +const ReusedLinkRow = Schema.Struct({ app: AppId, requirement: Schema.String, account: AccountId }); +/** + * Before a user who leaves an organization is deleted, unlink the reused app links in the apps they + * created there, except for accounts they own. Deleting the user sets those apps' creator to null, + * which the use-time check cannot tell from an app the system created, so a reuse refused while + * the creator was a member would serve again: an account's token reaching code a demoted member + * deployed. One transaction under the organization's reuse lock, so no reuse into those apps + * commits meanwhile. Returns the links removed, for the caller to audit. + */ +export const removeReusedLinksInAppsOf = ( + executor: Executor, + sql: SqlClient.SqlClient, + organization: string, + user: string, +) => + executor[StorageHost].transaction( + Effect.gen(function* () { + yield* lockAppLinkReuse(sql, organization); + const rows = yield* Schema.decodeUnknownEffect(Schema.Array(ReusedLinkRow))( + yield* sql`select l.app, l.requirement, l.account from hosted_app_account_links l + join hosted_app_access target on target.id = l.app + left join hosted_account_access p on p.account_id = l.account + where l.subject is null and starts_with(l.linked_by, ${reusedAppLinkPrefix}) + and target.organization_id = ${organization} and target.creator_id = ${user} + and p.personal_user_id is distinct from ${user} + order by l.app, l.requirement, l.account`, + ); + const removed: Array = []; for (const row of rows) - if (yield* removeLink(sql, executor.webhooks, { ...row, account, subject: null })) - removed = true; + if (yield* removeLink(sql, executor.webhooks, { ...row, subject: null })) removed.push(row); return removed; }).pipe(Effect.catchTags(sqlFailures)), ); diff --git a/apps/hosted/server/src/implementation/mcp-approvals.ts b/apps/hosted/server/src/implementation/mcp-approvals.ts index 785d34dbf2..5b780c1fdd 100644 --- a/apps/hosted/server/src/implementation/mcp-approvals.ts +++ b/apps/hosted/server/src/implementation/mcp-approvals.ts @@ -158,6 +158,8 @@ export const hostedMcpApproval = ( StorageError: () => Effect.succeed(HttpServerResponse.empty({ status: 503 })), OrganizationForbidden: () => Effect.succeed(HttpServerResponse.empty({ status: 403 })), AccountNotFound: () => Effect.succeed(HttpServerResponse.empty({ status: 403 })), + AppLinkCreatorNotAuthorized: (error) => + Effect.succeed(HttpServerResponse.jsonUnsafe(error, { status: 409 })), ElicitationFailed: (error) => Effect.succeed( HttpServerResponse.empty({ status: error.reason === "forbidden" ? 403 : 503 }), diff --git a/apps/hosted/server/src/implementation/mcp.ts b/apps/hosted/server/src/implementation/mcp.ts index 2b47488f16..0210fbbdfe 100644 --- a/apps/hosted/server/src/implementation/mcp.ts +++ b/apps/hosted/server/src/implementation/mcp.ts @@ -405,6 +405,8 @@ export const hostedMcpBackend = Effect.gen(function* () { OrganizationForbidden: () => Effect.fail(new ElicitationFailed({ reason: "forbidden" })), AppNotFound: () => Effect.fail(new ElicitationFailed({ reason: "forbidden" })), AccountRequired: () => Effect.fail(new ElicitationFailed({ reason: "forbidden" })), + AppLinkCreatorNotAuthorized: () => + Effect.fail(new ElicitationFailed({ reason: "forbidden" })), ConnectionAccountUnlinked: () => Effect.fail(new ElicitationFailed({ reason: "forbidden" })), AppNotDeployed: () => Effect.fail(new ElicitationFailed({ reason: "forbidden" })), diff --git a/apps/hosted/server/src/implementation/resource-lifecycle.ts b/apps/hosted/server/src/implementation/resource-lifecycle.ts index 10c3dbc44a..df58d11a76 100644 --- a/apps/hosted/server/src/implementation/resource-lifecycle.ts +++ b/apps/hosted/server/src/implementation/resource-lifecycle.ts @@ -3,6 +3,7 @@ import { AccountConnectionRefused, AccountConnectionTargetChanged, AccountId, + AppLinkCreatorNotAuthorized, AccountScope, AppId, StorageError, @@ -23,11 +24,14 @@ import { requireGroupSharing } from "./group-sharing.ts"; import { addLink, appLinkApps, - appLinkedAccounts, + checkedAppLinkedAccounts, + checkRequirementAppLinks, + refuseAppLinkCreators, linkedAccounts, sourceActorLost, } from "./app-accounts.ts"; -import { usableAppLinks } from "./resource-policy.ts"; +import { recordSecurityAuditOnCommit } from "./security-audit.ts"; +import { appLinkRows, creatorRefusal } from "./resource-policy.ts"; type AppCreation = | { readonly kind: "member" } @@ -146,19 +150,22 @@ const accountAccess = (sql: SqlClient.SqlClient, accounts: readonly AccountOwner where m."organizationId" = p.organization_id and m."userId" = ${user})`; const AllowedAccount = Schema.Struct({ id: AccountId, owner: Schema.String }); type AllowedAccount = typeof AllowedAccount.Type; -const AllowedUse = Schema.Struct({ requirement: Schema.String, id: AccountId }); +const CheckedUse = Schema.Struct({ + app: AppId, + requirement: Schema.String, + id: AccountId, + creatorRefusal: Schema.NullOr(Schema.String), +}); const AppRecheck = Schema.Struct({ access: Schema.Number, accounts: Schema.fromJsonString(Schema.Array(AllowedAccount)), - appLinks: Schema.fromJsonString(Schema.Array(AllowedUse)), + appLinks: Schema.fromJsonString(Schema.Array(CheckedUse)), }); /** {@link AppRecheck} for several apps: the row of each app the actor may use, and each app link. */ const AppsRecheck = Schema.Struct({ access: Schema.fromJsonString(Schema.Array(AppId)), accounts: AppRecheck.fields.accounts, - appLinks: Schema.fromJsonString( - Schema.Array(Schema.Struct({ ...AllowedUse.fields, app: AppId })), - ), + appLinks: AppRecheck.fields.appLinks, }); // Each account must still belong to the owner the invocation resolved it under. const allowedOf = (accounts: readonly AccountOwner[], allowed: ReadonlyArray) => { @@ -168,6 +175,24 @@ const allowedOf = (accounts: readonly AccountOwner[], allowed: ReadonlyArray[2][number]; const useKey = (requirement: string, account: AccountId) => JSON.stringify([requirement, account]); +const checkedUses = ( + app: AppId, + rows: readonly (typeof CheckedUse.Type)[], + uses: readonly AppUse[], +) => + rows.filter( + (row) => + row.app === app && + uses.some((use) => use.requirement === row.requirement && use.account.id === row.id), + ); +const useRows = ( + sql: SqlClient.SqlClient, + apps: readonly AppId[], + accounts: readonly AccountId[], +) => + sql`(select coalesce(json_agg(json_build_object('app', l.app, 'requirement', l.requirement, + 'id', a.id, 'creatorRefusal', ${creatorRefusal(sql)})), '[]'::json)::text + from ${appLinkRows(sql, apps)} and ${sql.in("l.account", accounts)})`; /** * The accounts that passed every check asked of them: each user account by `users`, and each of an * account's app requirements by `uses`. An account in both lists must pass both. @@ -251,29 +276,26 @@ export const hostedResourceLifecycle = Effect.gen(function* () { const linkRows = uses.length === 0 ? sql.literal(noRows) - : sql`(select coalesce(json_agg(json_build_object('requirement', l.requirement, - 'id', a.id)), '[]'::json)::text - from ${usableAppLinks(sql, [app.id])} - and ${sql.in( - "l.account", - uses.map((use) => use.account.id), - )})`; + : useRows( + sql, + [app.id], + uses.map((use) => use.account.id), + ); return { columns: sql`(select count(*)::int from (${access}) access) as access, ${users} as accounts, ${linkRows} as "appLinks"`, passed: (row: typeof AppRecheck.Type) => - row.access !== 1 - ? undefined - : passedEvery( - accounts, - appUses, - allowedOf(owned, row.accounts), - new Set( - row.appLinks - .filter((link) => uses.some((use) => use.account.id === link.id)) - .map((link) => useKey(link.requirement, link.id)), - ), - ), + Effect.gen(function* () { + if (row.access !== 1) return yield* new StorageError(); + const links = checkedUses(app.id, row.appLinks, uses); + yield* refuseAppLinkCreators(links); + return passedEvery( + accounts, + appUses, + allowedOf(owned, row.accounts), + new Set(links.map((link) => useKey(link.requirement, link.id))), + ); + }), }; }); const lifecycle: ResourceLifecycle = { @@ -297,11 +319,13 @@ export const hostedResourceLifecycle = Effect.gen(function* () { ), ), appLinks: (input) => - appLinkedAccounts(sql, [input.app]).pipe( - Effect.map((linked) => - linked.map((link) => ({ requirement: link.requirement, account: link.id })), - ), - ), + Effect.gen(function* () { + const links = yield* checkedAppLinkedAccounts(sql, input.app); + yield* checkRequirementAppLinks(input.app, input.requirements, links, input.source); + return links + .filter((link) => link.creatorRefusal === null) + .map((link) => ({ requirement: link.requirement, account: link.id })); + }), // The actor must be a member who may use the app now: a member removed from the app's // groups stops running it, including schedules, webhook callbacks and workflow steps. The // actor's use of its user accounts, and each app requirement's app link, are rechecked in @@ -313,8 +337,7 @@ export const hostedResourceLifecycle = Effect.gen(function* () { const rows = yield* sql`select ${check.columns}`; const checked = (yield* Schema.decodeUnknownEffect(Schema.Array(AppRecheck))(rows))[0]; if (checked === undefined) return yield* new StorageError(); - const passed = check.passed(checked); - return passed === undefined ? yield* new StorageError() : passed; + return yield* check.passed(checked); }).pipe( Effect.catchTags({ SqlError: () => new StorageError(), @@ -349,44 +372,39 @@ export const hostedResourceLifecycle = Effect.gen(function* () { const linkRows = used.length === 0 ? sql.literal(noRows) - : sql`(select coalesce(json_agg(json_build_object('app', l.app, - 'requirement', l.requirement, 'id', a.id)), '[]'::json)::text - from ${usableAppLinks( - sql, - used.map((app) => app.id), - )} - and ${sql.in("l.account", [ - ...new Set(used.flatMap((app) => usesOf(app).map((use) => use.account.id))), - ])})`; + : useRows( + sql, + used.map((app) => app.id), + [...new Set(used.flatMap((app) => usesOf(app).map((use) => use.account.id)))], + ); const rows = yield* sql`select (select coalesce(json_agg(access.id), '[]'::json)::text from (${access}) access) as access, ${users} as accounts, ${linkRows} as "appLinks"`; const checked = (yield* Schema.decodeUnknownEffect(Schema.Array(AppsRecheck))(rows))[0]; if (checked === undefined) return yield* new StorageError(); - return apps.map((app): Result.Result, StorageError> => { - // Exactly one row lets the actor use the app, as `appResolving` requires. - if (checked.access.filter((id) => id === app.id).length !== 1) - return Result.fail(new StorageError()); - const uses = usesOf(app); - return Result.succeed( - passedEvery( - app.accounts, - app.appUses, - allowedOf( - user === undefined ? [] : organizationAccounts(app.accounts), - checked.accounts, - ), - new Set( - checked.appLinks - .filter( - (row) => row.app === app.id && uses.some((use) => use.account.id === row.id), - ) - .map((row) => useKey(row.requirement, row.id)), + return yield* Effect.forEach(apps, (app) => + Effect.gen(function* () { + // Exactly one row lets the actor use the app, as `appResolving` requires. + if (checked.access.filter((id) => id === app.id).length !== 1) + return Result.fail(new StorageError()); + const uses = usesOf(app); + const links = checkedUses(app.id, checked.appLinks, uses); + const refusal = yield* Effect.result(refuseAppLinkCreators(links)); + if (Result.isFailure(refusal)) return Result.fail(refusal.failure); + return Result.succeed( + passedEvery( + app.accounts, + app.appUses, + allowedOf( + user === undefined ? [] : organizationAccounts(app.accounts), + checked.accounts, + ), + new Set(links.map((row) => useKey(row.requirement, row.id))), ), - ), - ); - }); + ); + }), + ); }).pipe( Effect.catchTags({ SqlError: () => new StorageError(), @@ -424,16 +442,27 @@ export const hostedResourceLifecycle = Effect.gen(function* () { and exists(select 1 from member owner_member where owner_member."organizationId" = p.organization_id and owner_member."userId" = p.personal_user_id))`; - const rows = yield* sql`select ${users} as accounts, ${ours} as ours`; + const linkRows = + uses.length === 0 + ? sql.literal(noRows) + : useRows( + sql, + [app.id], + uses.map((use) => use.account.id), + ); + const rows = yield* sql`select ${users} as accounts, ${ours} as ours, + ${linkRows} as "appLinks"`; const checked = (yield* Schema.decodeUnknownEffect( Schema.Array( Schema.Struct({ accounts: AppRecheck.fields.accounts, + appLinks: AppRecheck.fields.appLinks, ours: Schema.fromJsonString(Schema.Array(AccountId)), }), ), )(rows))[0]; if (checked === undefined) return yield* new StorageError(); + yield* refuseAppLinkCreators(checkedUses(app.id, checked.appLinks, uses)); const owner = new Set(checked.ours); return passedEvery( accounts, @@ -697,13 +726,29 @@ export const hostedResourceLifecycle = Effect.gen(function* () { if (!usable.has(input.account.id)) return yield* new AccountConnectionRefused(target); } const sources = yield* webhooks; - yield* addLink(sql, sources, { + const changed = yield* addLink(sql, sources, { app: input.app, requirement: input.requirement, account: input.account.id, subject: input.scope === "app" ? null : user, linkedBy: user, }).pipe(Effect.mapError(() => new StorageError())); + // A connection completing for an app requirement links the new account for everyone, + // recorded once the account save that makes the link commits. + if (input.scope === "app") + yield* recordSecurityAuditOnCommit({ + action: "app_account.link", + outcome: "success", + target: { kind: "account", id: input.account.id }, + detail: { + app: input.app, + authority: "owner", + via: "connect", + changed: String(changed), + }, + user, + organization, + }); }).pipe( Effect.catchTags({ SqlError: () => new StorageError(), diff --git a/apps/hosted/server/src/implementation/resource-policy.ts b/apps/hosted/server/src/implementation/resource-policy.ts index ce52a56498..162d79eab3 100644 --- a/apps/hosted/server/src/implementation/resource-policy.ts +++ b/apps/hosted/server/src/implementation/resource-policy.ts @@ -9,6 +9,8 @@ import { type App, } from "@executor-js/sdk/core"; import type { OAuthClientAccess } from "../contracts/oauth-clients.ts"; +import { permittedAppIds, permitsAction } from "@executor-js/authorization"; +import { CurrentPolicy } from "../contracts/authorization.ts"; import { Effect, Schema } from "effect"; import type { SqlClient } from "effect/sql"; import { GroupDatabase, GroupId, GroupMemberId } from "../contracts/groups.ts"; @@ -229,7 +231,20 @@ export const usableLinks = ( * first, as in {@link usableLinks}. */ export const usableAppLinks = (sql: SqlClient.SqlClient, apps: readonly AppId[]) => - sql`(select l.app, l.requirement, l.account, l.created_at, app.owner + sql`${appLinkRows(sql, apps)} and ${creatorMayUseAppLink(sql)}`; + +// Links inherited from the shared-account model can also have a different linker and owner. +// Mark only explicit reuses, preserving every pre-PR link without changing the schema. +export const reusedAppLinkPrefix = "reuse:"; + +const creatorMayUseAppLink = (sql: SqlClient.SqlClient) => + sql`(not starts_with(l.linked_by, ${reusedAppLinkPrefix}) + or (target.creator_id is not null and target.creator_id = p.personal_user_id) + or coalesce(creator_member.role in ('owner', 'admin'), false))`; + +/** Both usable and creator-refused links, excluding accounts whose owner left. */ +export const appLinkRows = (sql: SqlClient.SqlClient, apps: readonly AppId[]) => + sql`(select l.app, l.requirement, l.account, l.created_at, l.linked_by, app.owner from hosted_app_account_links l join executor_apps app on app.id = l.app where l.subject is null and ${sql.in("l.app", apps)} @@ -237,6 +252,10 @@ export const usableAppLinks = (sql: SqlClient.SqlClient, apps: readonly AppId[]) join executor_accounts a on a.id = l.account and a.owner = l.owner join hosted_account_access p on p.account_id = a.id and a.owner = 'organization:' || p.organization_id + join hosted_app_access target on target.id = l.app + and target.organization_id = p.organization_id + left join member creator_member on creator_member."organizationId" = target.organization_id + and creator_member."userId" = target.creator_id where exists(select 1 from member owner_member where owner_member."organizationId" = p.organization_id and owner_member."userId" = p.personal_user_id)`; @@ -244,6 +263,15 @@ export const usableAppLinks = (sql: SqlClient.SqlClient, apps: readonly AppId[]) export const linkedAccountJson = `json_build_object('app', l.app, 'requirement', l.requirement, 'id', a.id, 'provider', a.provider, 'label', a.label, 'description', a.description)`; +/** Null when usable; otherwise a private audit key for this link and creator membership state. */ +export const creatorRefusal = (sql: SqlClient.SqlClient) => + sql`case when ${creatorMayUseAppLink(sql)} then null else + json_build_array(l.created_at, creator_member.id, creator_member.xmin::text)::text end`; + +export const checkedAppLinkJson = (sql: SqlClient.SqlClient) => + sql`(${sql.literal(linkedAccountJson)}::jsonb || + jsonb_build_object('creatorRefusal', ${creatorRefusal(sql)}))`; + /** One account linked to an app requirement, which the caller or the app may still use. */ export const LinkedAccount = Schema.Struct({ app: AppId, @@ -254,10 +282,14 @@ export const LinkedAccount = Schema.Struct({ description: Schema.NullOr(Schema.String), }); export type LinkedAccount = typeof LinkedAccount.Type; -/** The caller's usable user links to an app and the app's usable app links, oldest first. */ +export const CheckedAppLink = Schema.Struct({ + ...LinkedAccount.fields, + creatorRefusal: Schema.NullOr(Schema.String), +}); +/** Usable user links and app links checked against creator authority, oldest first. */ export const AppAccountReads = Schema.Struct({ user: Schema.Array(LinkedAccount), - app: Schema.Array(LinkedAccount), + app: Schema.Array(CheckedAppLink), }); export type AppAccountReads = typeof AppAccountReads.Type; @@ -288,8 +320,8 @@ export const resourceAuthorityForLinks = ( where g.app_id = p.id and gm.member_id = m.id) as granted, (select coalesce(json_agg(${sql.literal(linkedAccountJson)} order by l.created_at, a.id), '[]'::json)::text from ${usableLinks(sql, { subject: user, apps: [app] })}) as "userLinks", - (select coalesce(json_agg(${sql.literal(linkedAccountJson)} order by l.created_at, a.id), - '[]'::json)::text from ${usableAppLinks(sql, [app])}) as "appLinks" + (select coalesce(json_agg(${checkedAppLinkJson(sql)} order by l.created_at, a.id), + '[]'::json)::text from ${appLinkRows(sql, [app])}) as "appLinks" from member m left join hosted_app_access p on p.id = ${app} and p.organization_id = m."organizationId" where m."organizationId" = ${organization} and m."userId" = ${user}`; @@ -316,7 +348,7 @@ export const resourceAuthorityForLinks = ( user: Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Array(LinkedAccount)))( row.userLinks, ), - app: Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Array(LinkedAccount)))( + app: Schema.decodeUnknownEffect(Schema.fromJsonString(AppAccountReads.fields.app))( row.appLinks, ), }).pipe(Effect.mapError(() => new StorageError())), @@ -329,7 +361,7 @@ const MemberPoliciesLinks = Schema.Struct({ ...Membership.fields, policies: Schema.fromJsonString(Schema.Array(AppPolicy)), userLinks: Schema.fromJsonString(Schema.Array(LinkedAccount)), - appLinks: Schema.fromJsonString(Schema.Array(LinkedAccount)), + appLinks: Schema.fromJsonString(AppAccountReads.fields.app), }); /** * {@link resourceAuthorityForLinks} for many apps, such as every app a search reads: membership, @@ -358,8 +390,8 @@ export const resourceAuthorityForAppsWithLinks = ( where ${sql.in("p.id", apps)} and p.organization_id = m."organizationId") as policies, (select coalesce(json_agg(${json} order by l.created_at, a.id), '[]'::json)::text from ${usableLinks(sql, { subject: user, apps })}) as "userLinks", - (select coalesce(json_agg(${json} order by l.created_at, a.id), '[]'::json)::text - from ${usableAppLinks(sql, apps)}) as "appLinks" + (select coalesce(json_agg(${checkedAppLinkJson(sql)} order by l.created_at, a.id), + '[]'::json)::text from ${appLinkRows(sql, apps)}) as "appLinks" from member m where m."organizationId" = ${organization} and m."userId" = ${user}`; const found = yield* Schema.decodeUnknownEffect(Schema.Array(MemberPoliciesLinks))(rows); @@ -399,8 +431,8 @@ export const resourceAuthorityForLinksOfApps = ( (select coalesce(json_agg(${sql.literal(linkedAccountJson)} order by l.created_at, a.id), '[]'::json)::text from ${usableLinks(sql, { subject: user, apps })} and l.app = ap.id) as "userLinks", - (select coalesce(json_agg(${sql.literal(linkedAccountJson)} order by l.created_at, a.id), - '[]'::json)::text from ${usableAppLinks(sql, apps)} and l.app = ap.id) as "appLinks" + (select coalesce(json_agg(${checkedAppLinkJson(sql)} order by l.created_at, a.id), + '[]'::json)::text from ${appLinkRows(sql, apps)} and l.app = ap.id) as "appLinks" from member m left join hosted_app_access ap on ${sql.in("ap.id", apps)} and ap.organization_id = m."organizationId" @@ -433,7 +465,9 @@ export const resourceAuthorityForLinksOfApps = ( ), accounts: Effect.all({ user: links(row.userLinks), - app: links(row.appLinks), + app: Schema.decodeUnknownEffect( + Schema.fromJsonString(AppAccountReads.fields.app), + )(row.appLinks), }).pipe(Effect.mapError(() => new StorageError())), }, ] as const, @@ -596,6 +630,128 @@ export const requireAccountOwnerAs = (actor: ResourceAuthority, account: Account if (!(yield* ownedAccountIds([account], actor)).has(account)) return yield* new OrganizationForbidden(); }); +/** + * What lets an actor link an account to an app's own requirement for everyone: `owner` for their + * own account, `app_link` for an account an administrator reuses from another app's link. + */ +export type AppLinkAuthority = "owner" | "app_link"; +const ReusableLink = Schema.Struct({ + account: AccountId, + app: AppId, + owner: Principal.fields.userId, +}); +const TargetEditors = Schema.Struct({ + creator: Schema.NullOr(Principal.fields.userId), + role: Schema.NullOr(OrganizationRole), +}); +/** + * The accounts among `accounts` the actor may link to `app`'s own requirements for everyone, with + * what lets them. An account's owner may link it anywhere they edit. An organization owner or + * administrator may also reuse an account its owner already linked for everyone to an app they + * can edit, whose owner is still a member: editing that app already lets them run code with the + * account, so they gain no use of it. + * + * Reuse never reaches a member who could not already use the account: it is refused when the + * target app's creator is a member other than the account's owner, since that member edits the + * app and so could deploy code that reads the credential. A creator who has left the organization + * counts as a member: invited back, they would edit the app again. An app without a creator takes + * no reuse either: the system's apps and apps whose creator's user was deleted look the same, and + * the latter may run code a demoted creator deployed. Members never reuse an account. At use + * time the same creator rule applies: demotion, departure or a missing creator stops a reused + * link serving, and promotion can restore it. Owner-made and legacy links do not depend on the + * creator's role. The account stays its owner's; nobody else may use it directly or link it for + * themselves. + * + * With `lock`, the app link rows a reuse relies on and the target's access row stay share-locked + * until the transaction ends, so unlinking a source or changing the target's access waits for the + * link. Source apps must also be readable and editable through this request's credential policy. + * The owner's membership is not locked: leaving independently stops the account serving apps + * through usableAppLinks. + */ +export const appLinkableAccountsAs = ( + actor: ResourceAuthority, + app: AppId, + accounts: readonly AccountId[], + lock = false, +) => + Effect.gen(function* () { + const linkable = new Map(); + if (accounts.length === 0) return linkable; + for (const account of yield* ownedAccountIds(accounts, actor)) linkable.set(account, "owner"); + const others = accounts.filter((account) => !linkable.has(account)); + if (others.length === 0 || actor.role === "member") return linkable; + const policy = yield* CurrentPolicy; + if (!permitsAction(policy, "read") || !permitsAction(policy, "manage")) return linkable; + const permitted = permittedAppIds(policy); + const sql = yield* policyDatabase; + const [target] = yield* Schema.decodeUnknownEffect(Schema.Array(TargetEditors))( + yield* sql`select t.creator_id as creator, m.role + from hosted_app_access t + left join member m on m."organizationId" = t.organization_id and m."userId" = t.creator_id + where t.id = ${app} and t.organization_id = ${actor.organization}${lock ? sql` for share of t` : sql``}`, + ); + if (target === undefined) return linkable; + const rows = yield* sql`select l.account, l.app, p.personal_user_id as owner + from hosted_app_account_links l + join executor_apps app on app.id = l.app + join executor_accounts a on a.id = l.account and a.owner = app.owner + join hosted_account_access p on p.account_id = a.id + and a.owner = 'organization:' || p.organization_id + join hosted_app_access target on target.id = l.app + and target.organization_id = p.organization_id + left join member creator_member on creator_member."organizationId" = p.organization_id + and creator_member."userId" = target.creator_id + where l.subject is null and ${sql.in("l.account", others)} + and p.organization_id = ${actor.organization} + and ${creatorMayUseAppLink(sql)} + and exists(select 1 from member owner_member + where owner_member."organizationId" = p.organization_id + and owner_member."userId" = p.personal_user_id)${lock ? sql` for share of l` : sql``}`; + const links = yield* Schema.decodeUnknownEffect(Schema.Array(ReusableLink))(rows); + const editable = new Set( + (yield* applicationAccesses([...new Set(links.map((link) => link.app))], actor)) + .filter( + (access) => + access.canManage && (permitted === undefined || permitted.includes(access.app)), + ) + .map((access) => access.app), + ); + for (const link of links) + if ( + editable.has(link.app) && + (target.role === "owner" || target.role === "admin" || target.creator === link.owner) + ) + linkable.set(link.account, "app_link"); + return linkable; + }).pipe( + Effect.catchTags({ SqlError: () => new StorageError(), SchemaError: () => new StorageError() }), + ); +/** The accounts of a list the request's actor may link to `app` for everyone. */ +export const appLinkableAccounts = (app: AppId, accounts: readonly Account[]) => + Effect.gen(function* () { + const actor = yield* currentResourceAuthority; + const linkable = yield* appLinkableAccountsAs( + actor, + app, + accounts.map((account) => account.id), + ); + return accounts.filter((account) => linkable.has(account.id)); + }); +/** + * What lets the actor link this account to `app` for everyone, or `OrganizationForbidden` when + * nothing does; see {@link appLinkableAccountsAs}. + */ +export const requireAppLinkableAs = ( + actor: ResourceAuthority, + app: AppId, + account: AccountId, + lock = false, +) => + Effect.gen(function* () { + const authority = (yield* appLinkableAccountsAs(actor, app, [account], lock)).get(account); + if (authority === undefined) return yield* new OrganizationForbidden(); + return authority; + }); /** Missing or denied policies are omitted; storage failures remain visible rather than empty lists. */ export const visibleAccounts = (accounts: readonly Account[]) => Effect.flatMap(currentResourceAuthority, (actor) => visibleAccountsAs(actor, accounts)); diff --git a/apps/hosted/server/src/implementation/security-audit.ts b/apps/hosted/server/src/implementation/security-audit.ts index 74937a495a..22d5060c76 100644 --- a/apps/hosted/server/src/implementation/security-audit.ts +++ b/apps/hosted/server/src/implementation/security-audit.ts @@ -3,6 +3,7 @@ * request's span knows about the client and its credential, so it can be read on its own. */ import { currentRequestSpan } from "@executor-js/telemetry/http"; +import type { AccountId, AppId } from "@executor-js/sdk/core"; import { Cause, Context, Effect, Exit, Option } from "effect"; import { HttpServerRequest } from "effect/http"; import { CurrentUserId } from "../contracts/auth.ts"; @@ -61,22 +62,61 @@ export const recordSecurityAudit = (event: SecurityAuditEvent) => yield* Effect.logInfo("security.audit").pipe(Effect.annotateLogs(annotations)); }); +/** + * Telemetry is best effort. Keep a bounded, per-process/isolate set of recently reported link + * states, not a permission cache. Repeated calls for one refusal do not each emit a record. + * The database supplies the link and creator membership revision; a later demotion is new even + * if nobody tried to use the link while the creator was promoted. + */ +const reportedAppLinkRefusals = new Map(); +export const recordAppLinkCreatorRefusal = (link: { + readonly app: AppId; + readonly id: AccountId; + readonly requirement: string; + readonly creatorRefusal: string; +}) => + Effect.suspend(() => { + const key = JSON.stringify([link.app, link.requirement, link.id]); + const previous = reportedAppLinkRefusals.get(key); + reportedAppLinkRefusals.delete(key); + reportedAppLinkRefusals.set(key, link.creatorRefusal); + if (previous === link.creatorRefusal) return Effect.void; + if (reportedAppLinkRefusals.size > 4096) { + const oldest = reportedAppLinkRefusals.keys().next().value; + if (oldest !== undefined) reportedAppLinkRefusals.delete(oldest); + } + return recordSecurityAudit({ + action: "app_account.use_refused", + outcome: "failure", + target: { kind: "account", id: link.id }, + detail: { + app: link.app, + requirement: link.requirement, + reason: "creator_not_admin_or_owner", + }, + }); + }); + /** * Record an action a product handler performs, with its outcome. A refusal carries the request's * `auth.failure.reason` when a check recorded one. A request that ends before the action does is * `interrupted`: a change it started may still be stored. Only a completed action names its target, * by `target.id` of its result, a record the action found authorized or stored; what the request - * asked for can be any text. + * asked for can be any text. `succeeded` adds detail read from a completed action's result. A + * `detail` function is read once the action ends, so it can name what the action resolved before + * it was refused, never what the request named. */ export const audited = ( action: SecurityAuditAction, target: { readonly kind: string; readonly id: (value: A) => string | undefined }, - detail?: Readonly>, + detail?: Readonly> | (() => Readonly>), + succeeded?: (value: A) => Readonly>, ) => (effect: Effect.Effect) => - Effect.onExit(effect, (exit) => - recordSecurityAudit({ + Effect.onExit(effect, (exit) => { + const ended = typeof detail === "function" ? detail() : detail; + return recordSecurityAudit({ action, outcome: Exit.isSuccess(exit) ? "success" @@ -84,6 +124,47 @@ export const audited = ? "interrupted" : "failure", target: { kind: target.kind, id: Exit.isSuccess(exit) ? target.id(exit.value) : undefined }, - detail, - }), + detail: + Exit.isSuccess(exit) && succeeded !== undefined + ? { ...ended, ...succeeded(exit.value) } + : ended, + }); + }); + +/** + * The records hooks inside a storage transaction made, which a request that starts the transaction + * collects and records once it returns, so after the transaction committed. + */ +export class CommittedAudits extends Context.Service< + CommittedAudits, + { readonly pending: SecurityAuditEvent[] } +>()("hosted/CommittedAudits") {} + +/** + * Record `event` once the transaction it describes commits: held for the request that collects + * {@link CommittedAudits}, or recorded now where no request does. + */ +export const recordSecurityAuditOnCommit = (event: SecurityAuditEvent) => + Effect.flatMap( + Effect.serviceOption(CommittedAudits), + Option.match({ + onNone: () => recordSecurityAudit(event), + onSome: (audits) => + Effect.sync(() => { + audits.pending.push(event); + }), + }), + ); + +/** + * Run `effect`, which commits its transactions before it returns, collecting the records its hooks + * made; record them once it succeeds. A failed or interrupted run rolled back what they describe. + */ +export const withCommittedAudits = (effect: Effect.Effect) => + Effect.suspend(() => { + const audits = { pending: [] as SecurityAuditEvent[] }; + return effect.pipe( + Effect.provideService(CommittedAudits, audits), + Effect.tap(() => Effect.forEach(audits.pending, recordSecurityAudit, { discard: true })), ); + }); diff --git a/apps/hosted/server/src/implementation/user-removal.ts b/apps/hosted/server/src/implementation/user-removal.ts index 6a91f8aa78..4df88794eb 100644 --- a/apps/hosted/server/src/implementation/user-removal.ts +++ b/apps/hosted/server/src/implementation/user-removal.ts @@ -23,7 +23,8 @@ import { type UserRemovalOutcome, } from "../contracts/user-removal.ts"; import { removeAccount } from "./accounts.ts"; -import { removeAppLinksOf } from "./app-accounts.ts"; +import { removeAppLinksOf, removeReusedLinksInAppsOf } from "./app-accounts.ts"; +import { recordSecurityAudit } from "./security-audit.ts"; import { ScheduleWakeup } from "../contracts/schedules.ts"; import { policyDatabase } from "./resource-policy.ts"; @@ -123,7 +124,7 @@ export const removeUser = Effect.gen(function* () { (entry) => entry.organization === organization.id, )) { // Leaving already stops the account serving its apps; deleting it removes those links. - if (yield* removeAppLinksOf(executor, sql, account)) + if (yield* removeAppLinksOf(executor, sql, organization.id, account)) yield* Effect.serviceOption(ScheduleWakeup).pipe( Effect.flatMap(Option.match({ onNone: () => Effect.void, onSome: (wake) => wake })), ); @@ -140,6 +141,29 @@ export const removeUser = Effect.gen(function* () { ); } } + // Deleting the user nulls the creator of the apps they made, in every organization they created + // one in, including those they already left; unlink the reuses in those apps first. + const created = yield* sql`select distinct organization_id as id from hosted_app_access + where creator_id = ${user} order by organization_id`.pipe( + Effect.flatMap(Schema.decodeUnknownEffect(Schema.Array(Schema.Struct({ id: OrganizationId })))), + Effect.mapError(() => new StorageError()), + ); + for (const organization of created) { + const removed = yield* removeReusedLinksInAppsOf(executor, sql, organization.id, user); + for (const link of removed) + yield* recordSecurityAudit({ + action: "app_account.unlink", + outcome: "success", + target: { kind: "account", id: link.account }, + detail: { app: link.app, authority: "creator_removal" }, + user, + organization: organization.id, + }); + if (removed.length > 0) + yield* Effect.serviceOption(ScheduleWakeup).pipe( + Effect.flatMap(Option.match({ onNone: () => Effect.void, onSome: (wake) => wake })), + ); + } const removals = yield* OrganizationRemovals; for (const organization of deleted) yield* removals.begin(organization.id, organization.id); yield* (yield* SessionAuthentication).removeUser(user); diff --git a/apps/hosted/web/src/contracts/errors.ts b/apps/hosted/web/src/contracts/errors.ts index f668c52bd2..c74c4afc8b 100644 --- a/apps/hosted/web/src/contracts/errors.ts +++ b/apps/hosted/web/src/contracts/errors.ts @@ -27,6 +27,7 @@ const errorMessage = Match.type().pipe( ScheduleNotFound: () => "This schedule or run is no longer available.", ConnectionNotFound: () => "This connection was revoked or no longer exists.", ConnectionAccountUnlinked: (error) => error.description, + AppLinkCreatorNotAuthorized: (error) => error.description, ConnectionIdTaken: () => "This connection could not be created. Close the form and try again.", ConnectedAgentNotFound: () => "This agent is no longer connected.", ConnectionKeyNotFound: () => "This key was already revoked.", diff --git a/apps/local/server/src/contracts/dashboard.ts b/apps/local/server/src/contracts/dashboard.ts index 307583b058..8436bee25d 100644 --- a/apps/local/server/src/contracts/dashboard.ts +++ b/apps/local/server/src/contracts/dashboard.ts @@ -30,6 +30,7 @@ import { CredentialCheck, AccountNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountSelectionInvalid, MarkdownUnavailable, App, @@ -494,6 +495,7 @@ const toolErrors = [ AppProviderFailed, AccountNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, ToolDiscoveryTimedOut, @@ -649,6 +651,7 @@ const dashboardApi = HttpApi.make("local-dashboard").add( AppProviderFailed, AccountNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, OAuthReconnectRequired, diff --git a/apps/local/web/src/contracts/errors.ts b/apps/local/web/src/contracts/errors.ts index 5e7a0837b6..66b6254a61 100644 --- a/apps/local/web/src/contracts/errors.ts +++ b/apps/local/web/src/contracts/errors.ts @@ -174,6 +174,7 @@ const errorMessage = Match.type().pipe( "Connect an account first", "This app needs an account before its live tools can load. Connect one from the Accounts tab.", ), + AppLinkCreatorNotAuthorized: (error) => message(error.title, error.description), AccountChoiceUnsupported: () => message( "Accounts are not chosen per call", diff --git a/e2e/ci-selection.ts b/e2e/ci-selection.ts index 64773672a4..d699480c41 100644 --- a/e2e/ci-selection.ts +++ b/e2e/ci-selection.ts @@ -205,7 +205,7 @@ const jobs = { runner: 4, pattern: - "A Better Auth query|an app link that waits for the admin.s membership while a demotion commits|two first checks of an account that write its check row|saving an account beside 100,000 accounts, links and webhook sources", + "A Better Auth query|an app link that waits for the admin.s membership while a demotion commits|a reused hosted app account refused after its app.s creator is demoted|two first checks of an account that write its check row|saving an account beside 100,000 accounts, links and webhook sources", }, // Removal recovery needs the Cloud's one recovery alarm and its removal job to itself: the job // starts every pending removal on the Cloud, and the scenario locks the start records' table to diff --git a/e2e/support/app-links.ts b/e2e/support/app-links.ts index 7f94b13e6f..cdf1270a1f 100644 --- a/e2e/support/app-links.ts +++ b/e2e/support/app-links.ts @@ -369,6 +369,8 @@ export const appLinksFixture = (files: ReadonlyArray<{ path: string; content: st get, call, group, + /** Remove an app deployed outside `another` on cleanup, before the fixture's accounts. */ + adopt: (appPath: string) => Effect.sync(() => others.push(appPath)), /** Record an account saved outside `connect` for cleanup. */ saved: (actor: Session, id: string) => Effect.sync(() => saved.push({ actor, id })), }; diff --git a/e2e/test-plan.ts b/e2e/test-plan.ts index 5d7a2251d7..6ea17f0c6f 100644 --- a/e2e/test-plan.ts +++ b/e2e/test-plan.ts @@ -9442,6 +9442,83 @@ export const scenarios = plan({ cloud: scheduled, }, }, + hostedAppLinkReuse: { + fixtures: "actors", + file: "app-account-app-links.spec.ts", + title: + "a hosted administrator links for everyone an account they do not own only once it serves an app they can edit; its owner may still unlink it from any app", + targets: { + local: na("Hosted managers and personal accounts; Local has one user."), + "self-host": scheduled, + cloud: scheduled, + }, + }, + hostedAppLinkReuseChain: { + fixtures: "actors", + file: "app-account-app-links.spec.ts", + title: + "a hosted administrator cannot reuse an owner's account into an app a member edits, so the member can never chain it into their own apps or read its secret, before or after an unlink", + targets: { + local: na("Hosted managers and personal accounts; Local has one user."), + "self-host": scheduled, + cloud: scheduled, + }, + }, + hostedAppLinkReuseDeparted: { + fixtures: "actors", + file: "app-account-app-links.spec.ts", + title: + "a hosted administrator's reuse of an account stops serving once its owner leaves, and is refused into an app whose creator left, as if they were still a member", + targets: { + local: na("Hosted managers and personal accounts; Local has one user."), + "self-host": scheduled, + cloud: scheduled, + }, + }, + hostedAppLinkAudit: { + fixtures: "actors", + file: "app-account-app-links.spec.ts", + title: + "hosted app links and unlinks, a connection's included, leave audit records naming the account, the app found and the authority; a refused one names no app", + targets: { + local: na("Local has no hosted audit records."), + "self-host": scheduled, + cloud: scheduled, + }, + }, + hostedAppLinkCreatorDemotion: { + fixtures: "actors", + file: "app-account-app-links.spec.ts", + title: + "a reused hosted app account stops serving when the app's creator is demoted and serves again on promotion, while an owner-made link keeps serving", + targets: { + local: na("Local has no organization roles."), + "self-host": scheduled, + cloud: scheduled, + }, + }, + hostedAppLinkCreatorRemoval: { + fixtures: "actors", + file: "app-account-app-links.spec.ts", + title: + "a reused hosted app account refused after its app's creator is demoted stays refused once the creator, removed from the organization, deletes their user, and the unlink is audited", + targets: { + local: na("Local has no organization roles."), + "self-host": na("Self-host users are managed by the instance operator."), + cloud: managedCloud, + }, + }, + hostedAppLinkCreatorDemotionDuringRefresh: { + fixtures: "actors", + file: "app-account-app-links.spec.ts", + title: + "a reused hosted app account refuses a call already waiting on OAuth refresh when the creator is demoted", + targets: { + local: na("Local has no organization roles."), + "self-host": scheduled, + cloud: na("Uses a scoped loopback OAuth issuer."), + }, + }, hostedAppLinkOAuthConnections: { fixtures: "actors", file: "app-account-app-links.spec.ts", @@ -9464,6 +9541,17 @@ export const scenarios = plan({ local: na("Local has no organization roles."), }, }, + hostedAppLinkReuseHeldDemotion: { + fixtures: "actors", + file: "app-account-link-held-access.spec.ts", + title: + "an administrator's reuse of an owner's account waiting for its checks is refused when they are demoted meanwhile, and a demoted administrator can neither reuse nor unlink it", + targets: { + "self-host": scheduled, + cloud: na("The statement hold is mounted by the self-host test entry point."), + local: na("Local has no organization roles."), + }, + }, hostedDepartedMemberAppLink: { fixtures: "actors", file: "departed-member-app-links.spec.ts", diff --git a/e2e/tests/app-account-app-links.spec.ts b/e2e/tests/app-account-app-links.spec.ts index 96e3e3c22b..af2152f10f 100644 --- a/e2e/tests/app-account-app-links.spec.ts +++ b/e2e/tests/app-account-app-links.spec.ts @@ -6,20 +6,24 @@ * override them. */ import { expect, layer } from "@effect/vitest"; -import { Effect, Schema } from "effect"; +import { Effect, Fiber, Schedule, Schema } from "effect"; import { FetchHttpClient, HttpClient } from "effect/http"; -import { randomUUID } from "node:crypto"; +import { randomBytes, randomUUID } from "node:crypto"; import { Api, body, type Session } from "../support/api.ts"; import { Actors } from "../support/actors.ts"; import { HostedLive, withHostedCase } from "../support/case.ts"; +import { cloudLocks } from "../support/cloud-locks.ts"; import { Resource } from "../support/contracts.ts"; +import { Telemetry } from "../support/evidence.ts"; import { oauthSetupIssuer } from "../support/oauth-setup-issuer.ts"; +import { settleWebhookSources } from "../support/webhook-sources.ts"; import { appsManifest } from "../support/apps-release.ts"; import { accountLinks, linkAccount, linkAccountResponse, linkedAccountIds, + requirementPath, } from "../support/app-accounts.ts"; import { LinkRequirement, @@ -61,6 +65,15 @@ const Who = Schema.Struct({ extra: Schema.Array(Schema.String), mail: Schema.String, }); +/** Another app whose one app requirement takes the team provider, reporting the account it runs with. */ +const teamAppSource = `import { defineApp, defineProvider, secrets, query, object, string, router, type QueryContext } from "apps"; +const team = ${teamProvider}; +const requirements = { app: { accounts: { team } } }; +const who = query({ input: object({}) }, async (ctx: QueryContext) => ({ + team: ctx.app.accounts.team.id, + token: ctx.app.accounts.team.fields.token, +})); +export default defineApp(requirements, async () => ({ tools: router({ who }) }));`; /** An app whose one key requirement is in `scope`. */ const scopedKeyApp = ( scope: "app" | "user", @@ -75,6 +88,36 @@ const scopedOAuthApp = ( const service = defineProvider({ name: "App link connection OAuth fixture", auth: { oauth: oauth2({ discover: ${JSON.stringify(`${issuer}/mcp`)} }) } }); export default defineApp({ ${scope}: { accounts: { service } } }, async () => ({ tools: router({}) }));`; const SignIn = Schema.Struct({ authorizationUrl: Schema.String }); +const TeamWho = Schema.Struct({ team: Schema.String, token: Schema.String }); +const SessionIdentity = Schema.Struct({ user: Schema.Struct({ email: Schema.String }) }); +const Memberships = Schema.Struct({ + members: Schema.Array( + Schema.Struct({ ...Resource.fields, ...SessionIdentity.fields, userId: Schema.String }), + ), +}); +const LogRecords = Schema.fromJsonString( + Schema.Struct({ + data: Schema.Array( + Schema.Struct({ + body: Schema.String, + attributes: Schema.Record(Schema.String, Schema.Unknown), + }), + ), + }), +); +/** The audit attributes of the one `security.audit` record for `action` in a trace's logs. */ +const auditOf = (logs: string, action: string) => { + const records = Schema.decodeUnknownSync(LogRecords)(logs).data.filter( + (record) => record.body === "security.audit" && record.attributes["audit.action"] === action, + ); + expect(records, `one ${action} record`).toHaveLength(1); + const attributes = records[0]?.attributes ?? {}; + return Object.fromEntries(Object.entries(attributes).filter(([key]) => key.startsWith("audit."))); +}; +const traced = () => { + const traceId = randomBytes(16).toString("hex"); + return { traceId, traceparent: `00-${traceId}-${randomBytes(8).toString("hex")}-01` }; +}; const LinkedRefusal = Schema.Struct({ _tag: Schema.String, apps: Schema.Array(Schema.String) }); layer(HostedLive, { excludeTestServices: true })("App account app links", (it) => { @@ -259,8 +302,11 @@ layer(HostedLive, { excludeTestServices: true })("App account app links", (it) = expect( yield* refused(yield* link(actors.owner, "team", ownerMail), 422, "RequirementMismatch"), ).toMatchObject({ requirement: "team", scope: "app", reason: "provider" }); - // The admin manages the app but cannot use the owner's personal account. - expect((yield* link(actors.admin, "extra", first)).status).toBe(403); + // The admin manages the app but does not own the owner's personal account. It is already + // linked here for everyone, so the admin may link it to another requirement of the app, as + // the app already runs with it; they still cannot read the account itself. + yield* ok(yield* link(actors.admin, "extra", first)); + yield* ok(yield* unlink(actors.admin, "extra", first)); // An account linked for everyone cannot be deleted until it is unlinked. const deleting = yield* api.request(actors.owner, "DELETE", `${prefix}/accounts/${first}`); @@ -432,6 +478,900 @@ layer(HostedLive, { excludeTestServices: true })("App account app links", (it) = }), ), ); + it.effect(scenarios.hostedAppLinkReuse.title, (context) => + withHostedCase( + context, + Effect.gen(function* () { + const fixture = yield* appLinksFixture([{ path: "index.ts", content: source }]); + const { api, actors, prefix, connect, start, submit } = fixture; + const appLink = (actor: Session, appPath: string, account: string) => + linkAccountResponse(actor, appPath, "team", account, "app"); + const appUnlink = (actor: Session, appPath: string, account: string) => + api.request( + actor, + "DELETE", + `${appPath}/accounts/app/team/${encodeURIComponent(account)}`, + ); + const choices = (actor: Session, appPath: string) => + api.request(actor, "GET", `${appPath}/accounts/app/team`).pipe( + Effect.flatMap((response) => okBody(LinkRequirement, response)), + Effect.map((read) => read.accounts.map((entry) => [entry.account.id, entry.linked])), + ); + const who = (actor: Session, appPath: string) => + api + .request(actor, "POST", `${appPath}/tools/call`, { + tool: "who", + kind: "query", + input: {}, + }) + .pipe( + Effect.flatMap((response) => + okBody(Schema.Struct({ team: Schema.String }), response), + ), + ); + + // The owner links their own team account for everyone to the fixture's app. A second app + // the admin can edit takes the same provider; nothing fills its requirement yet. + const shared = yield* connect(actors.owner, "app", "team", "team-shared"); + const second = yield* fixture.another( + teamAppSource.replace( + "accounts: { team }", + "accounts: { team, privateTeam: team.many() }", + ), + ); + yield* refused( + yield* api.request(actors.member, "POST", `${second}/tools/call`, { + tool: "who", + kind: "query", + input: {}, + }), + 409, + "AccountRequired", + ); + + // The same admin's credential narrowed to the target cannot borrow authority from the + // source app outside its selection, for either listing or linking. + const connection = yield* okBody( + Resource, + yield* api.request(actors.admin, "POST", `${prefix}/mcp-connections`, { + id: randomUUID(), + name: "App reuse scope", + apps: [ + { + app: second.slice(second.lastIndexOf("/") + 1), + runsAs: [{ kind: "app" }], + tools: { kind: "all" }, + }, + ], + }), + ); + yield* Effect.addFinalizer(() => + api + .request(actors.admin, "POST", `${prefix}/mcp-connections/${connection.id}/revoke`) + .pipe(Effect.asVoid, Effect.orDie), + ); + // A host-attached calling connection narrows the session's policy exactly as it does + // for an agent's nested API calls. Connection keys themselves are MCP-only on this fork. + const scoped: Session = { + cookies: actors.admin.cookies, + send: (method, path, data, headers) => + actors.admin.send(method, path, data, { + ...headers, + "x-executor-caller-connection": connection.id, + }), + }; + expect(yield* choices(scoped, second)).toEqual([]); + yield* refused(yield* appLink(scoped, second, shared), 403, "OrganizationForbidden"); + yield* refused( + yield* api.request(scoped, "GET", `${fixture.path}/accounts/app/team`), + 403, + "OrganizationForbidden", + ); + + // The admin does not own the account, but it already serves an app they can edit, so they + // may choose it for the second app and link it there for everyone. Every caller then runs + // with it, and the admin still cannot read the account itself. + expect(yield* choices(actors.admin, second)).toEqual([[shared, false]]); + yield* ok(yield* appLink(actors.admin, second, shared)); + expect(yield* who(actors.member, second)).toEqual({ team: shared }); + expect( + (yield* api.request(actors.admin, "GET", `${prefix}/accounts/${shared}`)).status, + ).not.toBe(200); + + // Nothing else is opened: the admin cannot link the account for themselves, and an account + // of the owner's that serves no app for everyone is neither offered nor linkable. + const userTeam = yield* fixture.another(teamUserSource); + yield* refused( + yield* linkAccountResponse(actors.admin, userTeam, "team", shared), + 403, + "OrganizationForbidden", + ); + const ownerStarted = yield* start(actors.owner, "user", "team", userTeam); + expect(ownerStarted.status, JSON.stringify(ownerStarted.body)).toBe(200); + const personal = (yield* okBody( + Resource, + yield* submit(actors.owner, (yield* body(Resource, ownerStarted)).id, "team-personal"), + )).id; + yield* fixture.saved(actors.owner, personal); + expect((yield* choices(actors.admin, second)).map(([id]) => id)).not.toContain(personal); + yield* refused( + yield* appLink(actors.admin, second, personal), + 403, + "OrganizationForbidden", + ); + + // A member who cannot edit the second app cannot link to it either. + yield* refused(yield* appLink(actors.member, second, shared), 403, "OrganizationForbidden"); + + // A member links their own account for everyone to an app they created. The admin reuses + // it for the second app, replacing its plain link. The account cannot be deleted while any + // app links it, but the member, who cannot edit the second app, may still stop it using + // their account, though not unlink another's. + const created = yield* api.request(actors.member, "POST", `${prefix}/apps/deploy`, { + name: `Member app ${randomUUID().slice(0, 8)}`, + files: [{ path: "index.ts", content: teamAppSource }, appsManifest], + }); + expect(created.status, JSON.stringify(created.body)).toBe(200); + const own = `${prefix}/apps/${(yield* body(Resource, created)).id}`; + yield* fixture.adopt(own); + const memberStarted = yield* start(actors.member, "app", "team", own); + expect(memberStarted.status, JSON.stringify(memberStarted.body)).toBe(200); + const memberTeam = (yield* okBody( + Resource, + yield* submit(actors.member, (yield* body(Resource, memberStarted)).id, "member-team"), + )).id; + yield* fixture.saved(actors.member, memberTeam); + yield* ok(yield* appLink(actors.admin, second, memberTeam)); + yield* ok( + yield* linkAccountResponse(actors.admin, second, "privateTeam", memberTeam, "app"), + ); + expect(yield* who(actors.owner, second)).toEqual({ team: memberTeam }); + // Consent must work even when the account owner cannot read the app or discover its + // requirement names. Only their own links' app ids leave the server. + const access = yield* okBody( + Schema.Struct({ revision: Schema.String }), + yield* api.request(actors.owner, "GET", `${second}/access`), + ); + yield* ok( + yield* api.request(actors.owner, "PATCH", `${second}/access`, { + revision: access.revision, + audience: { kind: "private" }, + }), + ); + for (const route of [second, `${second}/accounts/app`, `${second}/accounts/app/team`]) { + const denied = yield* api.request(actors.member, "GET", route); + yield* refused(denied, 403, "OrganizationForbidden"); + expect(JSON.stringify(denied.body)).not.toContain("privateTeam"); + } + const deletion = yield* api.request( + actors.member, + "DELETE", + `${prefix}/accounts/${memberTeam}`, + ); + yield* refused(deletion, 409, "AccountLinkedToApp"); + expect(JSON.stringify(deletion.body)).not.toContain("privateTeam"); + expect(yield* body(Schema.Struct({ apps: Schema.Array(Schema.String) }), deletion)).toEqual( + { + apps: [own, second].map((path) => path.slice(path.lastIndexOf("/") + 1)).sort(), + }, + ); + yield* refused( + yield* appUnlink(actors.member, second, shared), + 403, + "OrganizationForbidden", + ); + const unlinkOwned = (path: string, account: string) => + api.request(actors.member, "DELETE", `${path}/accounts/app/${account}`); + yield* refused(yield* unlinkOwned(second, shared), 403, "OrganizationForbidden"); + // An unrelated existing app and a nonexistent one give the same refusal. + for (const path of [fixture.path, `${prefix}/apps/app_${randomUUID()}`]) + yield* refused(yield* unlinkOwned(path, memberTeam), 403, "OrganizationForbidden"); + expect(yield* ok(yield* unlinkOwned(second, memberTeam))).toEqual({ + app: second.slice(second.lastIndexOf("/") + 1), + account: memberTeam, + }); + expect( + yield* ok(yield* api.request(actors.owner, "GET", `${second}/accounts/app`)), + ).toEqual([]); + yield* refused(yield* unlinkOwned(second, memberTeam), 403, "OrganizationForbidden"); + yield* refused( + yield* api.request(actors.owner, "POST", `${second}/tools/call`, { + tool: "who", + kind: "query", + input: {}, + }), + 409, + "AccountRequired", + ); + + // Once the account serves no app the admin can edit, they cannot link it again. + yield* ok(yield* appUnlink(actors.member, own, memberTeam)); + yield* refused( + yield* appLink(actors.admin, second, memberTeam), + 403, + "OrganizationForbidden", + ); + }), + ), + ); + it.effect(scenarios.hostedAppLinkReuseChain.title, (context) => + withHostedCase( + context, + Effect.gen(function* () { + const fixture = yield* appLinksFixture([{ path: "index.ts", content: source }]); + const { api, actors, prefix, connect } = fixture; + const appLink = (actor: Session, appPath: string, account: string) => + linkAccountResponse(actor, appPath, "team", account, "app"); + const appUnlink = (actor: Session, appPath: string, account: string) => + api.request( + actor, + "DELETE", + `${appPath}/accounts/app/team/${encodeURIComponent(account)}`, + ); + const memberApp = Effect.gen(function* () { + const created = yield* api.request(actors.member, "POST", `${prefix}/apps/deploy`, { + name: `Member app ${randomUUID().slice(0, 8)}`, + files: [{ path: "index.ts", content: teamAppSource }, appsManifest], + }); + expect(created.status, JSON.stringify(created.body)).toBe(200); + const appPath = `${prefix}/apps/${(yield* body(Resource, created)).id}`; + yield* fixture.adopt(appPath); + return appPath; + }); + + // The owner links their own account for everyone to the fixture's app. A member cannot + // link it to an app they created. + const secret = yield* connect(actors.owner, "app", "team", "OWNER-SECRET-TOKEN"); + const m1 = yield* memberApp; + yield* refused(yield* appLink(actors.member, m1, secret), 403, "OrganizationForbidden"); + + // Nor can an administrator put it there for them: the member edits that app, so its code + // could read the owner's credential. The account is not offered there either. + yield* refused(yield* appLink(actors.admin, m1, secret), 403, "OrganizationForbidden"); + const offered = yield* api.request(actors.admin, "GET", `${m1}/accounts/app/team`); + expect( + (yield* okBody(LinkRequirement, offered)).accounts.map((entry) => entry.account.id), + ).not.toContain(secret); + + // So the member has nothing to chain from: linking it to a second app of theirs is + // refused, and that app's code never receives the owner's token. + const m2 = yield* memberApp; + yield* refused(yield* appLink(actors.member, m2, secret), 403, "OrganizationForbidden"); + const read = yield* api.request(actors.member, "POST", `${m2}/tools/call`, { + tool: "who", + kind: "query", + input: {}, + }); + yield* refused(read, 409, "AccountRequired"); + expect(JSON.stringify(read.body)).not.toContain("OWNER-SECRET-TOKEN"); + + // The administrator's unlink finds nothing in the member's app, and afterwards the member + // still cannot link the account to any app of theirs. + yield* refused(yield* appUnlink(actors.admin, m1, secret), 404, "AccountNotFound"); + const m4 = yield* memberApp; + yield* refused(yield* appLink(actors.member, m4, secret), 403, "OrganizationForbidden"); + expect(yield* linkedAccountIds(actors.member, m4, "app")).toEqual([]); + }), + ), + ); + it.effect(scenarios.hostedAppLinkReuseDeparted.title, (context) => + withHostedCase( + context, + Effect.gen(function* () { + const fixture = yield* appLinksFixture([{ path: "index.ts", content: source }]); + const { api, actors, prefix, connect } = fixture; + const appLink = (actor: Session, appPath: string, account: string) => + linkAccountResponse(actor, appPath, "team", account, "app"); + /** An app with one plain team requirement that `actor` creates. */ + const deployAs = (actor: Session) => + Effect.gen(function* () { + const created = yield* api.request(actor, "POST", `${prefix}/apps/deploy`, { + name: `Reuse departure ${randomUUID().slice(0, 8)}`, + files: [{ path: "index.ts", content: teamAppSource }, appsManifest], + }); + expect(created.status, JSON.stringify(created.body)).toBe(200); + const appPath = `${prefix}/apps/${(yield* body(Resource, created)).id}`; + yield* fixture.adopt(appPath); + return appPath; + }); + const who = (actor: Session, appPath: string) => + api.request(actor, "POST", `${appPath}/tools/call`, { + tool: "who", + kind: "query", + input: {}, + }); + const leaves = (actor: Session) => + Effect.gen(function* () { + const { user } = yield* body( + SessionIdentity, + yield* api.request(actor, "GET", "/api/auth/get-session"), + ); + yield* ok( + yield* api.request(actors.owner, "POST", "/api/auth/organization/remove-member", { + organizationId: actors.organization.id, + memberIdOrEmail: user.email, + }), + ); + }); + + // The member links their own account for everyone to an app they created, and the + // administrator reuses it in an app of their own, whose code then runs with it. + const memberSource = yield* deployAs(actors.member); + const started = yield* api.request( + actors.member, + "POST", + `${requirementPath(memberSource, "app", "team")}/connections`, + {}, + ); + const saved = yield* okBody( + Resource, + yield* fixture.submit( + actors.member, + (yield* okBody(Resource, started)).id, + "MEMBER-SECRET-TOKEN", + ), + ); + const memberAccount = saved.id; + yield* fixture.saved(actors.member, memberAccount); + const adminApp = yield* deployAs(actors.admin); + yield* ok(yield* appLink(actors.admin, adminApp, memberAccount)); + expect(yield* okBody(TeamWho, yield* who(actors.admin, adminApp))).toEqual({ + team: memberAccount, + token: "MEMBER-SECRET-TOKEN", + }); + + // The owner links theirs for everyone to the fixture's app, while the member still + // created an app the administrator can edit. + const secret = yield* connect(actors.owner, "app", "team", "OWNER-SECRET-TOKEN"); + const memberTarget = yield* deployAs(actors.member); + yield* refused( + yield* appLink(actors.admin, memberTarget, secret), + 403, + "OrganizationForbidden", + ); + + // The member leaves. Their account stops serving every app, the administrator's included, + // and the administrator can no longer reuse it anywhere. + yield* leaves(actors.member); + yield* refused(yield* who(actors.admin, adminApp), 409, "AccountRequired"); + const otherAdminApp = yield* deployAs(actors.admin); + yield* refused( + yield* appLink(actors.admin, otherAdminApp, memberAccount), + 403, + "OrganizationForbidden", + ); + + // An app whose creator left still counts as theirs: invited back, they would edit it + // again, so the owner's account is neither offered there nor linked. + const offered = yield* api.request( + actors.admin, + "GET", + `${memberTarget}/accounts/app/team`, + ); + expect( + (yield* okBody(LinkRequirement, offered)).accounts.map((entry) => entry.account.id), + ).not.toContain(secret); + yield* refused( + yield* appLink(actors.admin, memberTarget, secret), + 403, + "OrganizationForbidden", + ); + expect(yield* linkedAccountIds(actors.admin, memberTarget, "app")).toEqual([]); + // An app the administrator created still takes it. + yield* ok(yield* appLink(actors.admin, otherAdminApp, secret)); + expect(yield* okBody(TeamWho, yield* who(actors.admin, otherAdminApp))).toEqual({ + team: secret, + token: "OWNER-SECRET-TOKEN", + }); + }), + ), + ); + it.effect(scenarios.hostedAppLinkCreatorDemotion.title, (context) => + withHostedCase( + context, + Effect.gen(function* () { + const fixture = yield* appLinksFixture([{ path: "index.ts", content: source }]); + const { api, actors, prefix, connect } = fixture; + const telemetry = yield* Telemetry; + const creator = yield* okBody( + SessionIdentity, + yield* api.request(actors.admin, "GET", "/api/auth/get-session"), + ); + const { members } = yield* okBody( + Memberships, + yield* api.request( + actors.owner, + "GET", + `/api/auth/organization/list-members?organizationId=${actors.organization.id}`, + ), + ); + const membership = members.find((member) => member.user.email === creator.user.email); + if (membership === undefined) return yield* Effect.die("The creator has no membership"); + const role = (role: "admin" | "member") => + Effect.gen(function* () { + yield* ok( + yield* api.request( + actors.owner, + "POST", + "/api/auth/organization/update-member-role", + { + organizationId: actors.organization.id, + memberId: membership.id, + role, + }, + ), + ); + }); + const token = `demotion-fixture-${randomUUID()}`; + const account = yield* connect(actors.owner, "app", "team", token); + const created = yield* okBody( + Resource, + yield* api.request(actors.admin, "POST", `${prefix}/apps/deploy`, { + name: "Creator demotion", + files: [{ path: "index.ts", content: teamAppSource }, appsManifest], + }), + ); + const path = `${prefix}/apps/${created.id}`; + yield* fixture.adopt(path); + const link = (actor: Session) => linkAccount(actor, path, "team", account, "app"); + const who = (traceparent?: string) => + api.request( + actors.admin, + "POST", + `${path}/tools/call`, + { tool: "who", kind: "query", input: {} }, + traceparent === undefined ? undefined : { traceparent }, + ); + const serves = Effect.gen(function* () { + const served = yield* okBody(TeamWho, yield* who()); + expect(served.team).toBe(account); + expect(served.token === token).toBe(true); + }); + const action = "app_account.use_refused"; + yield* link(actors.admin); + yield* serves; + yield* role("member"); + + // The creator can still use their app, but not the reused account. + const first = traced(); + const declined = yield* who(first.traceparent); + expect(declined.status).toBe(409); + yield* refused(declined, 409, "AppLinkCreatorNotAuthorized"); + const logs = yield* telemetry.logs(first.traceId).pipe( + Effect.filterOrFail( + (logs) => logs.includes(action), + () => new Error("The refusal audit has not reached the collector"), + ), + Effect.retry({ schedule: Schedule.spaced("500 millis"), times: 40 }), + ); + expect(auditOf(logs, action)).toEqual({ + "audit.action": action, + "audit.outcome": "failure", + "audit.target.kind": "account", + "audit.target.id": account, + "audit.app": created.id, + "audit.requirement": "team", + "audit.reason": "creator_not_admin_or_owner", + }); + expect(logs.includes(token)).toBe(false); + yield* refused(yield* who(first.traceparent), 409, "AppLinkCreatorNotAuthorized"); + + yield* role("admin"); + yield* serves; + yield* role("member"); + const second = traced(); + yield* refused(yield* who(second.traceparent), 409, "AppLinkCreatorNotAuthorized"); + const secondLogs = yield* telemetry.logs(second.traceId).pipe( + Effect.filterOrFail( + (logs) => logs.includes(action), + () => new Error("The second demotion audit has not reached the collector"), + ), + Effect.retry({ schedule: Schedule.spaced("500 millis"), times: 40 }), + ); + expect(auditOf(secondLogs, action)["audit.target.id"]).toBe(account); + expect(auditOf(yield* telemetry.logs(first.traceId), action)["audit.target.id"]).toBe( + account, + ); + + // The account owner can make the link themselves without promoting the creator. + yield* link(actors.owner); + yield* serves; + yield* role("admin"); + yield* role("member"); + yield* serves; + }), + ), + ); + it.effect(scenarios.hostedAppLinkCreatorRemoval.title, (context) => + withHostedCase( + context, + Effect.gen(function* () { + const fixture = yield* appLinksFixture([{ path: "index.ts", content: source }]); + const { api, actors, prefix, connect, ids } = fixture; + const telemetry = yield* Telemetry; + const creator = yield* okBody( + SessionIdentity, + yield* api.request(actors.admin, "GET", "/api/auth/get-session"), + ); + const { members } = yield* okBody( + Memberships, + yield* api.request( + actors.owner, + "GET", + `/api/auth/organization/list-members?organizationId=${actors.organization.id}`, + ), + ); + const membership = members.find((member) => member.user.email === creator.user.email); + if (membership === undefined) return yield* Effect.die("The creator has no membership"); + const token = `creator-removal-fixture-${randomUUID()}`; + const account = yield* connect(actors.owner, "app", "team", token); + const created = yield* okBody( + Resource, + yield* api.request(actors.admin, "POST", `${prefix}/apps/deploy`, { + name: "Creator removal", + files: [{ path: "index.ts", content: teamAppSource }, appsManifest], + }), + ); + const path = `${prefix}/apps/${created.id}`; + yield* fixture.adopt(path); + // Keep the owner's access independent of the creator and their membership. + const { revision } = yield* okBody( + Schema.Struct({ revision: Schema.String }), + yield* api.request(actors.admin, "GET", `${path}/access`), + ); + yield* ok( + yield* api.request(actors.admin, "PATCH", `${path}/access`, { + revision, + audience: { kind: "everyone" }, + }), + ); + const who = (actor: Session) => + api.request(actor, "POST", `${path}/tools/call`, { + tool: "who", + kind: "query", + input: {}, + }); + // The admin reuses the owner's account in the app they created, then is demoted. + yield* linkAccount(actors.admin, path, "team", account, "app"); + expect((yield* okBody(TeamWho, yield* who(actors.owner))).token === token).toBe(true); + yield* ok( + yield* api.request(actors.owner, "POST", "/api/auth/organization/update-member-role", { + organizationId: actors.organization.id, + memberId: membership.id, + role: "member", + }), + ); + yield* refused(yield* who(actors.owner), 409, "AppLinkCreatorNotAuthorized"); + + // The owner removes them from the organization before they delete their user, so the + // organization is no longer one of theirs when they do. + yield* ok( + yield* api.request(actors.owner, "POST", "/api/auth/organization/remove-member", { + organizationId: actors.organization.id, + memberIdOrEmail: creator.user.email, + }), + ); + yield* refused(yield* who(actors.owner), 409, "AppLinkCreatorNotAuthorized"); + + // Model a creator being nulled outside the deletion sweep (for example, a concurrent + // reuse committed after the sweep). The stored reuse must still be refused at use time. + const locks = yield* cloudLocks; + yield* locks.run({ + sql: "update hosted_app_access set creator_id = null where id = $1", + params: [created.id], + }); + yield* refused(yield* who(actors.owner), 409, "AppLinkCreatorNotAuthorized"); + yield* locks.run({ + sql: "update hosted_app_access set creator_id = $2 where id = $1", + params: [created.id, ids.admin], + }); + + // Deleting their user leaves the app without a creator; the reuse must not serve again. + const removal = traced(); + const deleted = yield* api.request(actors.admin, "DELETE", "/api/user", undefined, { + traceparent: removal.traceparent, + }); + expect(deleted.status, JSON.stringify(deleted.body)).toBe(200); + const after = yield* who(actors.owner); + expect(JSON.stringify(after.body).includes(token)).toBe(false); + yield* refused(after, 409, "AccountRequired"); + expect(yield* linkedAccountIds(actors.owner, path, "app")).toEqual([]); + const action = "app_account.unlink"; + const logs = yield* telemetry.logs(removal.traceId).pipe( + Effect.filterOrFail( + (logs) => logs.includes(action), + () => new Error("The unlink audit has not reached the collector"), + ), + Effect.retry({ schedule: Schedule.spaced("500 millis"), times: 40 }), + ); + expect(auditOf(logs, action)).toEqual({ + "audit.action": action, + "audit.outcome": "success", + "audit.target.kind": "account", + "audit.target.id": account, + "audit.app": created.id, + "audit.authority": "creator_removal", + }); + expect(logs.includes(token)).toBe(false); + + // A creator-less app also refuses a fresh reuse of the same still-eligible source. + const other = members.find(({ userId }) => userId === ids.member); + if (other === undefined) return yield* Effect.die("The other member has no membership"); + yield* ok( + yield* api.request(actors.owner, "POST", "/api/auth/organization/update-member-role", { + organizationId: actors.organization.id, + memberId: other.id, + role: "admin", + }), + ); + yield* refused( + yield* linkAccountResponse(actors.member, path, "team", account, "app"), + 403, + "OrganizationForbidden", + ); + // The account's owner can still link it to the app themselves. + yield* linkAccount(actors.owner, path, "team", account, "app"); + expect((yield* okBody(TeamWho, yield* who(actors.owner))).token === token).toBe(true); + }), + ), + ); + it.effect(scenarios.hostedAppLinkCreatorDemotionDuringRefresh.title, (context) => + withHostedCase( + context, + Effect.gen(function* () { + const http = yield* HttpClient.HttpClient; + const issuer = yield* oauthSetupIssuer; + const telemetry = yield* Telemetry; + // The initial access token is already inside the renewal window. Settlement below lets + // the background source finish; the next explicit call still needs a refresh. + yield* issuer.configure({ refreshTokens: true, expiresIn: 20 }); + const oauthSource = `import { defineApp, defineProvider, oauth2, query, object, router } from "apps"; +const service = defineProvider({ name: "Reuse refresh fixture", auth: { oauth: oauth2({ discover: ${JSON.stringify(`${issuer.origin}/mcp`)} }) } }); +export default defineApp({ app: { accounts: { service } } }, async ({ app }) => ({ + tools: router({ who: query({ input: object({}) }, async () => ({ id: app.accounts.service.id })) }), +}));`; + const files = [{ path: "index.ts", content: oauthSource }]; + const fixture = yield* appLinksFixture(files); + const { api, actors, prefix, start, path, ids } = fixture; + const connection = yield* okBody(Resource, yield* start(actors.owner, "app", "service")); + const { authorizationUrl } = yield* okBody( + SignIn, + yield* api.request( + actors.owner, + "POST", + `${prefix}/connections/${connection.id}/oauth/start`, + { method: "oauth", label: "Reuse refresh fixture" }, + ), + ); + const callbackUrl = yield* Effect.scoped( + Effect.gen(function* () { + const consent = yield* HttpClient.withScope(http).get(authorizationUrl); + expect(consent.status).toBe(302); + const location = consent.headers.location; + if (location === undefined) return yield* Effect.die("No OAuth callback"); + return location; + }), + ).pipe(Effect.provideService(FetchHttpClient.RequestInit, { redirect: "manual" })); + const account = yield* okBody( + Resource, + yield* api.request( + actors.owner, + "POST", + `${prefix}/connections/${connection.id}/oauth/complete`, + { callbackUrl }, + ), + ); + yield* fixture.saved(actors.owner, account.id); + const created = yield* okBody( + Resource, + yield* api.request(actors.admin, "POST", `${prefix}/apps/deploy`, { + name: "Demotion during refresh", + files: [...files, appsManifest], + }), + ); + const target = `${prefix}/apps/${created.id}`; + yield* fixture.adopt(target); + yield* linkAccount(actors.admin, target, "service", account.id, "app"); + for (const appPath of [path, target]) + yield* settleWebhookSources(actors.owner, appPath, (sources) => + sources.some((source) => source.account === account.id && source.status !== "pending"), + ); + + const { members } = yield* okBody( + Memberships, + yield* api.request( + actors.owner, + "GET", + `/api/auth/organization/list-members?organizationId=${actors.organization.id}`, + ), + ); + const membership = members.find(({ userId }) => userId === ids.admin); + if (membership === undefined) return yield* Effect.die("The creator has no membership"); + const role = (role: "admin" | "member") => + Effect.flatMap( + api.request(actors.owner, "POST", "/api/auth/organization/update-member-role", { + organizationId: actors.organization.id, + memberId: membership.id, + role, + }), + ok, + ); + const who = (traceparent?: string) => + api.request( + actors.admin, + "POST", + `${target}/tools/call`, + { tool: "who", kind: "query", input: {} }, + traceparent === undefined ? undefined : { traceparent }, + ); + const trace = traced(); + yield* Effect.addFinalizer(() => + issuer.configure({ hold: null }).pipe(Effect.andThen(issuer.release)), + ); + yield* issuer.configure({ hold: "refresh-issued", refreshedExpiresIn: 3600 }); + const before = yield* issuer.metrics; + const pending = yield* Effect.forkChild(who(trace.traceparent)); + yield* issuer.metrics.pipe( + Effect.filterOrFail( + (metrics) => metrics.held > before.held, + () => new Error("The tool call never reached the issuer's refresh gate"), + ), + Effect.retry({ schedule: Schedule.spaced("100 millis"), times: 100 }), + ); + yield* role("member"); + yield* issuer.configure({ hold: null }); + yield* issuer.release; + const declined = yield* Fiber.join(pending); + expect(declined.status).toBe(409); + yield* refused(declined, 409, "AppLinkCreatorNotAuthorized"); + expect((yield* issuer.metrics).refreshesIssued).toBe(before.refreshesIssued + 1); + + const action = "app_account.use_refused"; + const logs = yield* telemetry.logs(trace.traceId).pipe( + Effect.filterOrFail( + (logs) => logs.includes(action), + () => new Error("The post-refresh refusal audit has not reached the collector"), + ), + Effect.retry({ schedule: Schedule.spaced("500 millis"), times: 40 }), + ); + expect(auditOf(logs, action)).toEqual({ + "audit.action": action, + "audit.outcome": "failure", + "audit.target.kind": "account", + "audit.target.id": account.id, + "audit.app": created.id, + "audit.requirement": "service", + "audit.reason": "creator_not_admin_or_owner", + }); + expect(/synthetic-(access|refresh)/u.test(logs)).toBe(false); + yield* role("admin"); + expect((yield* okBody(Resource, yield* who())).id).toBe(account.id); + }), + ), + ); + it.effect(scenarios.hostedAppLinkAudit.title, (context) => + withHostedCase( + context, + Effect.gen(function* () { + const fixture = yield* appLinksFixture([{ path: "index.ts", content: source }]); + const { api, actors, prefix, path, app } = fixture; + const telemetry = yield* Telemetry; + /** The trace's audit record for `action`, once it reached the collector. */ + const audit = (traceId: string, action: string) => + telemetry.logs(traceId).pipe( + Effect.filterOrFail( + (logs) => logs.includes(action), + () => new Error(`The ${action} record has not reached the collector`), + ), + Effect.retry({ schedule: Schedule.spaced("500 millis"), times: 40 }), + Effect.map((logs) => ({ logs, record: auditOf(logs, action) })), + ); + const idOf = (appPath: string) => appPath.slice(appPath.lastIndexOf("/") + 1); + + // Connecting from the app links the owner's new account for everyone: recorded once the + // save commits, as the owner's. + const connected = traced(); + const started = yield* api.request( + actors.owner, + "POST", + `${requirementPath(path, "app", "team")}/connections`, + {}, + ); + const saved = yield* okBody( + Resource, + yield* api.request( + actors.owner, + "POST", + `${prefix}/connections/${(yield* okBody(Resource, started)).id}/submit`, + { method: "key", label: "AUDIT-OWNER-TOKEN", fields: { token: "AUDIT-OWNER-TOKEN" } }, + { traceparent: connected.traceparent }, + ), + ); + const secret = saved.id; + yield* fixture.saved(actors.owner, secret); + const connect = yield* audit(connected.traceId, "app_account.link"); + expect(connect.record).toMatchObject({ + "audit.outcome": "success", + "audit.target.kind": "account", + "audit.target.id": secret, + "audit.app": app.id, + "audit.authority": "owner", + "audit.via": "connect", + "audit.changed": "true", + }); + expect(connect.logs).not.toContain("AUDIT-OWNER-TOKEN"); + + // The administrator reuses it in another app: recorded as a reuse. + const other = yield* fixture.another(teamAppSource); + const reused = traced(); + yield* ok( + yield* api.request( + actors.admin, + "PUT", + `${requirementPath(other, "app", "team")}/${secret}`, + undefined, + { traceparent: reused.traceparent }, + ), + ); + expect((yield* audit(reused.traceId, "app_account.link")).record).toMatchObject({ + "audit.outcome": "success", + "audit.target.id": secret, + "audit.app": idOf(other), + "audit.authority": "app_link", + "audit.changed": "true", + }); + + // A refused link names no app, and never the one the request named. + const missing = `app_${randomUUID()}`; + const refusal = traced(); + const denied = yield* api.request( + actors.admin, + "PUT", + `${requirementPath(`${prefix}/apps/${missing}`, "app", "team")}/${secret}`, + undefined, + { traceparent: refusal.traceparent }, + ); + expect(denied.status, JSON.stringify(denied.body)).not.toBe(200); + const failed = yield* audit(refusal.traceId, "app_account.link"); + expect(failed.record).toMatchObject({ "audit.outcome": "failure" }); + expect(failed.record).not.toHaveProperty("audit.app"); + expect(failed.record).not.toHaveProperty("audit.target.id"); + expect(failed.logs).not.toContain(missing); + + // An editor's unlink of someone else's account is recorded as the app manager's. + const managed = traced(); + yield* ok( + yield* api.request( + actors.admin, + "DELETE", + `${requirementPath(path, "app", "team")}/${secret}`, + undefined, + { traceparent: managed.traceparent }, + ), + ); + expect((yield* audit(managed.traceId, "app_account.unlink")).record).toMatchObject({ + "audit.outcome": "success", + "audit.target.id": secret, + "audit.app": app.id, + "audit.authority": "app_manager", + }); + + // Its owner's unlink from every requirement of the other app is recorded as the owner's. + const owned = traced(); + yield* ok( + yield* api.request(actors.owner, "DELETE", `${other}/accounts/app/${secret}`, undefined, { + traceparent: owned.traceparent, + }), + ); + expect((yield* audit(owned.traceId, "app_account.unlink")).record).toMatchObject({ + "audit.outcome": "success", + "audit.target.id": secret, + "audit.app": idOf(other), + "audit.authority": "owner", + }); + }), + ), + ); it.effect(scenarios.hostedAppRequirementScopes.title, (context) => withHostedCase( context, diff --git a/e2e/tests/app-account-link-held-access.spec.ts b/e2e/tests/app-account-link-held-access.spec.ts index 359a19124e..3d2c618cbe 100644 --- a/e2e/tests/app-account-link-held-access.spec.ts +++ b/e2e/tests/app-account-link-held-access.spec.ts @@ -1,6 +1,6 @@ /** * Linking an account for everyone needs edit access to the app and use of the account, and - * unlinking one needs edit access. Saving an MCP connection that runs an app as an account links + * unlinking someone else's needs edit access. Saving an MCP connection that runs an app as an account links * that account for the member, which needs use of the app and of the account. Each route checks * before its write, which then waits for the app's link lock; access revoked in between must refuse * the write. The self-host test entry point holds the request just after its last check has @@ -248,22 +248,28 @@ layer(HostedLive, { excludeTestServices: true })("App account link held access", expect(linked.status, JSON.stringify(linked.body)).toBe(200); expect(yield* appLinks).toEqual([["service", first]]); + // The owner links their account again. Its owner may unlink an account from any app, so + // the admin's unlinks below are of the owner's account, which needs edit access. + const relinked = yield* send(owner, "PUT", linkPath(second)); + expect(relinked.status, JSON.stringify(relinked.body)).toBe(200); + expect(yield* appLinks).toEqual([["service", second]]); + // Demoted while its unlink waits, the admin no longer edits the app: the link stays. const kept = yield* held( "app-access", app, - send(admin, "DELETE", linkPath(first)), + send(admin, "DELETE", linkPath(second)), setRole("member"), ); yield* refused(kept); - expect(yield* appLinks).toEqual([["service", first]]); + expect(yield* appLinks).toEqual([["service", second]]); yield* setRole("admin"); // Held and released with nothing changed, the unlink removes it. const unlinked = yield* held( "app-access", app, - send(admin, "DELETE", linkPath(first)), + send(admin, "DELETE", linkPath(second)), Effect.void, ); expect(unlinked.status, JSON.stringify(unlinked.body)).toBe(200); @@ -272,6 +278,67 @@ layer(HostedLive, { excludeTestServices: true })("App account link held access", ), ); + it.effect(scenarios.hostedAppLinkReuseHeldDemotion.title, (context) => + withHostedCase( + context, + Effect.gen(function* () { + const { send, admin, owner, prefix, setRole, connect, held } = yield* heldCase("app"); + // The owner links their own account for everyone to the case's app, which the admin can + // edit, so the admin may reuse it in another app the owner created. + const account = yield* connect(owner, "synthetic-held-reuse"); + const deployed = yield* send(owner, "POST", `${prefix}/apps/deploy`, { + name: `Held reuse ${randomUUID().slice(0, 8)}`, + files: [{ path: "index.ts", content: source }, appsManifest], + }); + expect(deployed.status, JSON.stringify(deployed.body)).toBe(200); + const target = `${prefix}/apps/${(yield* body(App, deployed)).id}`; + // Runs before the case's own cleanup, which deletes the account. + yield* Effect.addFinalizer(() => send(owner, "DELETE", target).pipe(Effect.orDie)); + const targetLinks = send(owner, "GET", `${target}/accounts/app`).pipe( + Effect.flatMap((response) => body(Links, response)), + Effect.map((found) => found.map((link) => [link.requirement, link.account.id])), + ); + const reusePath = `${requirementPath(target, "app", "service")}/${account}`; + + // Demoted while the reuse waits, just after its check of who owns the account, the admin + // reuses nothing: a member never reuses an account. + const demoted = yield* held( + "account-access", + account, + send(admin, "PUT", reusePath), + setRole("member"), + ); + yield* refused(demoted); + expect(yield* targetLinks).toEqual([]); + // Nor once the demotion committed. + yield* refused(yield* send(admin, "PUT", reusePath)); + expect(yield* targetLinks).toEqual([]); + yield* setRole("admin"); + + // Held and released with nothing changed, the reuse links the owner's account. + const reused = yield* held( + "account-access", + account, + send(admin, "PUT", reusePath), + Effect.void, + ); + expect(reused.status, JSON.stringify(reused.body)).toBe(200); + expect(yield* targetLinks).toEqual([["service", account]]); + + // Demoted right after, the admin can neither reuse it again nor unlink it: the app is the + // owner's, and only its owner and the account's owner may change its links. + yield* setRole("member"); + yield* refused(yield* send(admin, "DELETE", reusePath)); + yield* refused(yield* send(admin, "PUT", reusePath)); + expect(yield* targetLinks).toEqual([["service", account]]); + // The account's owner still unlinks it. + const unlinked = yield* send(owner, "DELETE", reusePath); + expect(unlinked.status, JSON.stringify(unlinked.body)).toBe(200); + expect(yield* targetLinks).toEqual([]); + }), + ), + ); + it.effect(scenarios.hostedMcpLinkHeldAccess.title, (context) => withHostedCase( context, diff --git a/e2e/tests/mcp-execute-failures.spec.ts b/e2e/tests/mcp-execute-failures.spec.ts index b7c4b9555b..67e765f9f0 100644 --- a/e2e/tests/mcp-execute-failures.spec.ts +++ b/e2e/tests/mcp-execute-failures.spec.ts @@ -475,8 +475,11 @@ const beforeTheApp = [ * Executor's own failures, which the call route can report before the host hands the call to the * app's code or after it, such as saving the approval request a write's policy asked for. After * it, they record that the call may have written (`mcpExecuteApprovalSaveFailed`). + * `AppLinkCreatorNotAuthorized` comes from the product's recheck of a reused app account, which + * runs where resolving the call's accounts does: before the hand-off, after the catalog read's + * hand-off, and again when an account's credentials are renewed during the call. */ -const eitherSide = ["StorageError", "CredentialsError"]; +const eitherSide = ["StorageError", "CredentialsError", "AppLinkCreatorNotAuthorized"]; /** The reply each forging tool's failure is rewritten to. */ const forgedReplies = { diff --git a/e2e/tests/tool-call-log.spec.ts b/e2e/tests/tool-call-log.spec.ts index 242ae23bb0..152a46f670 100644 --- a/e2e/tests/tool-call-log.spec.ts +++ b/e2e/tests/tool-call-log.spec.ts @@ -1672,6 +1672,7 @@ catch (error) { return { caught: { message: error.message, reference: typeof err "404": ["AccountNotFound", "AppNotFound", "ToolNotFound", "VersionNotFound"], "409": [ "AccountRequired", + "AppLinkCreatorNotAuthorized", "AppNotDeployed", "ConnectionAccountUnlinked", "OAuthReconnectRequired", diff --git a/packages/sdk/src/contracts/app-data.ts b/packages/sdk/src/contracts/app-data.ts index 20136f4229..31a6407c8d 100644 --- a/packages/sdk/src/contracts/app-data.ts +++ b/packages/sdk/src/contracts/app-data.ts @@ -10,6 +10,7 @@ import { AppNotFound, AppNotDeployed, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, } from "./apps.ts"; @@ -96,6 +97,7 @@ export const appDataErrors = [ AccountNotFound, VersionNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, OAuthReconnectRequired, diff --git a/packages/sdk/src/contracts/apps.ts b/packages/sdk/src/contracts/apps.ts index 26a395f182..03d4d381cd 100644 --- a/packages/sdk/src/contracts/apps.ts +++ b/packages/sdk/src/contracts/apps.ts @@ -3,6 +3,7 @@ import { ApiError } from "@executor-js/utils/api-error"; import { withLegacyKeys } from "@executor-js/utils/legacy-keys"; import { DeclaredRequirements } from "apps/contracts"; import { AppSlug } from "./app-slug.ts"; +import { callPresentation, MayHaveWritten } from "./call-presentation.ts"; export { AppSlug, appSlug, appSlugFits } from "./app-slug.ts"; /** Apps own deployed code and declared requirements. Callers choose accounts for each operation. */ import { Effect, Schema } from "effect"; @@ -426,6 +427,29 @@ export const AccountRequired = UserFacingError.define({ /** A plain requirement with no linked account. */ export type AccountRequired = typeof AccountRequired.Type; +/** A reused app link no longer has an authorized target creator. */ +export const AppLinkCreatorNotAuthorized = UserFacingError.define({ + tag: "AppLinkCreatorNotAuthorized", + status: 409, + fields: { + app: AppId, + account: AccountId, + requirement: Schema.String, + mayHaveWritten: MayHaveWritten, + }, + presentation: callPresentation(({ requirement }) => ({ + title: "This reused app account no longer serves", + description: `The reused account for “${requirement}” no longer serves because the app's creator is missing or is no longer an organization admin or owner. Ask an admin to relink an eligible account, or ask the account's owner to link it directly.`, + recovery: { + action: + "Ask an admin to relink an eligible account, or ask the account's owner to link it directly.", + instructions: + "In the app's Accounts tab, an admin can replace the refused link with an account eligible for this app. To keep using the same account when the creator is missing or is no longer an admin or owner, the account's owner must have edit access to the app and link their account directly.", + }, + })), +}); +export type AppLinkCreatorNotAuthorized = typeof AppLinkCreatorNotAuthorized.Type; + /** A linked account, named by its saved label. */ export const AccountChoice = Schema.Struct({ id: AccountId, label: Schema.String }); export type AccountChoice = typeof AccountChoice.Type; diff --git a/packages/sdk/src/contracts/executor.ts b/packages/sdk/src/contracts/executor.ts index 36959f7a63..cb8db4e9c5 100644 --- a/packages/sdk/src/contracts/executor.ts +++ b/packages/sdk/src/contracts/executor.ts @@ -28,7 +28,7 @@ import type { ToolSearchPage, ToolSearchUnavailable, } from "./tools.ts"; -import type { AccountScope, App, AppRequirements } from "./apps.ts"; +import type { AccountScope, App, AppLinkCreatorNotAuthorized, AppRequirements } from "./apps.ts"; import type { BuildMemoryExceeded, BuildUnanswered, VersionBuildFailed } from "./version.ts"; import type { Account } from "./account.ts"; import type { @@ -112,7 +112,9 @@ export interface ResourceLifecycle { */ readonly appLinks: (input: { readonly app: AppId; - }) => Effect.Effect, StorageError>; + readonly requirements: AppRequirements["accounts"]; + readonly source?: AccountId | undefined; + }) => Effect.Effect, StorageError | AppLinkCreatorNotAuthorized>; /** * Resolving a webhook source's accounts found that its actor no longer has the source account * linked, or can no longer use it. The product links the source again with another subject who @@ -143,7 +145,7 @@ export interface ResourceLifecycle { app: { readonly id: AppId; readonly owner: OwnerId }, accounts: readonly Pick[], appUses: readonly AppAccountUse[], - ) => Effect.Effect, StorageError>; + ) => Effect.Effect, StorageError | AppLinkCreatorNotAuthorized>; /** * {@link appResolving} for several apps of one owner at once, for the `CurrentActor`, such as * every app a search reads: a host reads them in one statement. Each app's entry, in order, is @@ -158,7 +160,9 @@ export interface ResourceLifecycle { readonly appUses: readonly AppAccountUse[]; }>, ) => Effect.Effect< - ReadonlyArray, StorageError>>, + ReadonlyArray< + Result.Result, StorageError | AppLinkCreatorNotAuthorized> + >, StorageError >; /** @@ -178,7 +182,7 @@ export interface ResourceLifecycle { app: { readonly id: AppId; readonly owner: OwnerId }, accounts: readonly Pick[], appUses: readonly AppAccountUse[], - ) => Effect.Effect, StorageError>; + ) => Effect.Effect, StorageError | AppLinkCreatorNotAuthorized>; /** * Recheck product authority for the `CurrentActor` before acquiring account credentials without * running the app's code or removing what it registered, such as checking an account, and again diff --git a/packages/sdk/src/contracts/schedules.ts b/packages/sdk/src/contracts/schedules.ts index 7c5694ed43..80a7bbbfba 100644 --- a/packages/sdk/src/contracts/schedules.ts +++ b/packages/sdk/src/contracts/schedules.ts @@ -18,6 +18,7 @@ import { AppNotDeployed, AppNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, } from "./apps.ts"; @@ -232,6 +233,7 @@ export const ScheduleErrors = [ AppNotFound, AppNotDeployed, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, AccountNotFound, diff --git a/packages/sdk/src/contracts/skills.ts b/packages/sdk/src/contracts/skills.ts index 9670b7e544..66eaa6261b 100644 --- a/packages/sdk/src/contracts/skills.ts +++ b/packages/sdk/src/contracts/skills.ts @@ -10,6 +10,7 @@ import { OAuthReconnectRequired, OAuthRenewalFailed } from "./oauth.ts"; import { AppEvaluationFailed, AppProviderFailed } from "./tools.ts"; import { AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, AppAccounts, @@ -114,6 +115,7 @@ export const AppSkillErrors = [ SkillDefinitionInvalid, SkillRevisionChanged, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, AppEvaluationFailed, diff --git a/packages/sdk/src/contracts/tools.ts b/packages/sdk/src/contracts/tools.ts index 8d789c83ca..06343c2dbe 100644 --- a/packages/sdk/src/contracts/tools.ts +++ b/packages/sdk/src/contracts/tools.ts @@ -61,6 +61,7 @@ import { import { AccountNotFound } from "./account.ts"; import { AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, AppAccounts, @@ -1367,6 +1368,7 @@ export type ToolSearchStale = typeof ToolSearchStale.Type; export const RenewalFailure = Schema.Union([ OAuthReconnectRequired, OAuthRenewalFailed, + AppLinkCreatorNotAuthorized, StorageError, CredentialsError, ]); @@ -1399,6 +1401,7 @@ const renewalRecovery = ( }, }; case "OAuthReconnectRequired": + case "AppLinkCreatorNotAuthorized": return { recovery: failure.recovery }; case "OAuthRenewalFailed": return failure.retryable @@ -1985,6 +1988,7 @@ export type ToolPolicyFailed = typeof ToolPolicyFailed.Type; */ export const CallFailure = Schema.Union([ ToolCallFailed, + AppLinkCreatorNotAuthorized, AppProviderFailed, AppEvaluationFailed, ToolElicitationFailed, @@ -2022,6 +2026,7 @@ export const callFailurePresentation = (failure: CallFailure): ErrorPresentation ToolApprovalRequired: refusalPresentation(toolApprovalRequired), ToolPolicyFailed: refusalPresentation(toolPolicyFailed), ToolCallFailed: presented, + AppLinkCreatorNotAuthorized: presented, AppProviderFailed: presented, AppEvaluationFailed: presented, ToolElicitationFailed: presented, @@ -2123,6 +2128,7 @@ const toolCallErrors = [ AppProviderFailed, AccountNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, ToolNotFound, @@ -2282,6 +2288,7 @@ export const ToolsGroup = HttpApiGroup.make("tools") AppProviderFailed, AccountNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, OAuthReconnectRequired, @@ -2309,6 +2316,7 @@ export const ToolsGroup = HttpApiGroup.make("tools") AppProviderFailed, AccountNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, OAuthReconnectRequired, @@ -2336,6 +2344,7 @@ export const ToolsGroup = HttpApiGroup.make("tools") AppProviderFailed, AccountNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, OAuthReconnectRequired, diff --git a/packages/sdk/src/contracts/webhooks.ts b/packages/sdk/src/contracts/webhooks.ts index 992d740e3e..b6012d06e8 100644 --- a/packages/sdk/src/contracts/webhooks.ts +++ b/packages/sdk/src/contracts/webhooks.ts @@ -23,6 +23,7 @@ import { AppNotFound, AppNotDeployed, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, } from "./apps.ts"; @@ -155,6 +156,7 @@ export const WebhookErrors = [ AccountNotFound, VersionNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, OAuthReconnectRequired, diff --git a/packages/sdk/src/contracts/workflows.ts b/packages/sdk/src/contracts/workflows.ts index 430975cdfe..88be2f2e40 100644 --- a/packages/sdk/src/contracts/workflows.ts +++ b/packages/sdk/src/contracts/workflows.ts @@ -19,6 +19,7 @@ import { AppNotDeployed, AppNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, } from "./apps.ts"; @@ -79,6 +80,7 @@ export const WorkflowErrors = [ AppNotDeployed, AccountNotFound, AccountRequired, + AppLinkCreatorNotAuthorized, AccountChoiceUnsupported, AccountSelectionInvalid, VersionNotFound, diff --git a/packages/sdk/src/implementation/background-accounts.ts b/packages/sdk/src/implementation/background-accounts.ts index b84bf846f7..83c901b004 100644 --- a/packages/sdk/src/implementation/background-accounts.ts +++ b/packages/sdk/src/implementation/background-accounts.ts @@ -80,7 +80,14 @@ export const backgroundAccounts = ( const appRequirements = required("app"); if (appRequirements.length > 0) { - const links = lifecycle === undefined ? [] : yield* lifecycle.appLinks({ app: app.id }); + const links = + lifecycle === undefined + ? [] + : yield* lifecycle.appLinks({ + app: app.id, + requirements: version.requirements.accounts, + source: input.source, + }); const existing = new Set( (yield* readAccounts(db, [...new Set(links.map(({ account }) => account))])).map( (account) => account.id, diff --git a/packages/sdk/src/implementation/oauth.ts b/packages/sdk/src/implementation/oauth.ts index 62df2b085e..a6e7da0b4d 100644 --- a/packages/sdk/src/implementation/oauth.ts +++ b/packages/sdk/src/implementation/oauth.ts @@ -37,6 +37,7 @@ import { } from "./connection-state.ts"; import { requireTargetProvider } from "./connection-target.ts"; import { Account } from "../contracts/account.ts"; +import type { AppLinkCreatorNotAuthorized } from "../contracts/apps.ts"; import { OAuthClientUnavailable, type CheckOAuthSetup, @@ -2969,8 +2970,8 @@ export const makeOAuth = ( ? Effect.succeed(undefined) : lifecycle.accountsResolving(accounts); /** Fail unless the product still authorizes every one of `accounts` in `current`. */ - const enforced = ( - current: Effect.Effect | undefined, StorageError>, + const enforced = ( + current: Effect.Effect | undefined, E>, accounts: readonly StoredAccount[], ) => Effect.flatMap(current, (allowed) => @@ -2989,10 +2990,10 @@ export const makeOAuth = ( allowed: ReadonlySet | undefined, resolution: Resolution, rejected?: JsonObject, - recheck: Effect.Effect = enforced( - authorized([account]), - [account], - ), + recheck: Effect.Effect< + void, + StorageError | AccountUseRefused | AppLinkCreatorNotAuthorized + > = enforced(authorized([account]), [account]), ) => Effect.gen(function* () { if (allowed !== undefined && !allowed.has(account.id)) return yield* new AccountUseRefused(); @@ -3021,7 +3022,10 @@ export const makeOAuth = ( use: CredentialUse, accounts: readonly StoredAccount[], appUses: ReadonlyArray = [], - ): Effect.Effect | undefined, StorageError> => + ): Effect.Effect< + ReadonlySet | undefined, + StorageError | AppLinkCreatorNotAuthorized + > => lifecycle === undefined ? Effect.succeed(undefined) : use.kind === "run" @@ -3077,10 +3081,10 @@ export const makeOAuth = ( readonly provider: ProviderDefinition; } & AccountUses >, - authority: Effect.Effect | undefined, StorageError> = selectionAuthority( - use, - selected, - ), + authority: Effect.Effect< + ReadonlySet | undefined, + StorageError | AppLinkCreatorNotAuthorized + > = selectionAuthority(use, selected), ) => Effect.gen(function* () { const supplied = yield* GrantReadAhead; diff --git a/packages/sdk/src/implementation/tools.ts b/packages/sdk/src/implementation/tools.ts index 520f2d4365..88415f2147 100644 --- a/packages/sdk/src/implementation/tools.ts +++ b/packages/sdk/src/implementation/tools.ts @@ -30,6 +30,7 @@ import { } from "apps/contracts"; import { AccountRequired, + AppLinkCreatorNotAuthorized, AccountSelectionInvalid, AppNotFound, AppNotDeployed, @@ -332,7 +333,7 @@ export interface RecheckAhead { * `ResourceLifecycle.appResolving`. */ readonly appUses: readonly AppAccountUse[]; - readonly result: Exit.Exit, StorageError>; + readonly result: Exit.Exit, StorageError | AppLinkCreatorNotAuthorized>; } /** @@ -932,6 +933,8 @@ export const callFailure = Match.tagsExhaustive({ ToolCallFailed: (failure) => mayHaveWritten(ToolCallFailed, notSentOnly(failure) ? partlyRun(failure) : failure), + AppLinkCreatorNotAuthorized: (failure) => + mayHaveWritten(AppLinkCreatorNotAuthorized, failure), AppProviderFailed: (failure) => mayHaveWritten(AppProviderFailed, failure), AppEvaluationFailed: (failure) => mayHaveWritten(AppEvaluationFailed, failure), ToolElicitationFailed: (failure) => mayHaveWritten(ToolElicitationFailed, failure), diff --git a/packages/sdk/src/implementation/webhooks.ts b/packages/sdk/src/implementation/webhooks.ts index d3088d0aed..37c0601f76 100644 --- a/packages/sdk/src/implementation/webhooks.ts +++ b/packages/sdk/src/implementation/webhooks.ts @@ -74,6 +74,7 @@ const deliveryRefusals = { VersionNotFound: "version-not-found", AccountNotFound: "account-not-found", AccountRequired: "account-required", + AppLinkCreatorNotAuthorized: "app-link-creator-not-authorized", AccountSelectionInvalid: "account-selection-invalid", CredentialsError: "credentials", OAuthReconnectRequired: "oauth-reconnect-required", @@ -719,7 +720,10 @@ export const makeWebhooks = ( // The registration's actor, not the subscription's. Effect.provideService(CurrentActor, state.actor), Effect.provideService(CurrentCaller, systemCaller("webhook", state.actor)), - Effect.catchTag("AccountUseRefused", () => Effect.succeed(undefined)), + Effect.catchTags({ + AccountUseRefused: () => Effect.succeed(undefined), + AppLinkCreatorNotAuthorized: () => Effect.succeed(undefined), + }), ) : undefined; if (context === undefined) refused = true;