-
Notifications
You must be signed in to change notification settings - Fork 10
fix(connectors): stop granting act-as-artist authority off the flat account_artist_ids roster edge (P0b) #772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { checkConnectorAuthority } from "../checkConnectorAuthority"; | ||
|
|
||
| import { getAccountOrganizations } from "@/lib/supabase/account_organization_ids/getAccountOrganizations"; | ||
| import { selectArtistOrganizationIds } from "@/lib/supabase/artist_organization_ids/selectArtistOrganizationIds"; | ||
| import { selectAccountOrganizationIds } from "@/lib/supabase/account_organization_ids/selectAccountOrganizationIds"; | ||
| import { selectAccountWorkspaceId } from "@/lib/supabase/account_workspace_ids/selectAccountWorkspaceId"; | ||
| import { validateOrganizationAccess } from "@/lib/organizations/validateOrganizationAccess"; | ||
| import { selectAccountArtistId } from "@/lib/supabase/account_artist_ids/selectAccountArtistId"; | ||
|
|
||
| vi.mock("@/lib/const", () => ({ | ||
| RECOUP_ORG_ID: "recoup-admin-org-id", | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/account_organization_ids/getAccountOrganizations", () => ({ | ||
| getAccountOrganizations: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/artist_organization_ids/selectArtistOrganizationIds", () => ({ | ||
| selectArtistOrganizationIds: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/account_organization_ids/selectAccountOrganizationIds", () => ({ | ||
| selectAccountOrganizationIds: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/account_workspace_ids/selectAccountWorkspaceId", () => ({ | ||
| selectAccountWorkspaceId: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/organizations/validateOrganizationAccess", () => ({ | ||
| validateOrganizationAccess: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/account_artist_ids/selectAccountArtistId", () => ({ | ||
| selectAccountArtistId: vi.fn(), | ||
| })); | ||
|
|
||
| describe("checkConnectorAuthority", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked(getAccountOrganizations).mockResolvedValue([]); | ||
| vi.mocked(selectArtistOrganizationIds).mockResolvedValue([]); | ||
| vi.mocked(selectAccountOrganizationIds).mockResolvedValue([]); | ||
| vi.mocked(selectAccountWorkspaceId).mockResolvedValue(null); | ||
| vi.mocked(validateOrganizationAccess).mockResolvedValue(false); | ||
| }); | ||
|
|
||
| it("grants self access without any database calls", async () => { | ||
| const result = await checkConnectorAuthority("account-123", "account-123"); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(getAccountOrganizations).not.toHaveBeenCalled(); | ||
| expect(selectArtistOrganizationIds).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("denies a roster-only manager (bare account_artist_ids row, no org tie)", async () => { | ||
| // Even if a roster row exists, connector authority must never read it. | ||
| vi.mocked(selectAccountArtistId).mockResolvedValue({ | ||
| id: "aai-1", | ||
| artist_id: "artist-456", | ||
| pinned: false, | ||
| }); | ||
|
|
||
| const result = await checkConnectorAuthority("account-123", "artist-456"); | ||
|
|
||
| expect(result).toBe(false); | ||
| expect(selectAccountArtistId).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("grants access when caller and artist share an organization", async () => { | ||
| vi.mocked(selectArtistOrganizationIds).mockResolvedValue([{ organization_id: "org-1" }]); | ||
| vi.mocked(selectAccountOrganizationIds).mockResolvedValue([{ organization_id: "org-1" }]); | ||
|
|
||
| const result = await checkConnectorAuthority("account-123", "artist-456"); | ||
|
|
||
| expect(selectArtistOrganizationIds).toHaveBeenCalledWith("artist-456"); | ||
| expect(selectAccountOrganizationIds).toHaveBeenCalledWith("account-123", ["org-1"]); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it("grants RECOUP_ORG staff access to any target", async () => { | ||
| vi.mocked(getAccountOrganizations).mockResolvedValue([ | ||
| { | ||
| id: "aoi-1", | ||
| account_id: "admin-account", | ||
| organization_id: "recoup-admin-org-id", | ||
| updated_at: null, | ||
| organization: null, | ||
| }, | ||
| ]); | ||
|
|
||
| const result = await checkConnectorAuthority("admin-account", "artist-456"); | ||
|
|
||
| expect(result).toBe(true); | ||
| expect(selectArtistOrganizationIds).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("grants access to a workspace the caller owns", async () => { | ||
| vi.mocked(selectAccountWorkspaceId).mockResolvedValue({ workspace_id: "workspace-789" }); | ||
|
|
||
| const result = await checkConnectorAuthority("account-123", "workspace-789"); | ||
|
|
||
| expect(selectAccountWorkspaceId).toHaveBeenCalledWith("account-123", "workspace-789"); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it("grants access to an organization the caller belongs to", async () => { | ||
| vi.mocked(validateOrganizationAccess).mockResolvedValue(true); | ||
|
|
||
| const result = await checkConnectorAuthority("account-123", "org-target"); | ||
|
|
||
| expect(validateOrganizationAccess).toHaveBeenCalledWith({ | ||
| accountId: "account-123", | ||
| organizationId: "org-target", | ||
| }); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it("fails closed when artist org lookup errors", async () => { | ||
| vi.mocked(selectArtistOrganizationIds).mockResolvedValue(null); | ||
|
|
||
| const result = await checkConnectorAuthority("account-123", "artist-456"); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
| it("fails closed when account org lookup errors", async () => { | ||
| vi.mocked(selectArtistOrganizationIds).mockResolvedValue([{ organization_id: "org-1" }]); | ||
| vi.mocked(selectAccountOrganizationIds).mockResolvedValue(null); | ||
|
|
||
| const result = await checkConnectorAuthority("account-123", "artist-456"); | ||
|
|
||
| expect(result).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { RECOUP_ORG_ID } from "@/lib/const"; | ||
| import { getAccountOrganizations } from "@/lib/supabase/account_organization_ids/getAccountOrganizations"; | ||
| import { selectArtistOrganizationIds } from "@/lib/supabase/artist_organization_ids/selectArtistOrganizationIds"; | ||
| import { selectAccountOrganizationIds } from "@/lib/supabase/account_organization_ids/selectAccountOrganizationIds"; | ||
| import { selectAccountWorkspaceId } from "@/lib/supabase/account_workspace_ids/selectAccountWorkspaceId"; | ||
| import { validateOrganizationAccess } from "@/lib/organizations/validateOrganizationAccess"; | ||
|
|
||
| /** | ||
| * Check whether a caller has act-as authority over a target account for | ||
| * connector operations (authorize, execute, disconnect, tool exposure). | ||
| * | ||
| * Interim P0b cut (chat#1860): unlike `checkAccountArtistAccess`, this | ||
| * NEVER reads the bare `account_artist_ids` roster edge — a roster row | ||
| * alone must not grant use of another account's OAuth connectors. | ||
| * | ||
| * Authority is granted when: | ||
| * 1. Target is the caller itself, OR | ||
| * 2. Caller is a RECOUP_ORG member (admin bypass), OR | ||
| * 3. Caller shares an organization with the target artist, OR | ||
| * 4. Target is a workspace the caller owns, OR | ||
| * 5. Target is an organization the caller belongs to | ||
| * | ||
| * Fails closed: any database error results in denied authority. | ||
| * | ||
| * @param callerAccountId - The authenticated caller's account ID | ||
| * @param targetAccountId - The account whose connectors are being used | ||
| * @returns true if the caller has connector authority over the target | ||
| */ | ||
| export async function checkConnectorAuthority( | ||
| callerAccountId: string, | ||
| targetAccountId: string, | ||
| ): Promise<boolean> { | ||
| // 1. Self-access | ||
| if (targetAccountId === callerAccountId) return true; | ||
|
|
||
| // 2. Admin bypass: RECOUP_ORG staff | ||
| const callerOrgs = await getAccountOrganizations({ accountId: callerAccountId }); | ||
| if (callerOrgs.some(m => m.organization_id === RECOUP_ORG_ID)) return true; | ||
|
|
||
| // 3. Org co-membership with a target artist | ||
| const targetOrgs = await selectArtistOrganizationIds(targetAccountId); | ||
| if (targetOrgs?.length) { | ||
| const orgIds = targetOrgs.map(o => o.organization_id).filter((id): id is string => Boolean(id)); | ||
| if (orgIds.length) { | ||
| const sharedOrgs = await selectAccountOrganizationIds(callerAccountId, orgIds); | ||
| if (sharedOrgs?.length) return true; | ||
| } | ||
| } | ||
|
|
||
| // 4. Workspace owned by the caller | ||
| const isWorkspace = await selectAccountWorkspaceId(callerAccountId, targetAccountId); | ||
| if (isWorkspace) return true; | ||
|
|
||
| // 5. Organization the caller belongs to | ||
| return validateOrganizationAccess({ | ||
| accountId: callerAccountId, | ||
| organizationId: targetAccountId, | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Connector authority can still be granted after an artist-org or account-org lookup error because
nullis treated like “no rows” and the function falls through to later grants. Returningfalseexplicitly on thosenullresults keeps the fail-closed contract for connector operations.Prompt for AI agents