Skip to content

feat(app-server): enforce an agent profile's secret_refs scope - #364

Closed
simonrosenberg wants to merge 6 commits into
mainfrom
enterprise-profile-secret-refs
Closed

simonrosenberg wants to merge 6 commits into
mainfrom
enterprise-profile-secret-refs

Conversation

@simonrosenberg

@simonrosenberg simonrosenberg commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

  • A human has tested these changes.

AGENT:


No longer blocked: OpenHands/software-agent-sdk#4931 shipped in openhands-sdk 1.48.0 and main pins 1.50.1, so secret_refs is a field on AgentProfile here.

Why

OpenHands/software-agent-sdk#5030 + software-agent-sdk#4931 add secret_refs to AgentProfileBase: a per-profile allow-list over the user's saved secrets, so a "Code Exploration" agent can be denied the deploy tokens and the production database URL.

The SDK's enforcement point is one this app-server never reaches. The filter lives in conversation_service.start_conversation, inside if request.agent_profile_id is not None:. Cloud resolves the profile itself — settings_store.load(resolve_agent_profile=True, override_agent_profile_id=...) — and builds the outgoing request from the resolved agent:

create_kwargs: dict[str, Any] = {'agent': agent, 'user_id': laminar_user_id}

Every agent_profile_id in that file is an internal override_agent_profile_id for local settings resolution; none becomes a field on the outgoing StartConversationRequest. So a profile saved with a scope resolved and launched with every secret, silently — a control that persists and displays but restricts nothing, which is worse than not offering one. Canvas currently hides it on cloud for that reason (OpenHands/OpenHands#17237); this PR is what lets it be turned back on.

Summary

  • Settings.active_agent_profile_secret_refs carries the resolved profile's allow-list, alongside the active_agent_profile_id / _revision provenance already carried for the same reason: resolution replaces agent_settings with an AgentSettings dump, which has no field for it.
  • SaasSettingsStore._resolve_active_agent_profile returns it (3-tuple → 4-tuple); load() puts it on the settings.
  • _apply_profile_secret_scope narrows the conversation's secrets, applied in both builders (standard and ACP) after the API-secrets merge, so a caller sending extra secrets cannot widen its own scope.

Semantics match openhands.sdk.profiles.resolver.filter_profile_secrets exactly:

  • Strict — nothing is added back, for either agent kind. A profile that omits its own ACP provider credential, or a git provider token, does not receive it.
  • A ref matching no supplied secret is a no-op, not an error: this is an allow-list over what a launch assembles, so unlike mcp_server_refs it cannot dangle.
  • None (the default, and every non-profile launch) leaves the secret set byte-identical.

store() needs no new guard — it already refuses any settings carrying active_agent_profile_id, which is always set alongside the new field.

Issue Number

Closes OpenHands/OpenHands#344

How to Test

uv run pytest tests/unit/test_agent_profiles.py -q      # 55 passed
uv run pytest tests/unit -q                             # 5724 passed

Because the pinned SDK predates the field, the suite above needs the SDK branch:

uv pip install -e /path/to/software-agent-sdk/openhands-sdk   # branch: feat-profile-secret-refs
uv run --no-sync pytest tests/unit/test_agent_profiles.py -q

Manually: save several secrets, create an agent profile with secret_refs naming a subset, start a conversation with it, and confirm the conversation receives only those.

Video/Screenshots

Filter behavior against the real UserInfo / resolver on this branch, all four cases:

scoped   (['DATADOG_API_KEY'])       -> ['DATADOG_API_KEY']
empty    ([])                        -> []
unscoped (None)                      -> ['ANTHROPIC_API_KEY', 'DATADOG_API_KEY', 'GITHUB_TOKEN']
no-match (['GONE'])                  -> []

with agent_profile_secret_scope:filtered logged (dropped names only, never values). test_resolution_carries_secret_refs_out_of_the_store covers the half that would otherwise fail silently — if resolution stopped carrying the refs, the filter would simply never fire.

Full unit suite: 5724 passed. One unrelated pre-existing failure on macOS (test_process_sandbox_service.py::test_default_values, /tmp vs /var/folders TMPDIR) — verified identical on main.

Deployment coverage

Traced each install shape rather than assuming they share a path:

Install Settings store Behavior
Cloud / SaaS SaasSettingsStore resolves profiles, carries the refs, filter applies
Replicated / self-hosted on the file store FileSettingsStore load() discards the resolve flags — no agent-profile concept, refs stay None, filter is a pass-through (unchanged behavior)
OSS local agent-server n/a enforced by the SDK's own agent_profile_id branch (software-agent-sdk#4931)

Checks behind that table:

  • AuthUserContext._user_info_from_settings rebuilds UserInfo via **settings.model_dump(...), so the new field survives the projection — verified live, and now pinned by test_secret_refs_survive_the_settings_to_user_info_projection. This is the seam that would have disabled the scope silently.
  • SpecifyUserContext.get_user_info raises NotImplementedError, so it is not a live conversation-start path.
  • live_status_app_conversation_service is the only place that builds a StartConversationRequest; both of its builders load user with resolve_agent_profile=True and neither reassigns user before the filter.
  • Nothing mutates secrets between the filter and create_kwargs in either builder, so the filtered map is what ships.
  • store() needed no new guard: it already refuses any settings carrying active_agent_profile_id, which is always set alongside the new field.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

Merge order. This needs three things, in order:

  1. software-agent-sdk#4931 merged and released.
  2. pyproject.toml: openhands-sdk== bumped to that release.
  3. dev_config/python/.pre-commit-config.yaml: the mypy hook's additional_dependencies pins openhands-sdk==1.39.1 in its own isolated env — bump it too, or mypy reports "OpenHandsAgentProfile" has no attribute "secret_refs" regardless of the project venv.

Until (2) and (3), mypy fails on storage/saas_settings_store.py. I deliberately did not use getattr(profile, 'secret_refs', None) to paper over it: the attribute is real once the SDK lands, and a defensive read would silently no-op if the field were ever renamed.

Git provider tokens are in scope, by design. _setup_conversation_secrets injects GITHUB_TOKEN / GITLAB_TOKEN / … on the same channel, so a scoped profile that omits them loses repo auth. That is the honest outcome of the saved configuration, and it matches the SDK — but it is the sharpest edge here, and worth a deliberate look during review. These names are already listed in OVERRIDABLE_SYSTEM_SECRETS.

Follow-up: once this ships, re-enable the control for cloud backends in canvas (drop the kind === "cloud" short-circuit in agentProfileSupportsSecretRefs, OpenHands/OpenHands#17237).

🤖 Generated with Claude Code


Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-8167d30

Cloud resolves the agent profile itself and sends a resolved agent to the
agent-server, never agent_profile_id, so the SDK's filter — which lives in the
agent-server's profile branch — never ran here. A profile saved with a scope
launched with every secret, silently.

Resolution now carries the profile's secret_refs out alongside the provenance
it already returns (the resolved dump is an AgentSettings, which has no field
for it), and both conversation builders narrow their secrets to it after the
API merge, so a caller sending extra secrets cannot widen the scope either.

Strict, matching the SDK: nothing is added back for either agent kind, so a
profile that omits its own ACP provider credential or a git provider token
does not receive it. A ref matching no supplied secret is a no-op — this is an
allow-list over what a launch assembles, so unlike mcp_server_refs it cannot
dangle.

Requires an openhands-sdk release containing software-agent-sdk#4931; until
the pin in pyproject.toml and the mypy hook's additional_dependencies both
move, secret_refs is not a field on AgentProfile and mypy reports it missing.

Closes #344

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added the type: feat A new feature label Sep 11, 2026
simonrosenberg added a commit to OpenHands/OpenHands that referenced this pull request Sep 11, 2026
The gate reads as a permanent exclusion; cloud enforcement is implemented in
OpenHands/enterprise#364, so note that it comes out once that ships.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
simonrosenberg and others added 2 commits September 11, 2026 14:56
…t scope

Two halves were covered but not their join. _user_info_from_settings rebuilds
UserInfo from settings.model_dump(); if that ever became a field-by-field copy
the refs would be dropped, the filter would read None, and every scoped profile
would launch unrestricted without failing anything.

Also pins the pass-through for deployments with no agent-profile concept: the
file-backed store discards the resolve flags, so the refs stay None there and
the filter must not read that as an empty allow-list.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
main added an `llm_profiles` parameter to `_resolve_active_agent_profile`
while this branch extended its return tuple with the profile's secret_refs;
the signature carries both.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
simonrosenberg and others added 2 commits October 2, 2026 17:52
…cret-refs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The mypy hook ran in an environment pinned to openhands-sdk 1.39.1,
which predates AgentProfile.secret_refs, while pyproject pins 1.50.1.
Align the hook with the project pin so mypy sees the field this PR
reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  openhands/app_server/app_conversation
  live_status_app_conversation_service.py 2624
  openhands/app_server/settings
  settings_models.py
  storage
  saas_settings_store.py
Project Total  

This report was generated by python-coverage-comment-action

@simonrosenberg

Copy link
Copy Markdown
Member Author

Refreshed onto main without rewriting history. Agent-profile and settings-store suites passed 122 tests against real PostgreSQL; touched-file pre-commit including mypy passed.

Keeping this draft: #759 records a reproduced scope bypass when profile resolution fails. A typed stored profile with secret_refs=[] and a dangling MCP ref causes the real resolver/store to return None; composed settings then launch without this PR's secret scope. The happy-path filter works, but this is not a complete server-side restriction until the failure policy is fixed. That broader task needs its own design/issue, per the audit scope.

@simonrosenberg

Copy link
Copy Markdown
Member Author

CI follow-up: the refreshed branch had 7502 passing tests and 2 failures, both in the unrelated migration-186 OAuth provider tests. The same two tests fail on current main run 38402203103 (7493 passed), and all 5 tests in that migration file pass in isolation locally against real PostgreSQL. This points to an existing suite/process encryption-state interaction, not an agent-profile regression. I am not changing the released migration in this PR. #759 remains the independent reason to keep this secret-scope change draft.

@simonrosenberg

Copy link
Copy Markdown
Member Author

Superseded by #763, which consolidates strict profile secret allow-lists for both OpenHands and ACP launches with the other Enterprise Agent Profile fixes. The replacement uses released SDK 1.54.0, adds strict launch resolution and revision 187 after the current migration head, and includes an upgrade/audit guide.

#763 has passing CI, 7,577 passing local backend tests, real HTTP/model QA, and an independent automated QA PASS. Closing this duplicate PR so review and follow-up happen in one place; the linked issues remain tracked until #763 merges.

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

Labels

type: feat A new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Failed connection to server

1 participant