Repository navigation
fix(security): async-safe per-org credential context (Fixes #63) - #188
Merged
Merged
Conversation
…ation (#63) - services/org_env.py: os.environ becomes a context-local view. Org keys live in a ContextVar overlay, so each asyncio task (and to_thread worker) sees only its own org's keys through the unchanged os.getenv call sites. The process environment is never written. subprocess_env() hands the org's keys to kubectl/helm/terraform/executor child processes explicitly. - services/org_context.py: org_agent_credentials is now an async context manager guarded by an OrgGate per agent: same-org scopes run concurrently, another org waits until they leave, clients refresh on the first entry and restore on the last exit (never under a running scope). Release is synchronous so cancellation cannot wedge the gate; nested org switches by the sole holder still work and restore the outer org. - Callers (agent.run, execute_tool, execute_approved_action, queue worker, MCP handlers) use `async with`. - tests/test_org_credential_concurrency.py: overlapping orgs, untouched process env, child env, gate ordering, same-org concurrency, cancellation, nested switch, concurrent execute_tool. The env tests fail on the old code. Fixes #63 Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Fixes #63.
The bug
org_credentials()wrote each org's keys into the process-wideos.environfor the length of a request. Anyawaitinside that scope let another request run, and it read the first org's keys. For example, an MCP tool call fororg-bthat runs whileorg-a's incident is being processed could useorg-a's GitHub token or LLM key. The agent's cached clients (GitHub, cloud SDKs, the LLM client) are attributes of one shared agent, so they could be swapped in the middle of a run in the same way.tests/test_org_credential_concurrency.py::test_overlapping_scopes_see_only_their_own_keysand::test_process_environment_is_never_mutatedfail on main and pass on this branch.The fix
1. Context-local environment (
services/org_env.py). An org's keys live in aContextVaroverlay.os.environis switched, in place, to a subclass whose reads check the overlay first. The ~330 existingos.getenv(...)call sites, and libraries such as boto3 and the Kubernetes client, therefore resolve to the current org's key without changes. Writes still go to the real environment, and the process environment is never mutated. Child processes don't inherit aContextVar, so the executor, helm, terraform and the fix verifier's kubectl now passenv=subprocess_env().2. Org gate for shared clients (
services/org_context.py).org_agent_credentialsis now an async context manager guarded by anOrgGateper agent:org_scope_waiting) until those scopes have left.The callers (
agent.run,execute_tool,execute_approved_action, the queue worker and the MCP handlers) now useasync with. Issue item 3, org scoping forexecute_approved_action, was already in place and now goes through the gate too.Trade-off
Work for a different org waits while another org is using the shared agent. Webhooks still only enqueue, and the queue worker was already serial, so in practice this affects only Slack approvals and MCP calls that overlap with a running incident. Removing the wait needs one agent per org, which I'll file as a follow-up issue.
Tests
execute_toolcalls for two orgs.Note:
tools/executor.pygains one import and anenv=argument, so this will conflict trivially with #186. Whichever merges second needs a one-line rebase.