Skip to content

fix(security): async-safe per-org credential context (Fixes #63) - #188

Merged
OkiriGabriel merged 1 commit into
mainfrom
fix/org-credential-context-63
Nov 24, 2026
Merged

OkiriGabriel merged 1 commit into
mainfrom
fix/org-credential-context-63

Conversation

@OkiriGabriel

@OkiriGabriel OkiriGabriel commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #63.

The bug

org_credentials() wrote each org's keys into the process-wide os.environ for the length of a request. Any await inside that scope let another request run, and it read the first org's keys. For example, an MCP tool call for org-b that runs while org-a's incident is being processed could use org-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_keys and ::test_process_environment_is_never_mutated fail on main and pass on this branch.

The fix

1. Context-local environment (services/org_env.py). An org's keys live in a ContextVar overlay. os.environ is switched, in place, to a subclass whose reads check the overlay first. The ~330 existing os.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 a ContextVar, so the executor, helm, terraform and the fix verifier's kubectl now pass env=subprocess_env().

2. Org gate for shared clients (services/org_context.py). org_agent_credentials is now an async context manager guarded by an OrgGate per agent:

  • Scopes for the same org run concurrently and share the clients.
  • A scope for a different org waits (logging org_scope_waiting) until those scopes have left.
  • Clients are rebuilt when the first scope of an org enters and restored when the last one leaves, never while another scope is still using them.
  • Release is synchronous, so a cancelled task can't leave the gate locked.
  • A nested switch to another org by the only task inside the gate still works, and the outer org is restored afterwards.

The callers (agent.run, execute_tool, execute_approved_action, the queue worker and the MCP handlers) now use async with. Issue item 3, org scoping for execute_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

  • 8 new concurrency tests: overlapping orgs; the process env is untouched and a child process sees the org's key; gate ordering; same-org concurrency where the last scope out restores; cancellation doesn't block the gate; a nested switch restores the outer org; concurrent execute_tool calls for two orgs.
  • The existing isolation tests pass unchanged.
  • Full suite: 1207 passed, 4 skipped. Flake8 (CI selection) and bandit are clean.

Note: tools/executor.py gains one import and an env= argument, so this will conflict trivially with #186. Whichever merges second needs a one-line rebase.

…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]>
@OkiriGabriel
OkiriGabriel merged commit ba48a16 into main Nov 24, 2026
16 checks passed
@OkiriGabriel
OkiriGabriel deleted the fix/org-credential-context-63 branch November 26, 2026 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SECURITY] Replace global os.environ overlay with async-safe per-org credential context

1 participant