fix(mcp): validate org fetch before committing session - #95872
Draft
aashish00021 wants to merge 1 commit into
Draft
fix(mcp): validate org fetch before committing session#95872aashish00021 wants to merge 1 commit into
aashish00021 wants to merge 1 commit into
Conversation
switch-organization wrote orgId to the session cache and returned a success string before/independent of fetching the organization, so an agent switching to an id it cannot access was told the switch worked. Subsequent org-nested reads then failed with opaque 403/404s that didn't mention the switch, and the stale orgId persisted in the token-keyed cache (7-day TTL). switch-project had the identical shape and was already fixed in PostHog#71976. Mirror that pattern here: fetch the organization first, throw a descriptive error naming the recovery path on failure, and only commit orgId (and the cached org) once the fetch succeeds. Fixes PostHog#78629 Signed-off-by: aashish00021 <aashishbhardwaj07@icloud.com>
|
Merging to
After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
An agent calling
switch-organizationwith an organization id it cannot access is told the switch succeeded. The handler wroteorgIdto the session cache and returned a success message before (and independent of) fetching the organization, so a failed fetch was silently ignored. Every later org-scoped call then failed with an opaque 403/404 that never mentioned the switch, and the staleorgIdpersisted in the token-keyed cache for its full 7-day TTL.switch-projecthad the identical shape and was already fixed in #71976 — the fix was never mirrored ontoswitch-organization.Closes #78629
Changes
switch-organizationnow fetches the organization first; only on success does it commitorgIdand the cached org to the session.organizations-getas the recovery path) instead of reporting success. The original API error is preserved ascause, matchingswitch-project's pattern for keeping recoverable not-found/no-access failures out of exception tracking.switch-project.How did you test this code?
services/mcp/tests/unit/switch-organization.test.ts, mirroring the existingswitch-project.test.tspattern: one case asserting a failed fetch does not commitorgIdand preserves the errorcause, one case asserting a successful fetch commitsorgIdand the cached org. Both pass (npx vitest run tests/unit/switch-organization.test.ts tests/unit/switch-project.test.ts— 6/6 passed, including the 4 pre-existingswitch-projectcases, confirming no regression there).npx tsgo --noEmitinservices/mcp: no errors introduced by this change (confirmed identical pre-existing error count with and without the diff, viagit stash/stash pop; all pre-existing errors are unrelated@posthog/quillUI-app build artifacts, not touchingtools/organizationsortools/projects).oxlint --quietandoxfmt --checkon both changed files: clean.switch-projectfix.Automatic notifications
🤖 Agent context
Found via an isolated multi-repo scan for well-defined, unclaimed backend bugs. Root-caused by diffing
organizations/setActive.tsagainst the already-fixedprojects/setActive.ts, which shares the exact same fetch-then-cache shape and already documents the failure mode in its own comments. The fix, tests, lint/format, and typecheck were all done and verified in this session before opening this PR.Autonomy: Human-driven (agent-assisted)