Skip to content

feat(integrations): restrict connection building to selected teams - #1364

Closed
vsrikar08 wants to merge 16 commits into
feat/1361-team-membershipfrom
feat/1289-connection-audiences
Closed

vsrikar08 wants to merge 16 commits into
feat/1361-team-membershipfrom
feat/1289-connection-audiences

Conversation

@vsrikar08

@vsrikar08 vsrikar08 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

What changed

Connection teams now control who can build with a connection. Publish and connection calls check the current patch owner's access, including after queueing. Anyone who can open the patch can use its permitted handlers; Block guests is unchanged. Connection pages and SDK generation show only connections the person can build with.

Closes #1289. Stacked on #1362; the stack lands together. Implements Allison's approved later decision C. The lower stack and existing remote commits are preserved, with merge conflicts resolved. No dependent PRs were changed.

Look at

  • Owner removal/restoration and reassignment, publish refusal/restoration, hidden connection metadata and restricted Used by names have focused coverage.
  • Connection revisions, subscription keys, mismatch warnings, activation plumbing and unused reference helpers are removed. Query subscriptions with PostHog, Pidgeon, Attio and IMAP declarations run without connection revision entries.
  • One metadata lookup interface replaces duplicated helpers. Invalid team selections return 422; unauthorized administration returns 403. Logs filter restricted names, and queued Postgres checks preserve session/service refusals.

How it was verified

  • The previous CI failures were reproduced locally. The subscription reader still selected the removed audience_revision column; it now reads lifecycle_revision, which also advances for audience edits. Synthetic callback fixtures now store current patch owners, and team-page checks follow the approved membership rule.
  • The reproduced failures pass: 56 tests across local subscriptions, Attio callbacks, dev reads, team settings and revision fences; another 18 tests cover hosted guests/members and Pidgeon/IMAP callbacks.
  • The formerly failing two-viewer browser subscription test passed locally. The wider browser invocation exited without a complete result; full browser, tier 2, live external and upgrade-rehearsal confirmation remains with CI.
  • The canonical visibility adapter passed 23 connection-page/audience tests. Server and test typechecks, formatting, lint and whitespace checks passed. Earlier 160-test and native Postgres evidence remains applicable.
  • Connection metadata now consumes a Patches-owned visibility adapter using the canonical audience decision; Integrations no longer copies patch-membership policy. Existing filtered usage/deletion UI behavior is unchanged.

Release impact

  • Breaking — requires action from anyone already running this
  • Not breaking — no action required

Existing connections migrate to Company without credential, snapshot or Block guests changes. This PR is part of the one-way role/audience stack.

  • What does a revert leave behind? Connection audience columns and team grants.
  • Does the release before it still work with that? It ignores team connection grants and cannot safely enforce restrictions once they are written. The complete stack must land together; rollback across it requires a forward fix.

Implemented with GPT-6 in the Codex harness.

@github-actions github-actions Bot added area:server Touches the server area area:cli Touches the cli area area:core Touches the core area area:docs Touches the docs area area:primitives Patch tables and files area:integrations Company connections and integration implementations area:ui Portal and first-party browser pages, shared rendering and styles labels Nov 9, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T06:59:18.666623Z a67765f New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions github-actions Bot added the size:l 501-1,000 changed lines label Nov 9, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2e04cc3558

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

ConnectionStore.ConnectionStore,
yield* ConnectionStore.ConnectionStore
),
Effect.mapError(() => new Execution.AccessDenied({}))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve refusal types from the queued Postgres recheck

If the viewer's session expires or the authority lookup fails while a Postgres call waits for a backend, this mapper converts every recheck failure into PostgresAccessDenied (403), including SessionExpired, SessionRefreshRequired, and SourceUnavailable. The handler and callback journal consequently report a connection-permission failure, with a message asking an administrator to reconnect the database, instead of the actual session or service failure. Preserve the typed recheck failures through the execution error channel rather than replacing all of them with access denial.

AGENTS.md reference: AGENTS.md:L101-L101

Useful? React with 👍 / 👎.

@github-actions github-actions Bot added size:xl Over 1,000 changed lines; review in multiple passes and removed area:primitives Patch tables and files size:l 501-1,000 changed lines labels Nov 9, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner

Review of #1364 (connection audiences). Allison's review, posted by Fermy (his agent). Line numbers are at 3c13f9b7. It's a static review; nothing was run. Stack-wide points are explained in the stack review on #1192.

The model and the per-call enforcement are right:

  • audience_kind plus connection_team_grants, with a foreign key to teams.
  • canUse is checked on each call, at admission and again after queueing, on every integration: Postgres, PostHog, Pidgeon, Attio, IMAP and cloud-backed dev reads.
  • Discovery is filtered.
  • The super-admin check runs inside the store transaction.
  • Existing connections migrate as Company.

The live-revision half is machinery for a path that doesn't exist.

Waiting on Allison (C)

  • Outside guests skip the team list. For a guest, accessible checks only !blockGuests (SqlConnectionStore.ts:1592), and ConnectionStore.test.ts:259-264 pins that behavior.
  • Example: a Postgres connection is limited to Design, and a Company patch has a guest: true action. A guest's call succeeds, while a colleague outside Design gets access_denied.
  • Recommendation: a team audience admits only its team's members, and Block guests governs Company connections.

This PR

  • Blocker (traced, not run): patchy dev live queries fail for tier 2 patches that declare PostHog, Pidgeon, Attio or IMAP.
    • QuerySubscriptions.ts:105 adds connection:<id> for every declared connection.
    • ConnectionRevisions.ts:24 fails with SourceUnavailable for any id that has no revision.
    • Dev's authorityRevisions only knows the Postgres fixtures that devResources.ts builds.
    • So any useQuery in such a patch errors, even one that reads only its own table.
  • Should-fix: the connection fencing is dead.
    • Connection ops are allowed only in actions (CallbackGateway.ts's allowed), and live queries can't call actions. So no live result can depend on a connection.
    • Drop all of it:
      • authority_revision and its trigger (integrations/migrations.ts:94-102);
      • ConnectionRevisions.ts and how the server and dev wire it in;
      • the connection: keys in QuerySubscriptions.ts;
      • the trace in CallbackGateway.
    • That removes the blocker above, plus a SELECT … FROM connections on every query run.
    • If you keep it, note the revision has two writers: the trigger, and setAudience's authority_revision + 1 (SqlConnectionStore.ts:1704).
  • Should-fix (fix(access): protect patch metadata and simplify agent checks #1367): "Used by" lists every declaring patch, restricted ones included (ConnectionPage.ts:255, store.usage(companyId)). Filter it by the viewer.
  • Should-fix: InvalidConnectionAudience is three errors in one.
    • It folds "not a super admin", "teams switched off" and "unknown team" into one 403, worded for the switch: "Team audiences require completed access enforcement" (ConnectionStore.ts:234).
    • An unknown team should be a 422. The switch wording goes with item 2.
  • Nit: connectionWarnings runs too many queries and can't report its own failure.
    • It runs two or three queries per declared connection inside the publish or share transaction (Patches.ts:1948); one batch would do.
    • Its fallback warning, "Could not check…", catches SQL errors after Postgres has already aborted the transaction, so that warning can never be delivered.
  • Nit: the portal log names connections from the company-wide list (PortalPages.ts:938), while patchy logs uses listFor.
  • Nits (code):
    • listFor and detailFor duplicate list and detail; an optional viewer argument would do.
    • connectionRecheck's type is written twice (definition.ts:50).
    • CallbackGateway.ts:219 indexes manifest.uses[...] without the Object.hasOwn guard used five lines below.
  • Mirror:

Stack-wide

  • Activation switch (item 2):
    • SqlConnectionStore.ts:1578 and :1679;
    • the switch wording in InvalidConnectionAudience;
    • packages/core/src/AudienceActivation.ts, whose id still says @patchy/patches/….
  • Dead code (item 6): ConnectionStore.teamReferences, plus its SQL and its dev stub.

Tests

  • Fix: ConnectionStore.test.ts:101-136 fakes a failure by renaming the connections table on the shared migrated database. Inject a failing layer instead.
  • Drop:
    • "audience revisions and references roll back together", which tests Postgres rollback;
    • "a targeted detail read is independent of unrelated inventory", which pins a refactor;
    • the connection-revision subscription cases, along with the fencing.
  • Keep:
    • the per-call canUse revoke-and-restore cases;
    • ConnectionsApi "hides connection metadata…";
    • SdkApi "direct generation uses its required viewer".

@allisonmahmood

Copy link
Copy Markdown
Owner

Allison's decision on C (all four on #1192). Posted by Fermy (his agent). This replaces the recommendation under "Waiting on Allison (C)" above.

If a patch is shared with you, its connections work for you, within what the patch's code does. That holds for guests and for colleagues outside the connection's teams. A connection's teams decide who can build with it:

  • Build: the patch's owner must be on one of the connection's teams.
    • Publish refuses otherwise.
    • connectionRecheck (definition.ts:50) checks the patch's owner instead of the caller. Removing the owner from the team then stops the connection until the patch is reassigned or the owner is added back.
  • Use: anyone who can open the patch can call its handlers, including those that use the connection. Block guests is unchanged.
  • What goes:
    • the caller check in accessible (SqlConnectionStore.ts:1592) and its guest test (ConnectionStore.test.ts:259-264);
    • the "some viewers may be unable" warnings in connectionWarnings (Patches.ts:1948-1974);
    • with nothing per caller left to fence, the connection revisions and connection: subscription keys. That also removes the patchy dev blocker above.
  • Connection pages and SDK generation still list only the connections the person can build with (listFor).

@vsrikar08 vsrikar08 added the one-way Rolling production back past this PR would break the release before it label Nov 10, 2026
@vsrikar08 vsrikar08 changed the title feat(integrations): restrict connection access to selected teams feat(integrations): restrict connection building to selected teams Nov 10, 2026
@github-actions github-actions Bot added area:ci CI, shared testing infrastructure, development scripts and tooling and removed area:core Touches the core area labels Nov 10, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0aae1b1aef

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +379 to +380
const usage =
(yield* store.usage(viewer.company.id, viewer.user.id)).get(id) ?? ConnectionStore.unused;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the complete dependency guard when filtering patch names

When a connection is declared only by patches the super admin cannot open (or by disabled patches), this viewer-filtered query returns no usage and defaults to ConnectionStore.unused. Consequently, the connection appears in the Unused filter and offers Delete, although SqlConnectionStore.delete still checks all stored declarations and rejects the operation with ConnectionInUse. Keep patch names viewer-filtered, but derive the declared flag used by the administration controls from the complete dependency set.

Useful? React with 👍 / 👎.

Comment on lines +851 to +852
ConnectionAccessDenied: (error) =>
replayOrRespond(rejected("connection_access_denied", error.message)),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Clear pending CLI publishes after connection access refusals

When a repo publish receives this new 422 refusal, packages/patchy/src/Api.ts's isDefinitivePublishRefusal does not recognize connection_access_denied, so sendPublish retains the pending request. Subsequent publishes replay that saved payload before building the current source (commands.ts:752–761), meaning removing or replacing the forbidden connection in the repo cannot repair the failure: the CLI keeps submitting the original declaration. Add this code to the definitive-refusal classifier and its tests so a rejected attempt is cleared and the corrected repo can be published.

AGENTS.md reference: AGENTS.md:L73-L73

Useful? React with 👍 / 👎.

@github-actions github-actions Bot added the area:primitives Patch tables and files label Nov 10, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner

Superseded by #1416 (the rollback bridge) and #1417 (teams), which carry this work forward under Allison's decisions on #1192 (#1192 (comment)). Closing without merging; Srikar is co-author there. Posted by Fermy (Allison's agent).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ci CI, shared testing infrastructure, development scripts and tooling area:cli Touches the cli area area:docs Touches the docs area area:integrations Company connections and integration implementations area:primitives Patch tables and files area:server Touches the server area area:ui Portal and first-party browser pages, shared rendering and styles one-way Rolling production back past this PR would break the release before it size:xl Over 1,000 changed lines; review in multiple passes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants