Skip to content

fix(kv): enforce automation permissions on user-auth KV access (OHE-4172) - #762

Merged
hieptl merged 1 commit into
mainfrom
hiep/ohe-4172-security-any-org-member-can-read-change-and
Nov 18, 2026
Merged

hieptl merged 1 commit into
mainfrom
hiep/ohe-4172-security-any-org-member-can-read-change-and

Conversation

@hieptl

@hieptl hieptl commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Why

The KV API takes two kinds of credential: the per-run KV token, and a normal user credential (API key, X-Session-API-Key or session cookie) plus ?automation_id=. The user path, added in #445, only checked that the automation belongs to the caller's organization. Any caller resolved to that organization could therefore read, change and delete the KV data of every automation in it, including automations that run as another user or as a service account, and so could any conversation running with a member's key. A caller that arrives with an org_id but an empty permission list (the OpenHands server can return that when the caller has no active role in the organization) also got through, although every other router answers 403.

Ticket: https://linear.app/all-hands-ai/issue/OHE-4172/security-any-org-member-can-read-change-and-delete-any-automations-kv

Summary

  • User-authenticated KV requests now need view_automations. Without it the answer is 403 Requires view_automations permission, decided before the automation is looked up, so it does not reveal which ids exist.
  • A write also has to pass _assert_can_manage, the rule that already guards editing, deleting, dispatching and cancelling an automation: manage_automations, or being the user the automation runs as. Otherwise the answer is 403 and nothing is stored.
  • get_kv_auth_context treats every method except GET as a write. The three read routes (list, get, len) are the only GET routes; the other ten all change data, lpop and rpop included. No route signature changes.
  • A service account's key gets no KV write, not even to the automation it runs, the same as for edits. Its runs keep writing through AUTOMATION_KV_TOKEN; that path is unchanged.
  • Reads stay open to every holder of view_automations, so a member can still inspect any automation's state.
  • The KV feature map (F12) and the error table in docs/kv-store-design.md describe the new rule.

Testing

  • uv run pytest tests/test_kv_user_auth.py: 38 passed. The new TestUserAuthPermissions covers a member reading a teammate's automation, a member refused with 403 on each of the ten write routes with the stored values unchanged, an admin writing a teammate's automation, a caller without view_automations, and a service account refused a write to the automation it runs. With kv_router.py from main, 12 of these fail: the ten write routes, the missing permission and the service account.
  • The KV test files, tests/test_session_commit_scope.py and tests/test_verify_automation_skill.py: 337 passed, including tests/test_kv_postgres_concurrency.py against PostgreSQL.
  • Full suite: 2991 passed, 7 skipped, 7 failed. The seven (four in tests/test_router.py, three in tests/test_draft_router.py) need a MinIO endpoint at localhost:9000 and fail the same way on main locally; unrelated to this change.
  • ruff check, ruff format --check, pycodestyle and pyright are clean on the changed Python files.
  • Not exercised against a live deployment.

Notes

  • Behaviour change for anything that sends a user key with ?automation_id=: a write now needs an admin's or owner's key, or the key of the user the automation runs as. The run scripts in OpenHands/extensions use the KV token and are not affected. The opt-in structured_publication mode of github-pr-reviewer writes its lease keys with REVIEW_COORDINATION_API_KEY; that key must now belong to an admin or owner, or to the user the namespace automation runs as.
  • Local mode is unchanged: the local key carries both permissions.
  • An admin's or owner's key can still write any automation's KV, as it can edit any automation. Telling a person's key from a key held by a running conversation needs scoped API keys, which Organization-Scoped KV Tokens for Debugging and Management #201 is waiting on.

Jev-Fast-Audit

⚡ Jev fast audit · estimates · 0.37s · commit 1ba13a0
Strongest signal: No primary concern selected.
Evidence: No primary concern to locate.
Coverage: complete supplied coverage; 14/14 hunks, 4/4 files.

All estimates and evidence
Estimate Likelihood / value Direct evidence
SQL injection 4.0% No direct hunk selected
Command injection 3.0% No direct hunk selected
Weakened authentication 13.0% No direct hunk selected
Weakened authorization 14.0% No direct hunk selected
Contract regression 51.0% F003H003 · openhands/automation/kv_router.py:186–206
Data loss 5.0% No direct hunk selected
Sensitive data disclosure 4.0% No direct hunk selected
Unexpected data transfer 3.0% No direct hunk selected
Credential misuse 19.0% No direct hunk selected
Untrusted instruction authority 3.0% No direct hunk selected
Package source redirection 4.0% No direct hunk selected
Unverified remote execution 2.0% No direct hunk selected
Privileged environment access 2.0% No direct hunk selected
Security assessment bypass 8.0% No direct hunk selected
Prohibited workload 2.0% No direct hunk selected
Primary concern None selected; confidence 66.0% No primary concern to locate

…172)

The user-authenticated path of the KV API checked only that the
automation belonged to the caller's org. Any caller resolved to that org
could read, change and delete the KV data of every automation in it,
including a caller holding no permissions at all, which every other
router refuses.

The path now applies the rules the rest of the service uses:

- Every request needs view_automations (403 otherwise), checked before
  the automation is looked up.
- A write (any method but GET) must also pass _assert_can_manage, the
  rule for editing the automation: manage_automations, or being the user
  it runs as. A service account's key gets no write, as for edits; its
  runs keep writing through the per-run KV token.

Reads stay open to every holder of view_automations, and the KV token
path is unchanged.

Tests cover a member reading a teammate's automation and being refused
on every write route, an admin writing it, a caller without
view_automations, and a service account on the automation it runs. The
KV feature map and the design doc's error table state the new rule.
@github-actions github-actions Bot added the type: fix A bug fix label Nov 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Warning

Your comment is too long (maximum is 65536 characters), so the coverage report was not added. See the job log for how to reduce it.

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This review was posted by an AI agent (OpenHands).

Reviewed at head 1ba13a0fe8a358cfc39e03d0d391655dc7a0bdee. The change belongs here: it closes an authorization gap in the Automation Service's own KV API, matching the repo rule that org and automation ownership be authorized independently at every mutation boundary.

What I verified against the workspace code:

  • Gating order is right. _verify_automation_access rejects a caller without view_automations with 403 before any automation lookup (no id-existence leak), keeps the cross-org 404, and only then applies _assert_can_manage for writes. This mirrors require_permission usage in every other router and closes the empty-permissions hole from #445.
  • Write classification is complete. All 13 KV routes depend on get_kv_auth_context; exactly the three read routes (list, get, len) are GET, and the ten mutating routes (PUT/PATCH/DELETE, incr/decr, lpush/rpush/lpop/rpop, batch) are classified as writes via request.method != "GET". The heuristic fails closed for future routes.
  • _assert_can_manage reuse is consistent with edit/delete/dispatch/cancel semantics: manage_automations, or the run-as user, and no creator escape for service accounts. Its documented precondition (a prior view_automations check) is satisfied by the ordering above. The run's KV-token path short-circuits before user auth and is unchanged.
  • Tests prove the behavior. uv run pytest tests/test_kv_user_auth.py → 38 passed locally, including the new member-403 cases asserting stored values stayed unchanged across all ten write routes, admin write allowed, missing-permission 403, and service-account write refusal.
  • Docs and CI. The F12 feature map and the docs/kv-store-design.md error table describe the new 403s; all GitHub Actions checks pass on this head.

No material correctness, security, compatibility, or acceptance issues found.

✅ APPROVED

@hieptl hieptl self-assigned this Nov 17, 2026

@tofarr tofarr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🍰

@hieptl
hieptl merged commit 83bdc23 into main Nov 18, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants