From aa617cfd88369beefbea3d6389fe44c8a619da14 Mon Sep 17 00:00:00 2001 From: Simon Rosenberg <157206163+simonrosenberg@users.noreply.github.com> Date: Fri, 11 Sep 2026 11:59:17 +0200 Subject: [PATCH 1/3] feat(app-server): enforce an agent profile's secret_refs scope MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- .../live_status_app_conversation_service.py | 40 +++++++ .../app_server/settings/settings_models.py | 6 ++ storage/saas_settings_store.py | 15 ++- tests/unit/test_agent_profiles.py | 100 +++++++++++++++++- 4 files changed, 153 insertions(+), 8 deletions(-) diff --git a/openhands/app_server/app_conversation/live_status_app_conversation_service.py b/openhands/app_server/app_conversation/live_status_app_conversation_service.py index 85e6367828..7d8ba57f2b 100644 --- a/openhands/app_server/app_conversation/live_status_app_conversation_service.py +++ b/openhands/app_server/app_conversation/live_status_app_conversation_service.py @@ -276,6 +276,38 @@ def _to_sdk_marketplace_registrations( ] +def _apply_profile_secret_scope( + secrets: dict[str, Any], + user: UserInfo, +) -> dict[str, Any]: + """Narrow a conversation's secrets to the active agent profile's scope. + + Mirrors the local agent-server, which filters ``request.secrets`` inside its + ``agent_profile_id`` branch — a branch cloud never takes, because it resolves + the profile itself and sends a resolved agent. Without this the field would + persist and display while restricting nothing (OpenHands/enterprise#344). + + Strict, matching ``openhands.sdk.profiles.resolver.filter_profile_secrets``: + 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 harmless no-op; 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 set untouched. + """ + refs = getattr(user, 'active_agent_profile_secret_refs', None) + if refs is None: + return secrets + allowed = set(refs) + dropped = sorted(set(secrets) - allowed) + if dropped: + _logger.info( + 'agent_profile_secret_scope:filtered', + extra={'dropped_secret_names': dropped}, + ) + return {name: value for name, value in secrets.items() if name in allowed} + + @dataclass class LiveStatusAppConversationService(AppConversationServiceBase): """AppConversationService which combines live status info from the sandbox with stored data.""" @@ -2108,6 +2140,10 @@ async def _build_start_conversation_request_for_user( ) secrets[name] = StaticSecret(value=value) + # After the API merge, so a caller sending extra secrets cannot + # widen what the profile allows. + secrets = _apply_profile_secret_scope(secrets, user) + system_message_suffix = self._maybe_append_shallow_clone_context( user, selected_repository, system_message_suffix ) @@ -2478,6 +2514,10 @@ async def _build_acp_start_conversation_request( ) secrets[name] = StaticSecret(value=value) + # After the API merge, so a caller sending extra secrets cannot + # widen what the profile allows. + secrets = _apply_profile_secret_scope(secrets, user) + system_message_suffix = self._maybe_append_shallow_clone_context( user, selected_repository, system_message_suffix ) diff --git a/openhands/app_server/settings/settings_models.py b/openhands/app_server/settings/settings_models.py index 98cd1f9527..c40ae413d1 100644 --- a/openhands/app_server/settings/settings_models.py +++ b/openhands/app_server/settings/settings_models.py @@ -752,6 +752,12 @@ class Settings(BaseModel): # None and ``store()`` refuses instances that carry them (no backing column). active_agent_profile_id: str | None = None active_agent_profile_revision: int | None = None + # The resolved profile's ``secret_refs`` allow-list, carried alongside the + # provenance above for the same reason: resolution replaces + # ``agent_settings`` with an ``AgentSettings`` dump, which has no field for + # it, and conversation-start needs it to scope the conversation's secrets. + # ``None`` = unrestricted, matching the SDK's tri-state. + active_agent_profile_secret_refs: list[str] | None = None # True when this instance came from a resolve-requested load: its # agent_settings are the *effective* launch view (possibly an Agent diff --git a/storage/saas_settings_store.py b/storage/saas_settings_store.py index 3c1448d6a7..4d6560c192 100644 --- a/storage/saas_settings_store.py +++ b/storage/saas_settings_store.py @@ -223,7 +223,7 @@ def _resolve_active_agent_profile( merged_agent_settings: dict[str, Any], effective_llm_api_key: SecretStr | None, override_agent_profile_id: str | None = None, - ) -> tuple[dict[str, Any], str, int] | None: + ) -> tuple[dict[str, Any], str, int, list[str] | None] | None: """Resolve an agent profile into an ``agent_settings`` dump. Resolves ``override_agent_profile_id`` when given (a one-off, @@ -330,7 +330,10 @@ def _resolve_active_agent_profile( exc, ) return None - return resolved_dump, str(profile.id), profile.revision + # ``secret_refs`` rides out alongside the provenance: the resolved dump + # is an ``AgentSettings``, which has no field for it, and conversation + # start needs it to scope the conversation's secrets. + return resolved_dump, str(profile.id), profile.revision, profile.secret_refs async def load( self, @@ -536,10 +539,16 @@ async def load( override_agent_profile_id, ) if resolved is not None: - resolved_dump, resolved_id, resolved_revision = resolved + ( + resolved_dump, + resolved_id, + resolved_revision, + resolved_secret_refs, + ) = resolved kwargs['agent_settings'] = resolved_dump kwargs['active_agent_profile_id'] = resolved_id kwargs['active_agent_profile_revision'] = resolved_revision + kwargs['active_agent_profile_secret_refs'] = resolved_secret_refs settings = Settings(**kwargs) settings._mcp_config_updated = False diff --git a/tests/unit/test_agent_profiles.py b/tests/unit/test_agent_profiles.py index a6e3d77c16..c392e1eaa8 100644 --- a/tests/unit/test_agent_profiles.py +++ b/tests/unit/test_agent_profiles.py @@ -15,10 +15,14 @@ from fastapi.testclient import TestClient from sqlalchemy import select, update +from openhands.app_server.app_conversation.live_status_app_conversation_service import ( + _apply_profile_secret_scope, +) from openhands.app_server.settings.agent_profiles import ( MAX_AGENT_PROFILES, AgentProfiles, ) +from openhands.app_server.user.user_models import UserInfo from openhands.app_server.user_auth import get_user_id from openhands.sdk.profiles import ( ACPAgentProfile, @@ -682,7 +686,7 @@ def test_falls_back_to_org_wide_active_pointer_when_member_pointer_unset(self): result = store._resolve_active_agent_profile(org, member, {}, None) assert result is not None - _dump, resolved_id, _revision = result + _dump, resolved_id, _revision, _refs = result assert resolved_id == pid def test_stale_pointer_falls_back_to_none(self): @@ -705,7 +709,7 @@ def test_resolves_openhands_profile_and_returns_provenance(self): result = store._resolve_active_agent_profile(org, member, {}, None) assert result is not None - dump, resolved_id, revision = result + dump, resolved_id, revision, _refs = result assert resolved_id == pid assert revision == 0 assert dump['agent_kind'] == 'openhands' @@ -739,7 +743,7 @@ def test_resolves_profile_with_legacy_mcp_config(self): ) assert result is not None - dump, _resolved_id, _revision = result + dump, _resolved_id, _revision, _refs = result assert set(dump['mcp_config']) == {'shttp'} assert dump['mcp_config']['shttp']['auth'] == { 'strategy': 'bearer', @@ -780,7 +784,7 @@ def test_resolve_canonicalizes_legacy_litellm_proxy_llm(self): result = store._resolve_active_agent_profile(org, member, {}, None) assert result is not None - dump, _resolved_id, _revision = result + dump, _resolved_id, _revision, _refs = result assert dump['llm']['model'] == 'openhands/claude-opus-4-8' assert dump['llm'].get('base_url') is None @@ -796,7 +800,7 @@ def test_override_id_wins_over_member_pointer(self): org, member, {}, None, override_agent_profile_id=pid ) assert result is not None - _dump, resolved_id, _revision = result + _dump, resolved_id, _revision, _refs = result assert resolved_id == pid def test_override_id_does_not_mutate_member_pointer(self): @@ -1324,3 +1328,89 @@ async def test_admin_can_save_profile_201( ) assert response.status_code == status.HTTP_201_CREATED + + +class TestProfileSecretScope: + """``secret_refs`` must reach the launch, and narrow its secrets there. + + The local agent-server filters ``request.secrets`` inside its + ``agent_profile_id`` branch — a branch cloud never takes, since it resolves + the profile itself and sends a resolved agent. These cover the cloud-side + equivalent (OpenHands/enterprise#344). + """ + + def _user(self, refs): + return UserInfo(active_agent_profile_secret_refs=refs) + + SENT = { + 'DATADOG_API_KEY': 'dd', + 'GITHUB_TOKEN': 'gh', + 'ANTHROPIC_API_KEY': 'ak', + } + + def test_an_unscoped_profile_leaves_the_secrets_untouched(self): + assert ( + _apply_profile_secret_scope(dict(self.SENT), self._user(None)) == self.SENT + ) + + def test_a_scoped_profile_keeps_only_the_named_secrets(self): + got = _apply_profile_secret_scope( + dict(self.SENT), self._user(['DATADOG_API_KEY']) + ) + assert got == {'DATADOG_API_KEY': 'dd'} + + def test_an_empty_scope_keeps_nothing(self): + assert _apply_profile_secret_scope(dict(self.SENT), self._user([])) == {} + + def test_a_git_provider_token_is_not_exempt(self): + # Strict: GITHUB_TOKEN rides the same channel and is dropped when the + # profile does not list it. Intended, and the reason the editor shows + # what a scope excludes. + got = _apply_profile_secret_scope( + dict(self.SENT), self._user(['DATADOG_API_KEY']) + ) + assert 'GITHUB_TOKEN' not in got + + def test_an_acp_provider_credential_is_not_exempt(self): + got = _apply_profile_secret_scope( + dict(self.SENT), self._user(['DATADOG_API_KEY']) + ) + assert 'ANTHROPIC_API_KEY' not in got + + def test_a_ref_matching_no_supplied_secret_is_a_no_op(self): + got = _apply_profile_secret_scope( + dict(self.SENT), self._user(['DATADOG_API_KEY', 'NEVER_SUPPLIED']) + ) + assert got == {'DATADOG_API_KEY': 'dd'} + + def test_resolution_carries_secret_refs_out_of_the_store(self): + """Without this the filter above would silently never fire.""" + with patch('storage.database.a_session_maker'): + from storage.saas_settings_store import SaasSettingsStore + store = SaasSettingsStore(str(USER_ID)) + + org = MagicMock(spec=Org) + org.id = ORG_ID + ap = AgentProfiles() + save_profile_preserving_identity( + ap, + OpenHandsAgentProfile( + name='scoped', + llm_profile_ref='Default', + secret_refs=['DATADOG_API_KEY'], + ), + ) + org.agent_profiles = ap.model_dump( + mode='json', context={'expose_secrets': True} + ) + org.llm_profiles = { + 'profiles': {'Default': {'model': 'gpt-4o', 'api_key': 'orgkey'}}, + 'active': 'Default', + } + member = MagicMock(spec=OrgMember) + member.active_agent_profile_id = next(iter(ap.profiles.values())).id + + result = store._resolve_active_agent_profile(org, member, {}, None) + assert result is not None + _dump, _id, _revision, refs = result + assert refs == ['DATADOG_API_KEY'] From 75bc9960a2d7493b710aa11f676e75e1d0a0eafc Mon Sep 17 00:00:00 2001 From: Simon Rosenberg <157206163+simonrosenberg@users.noreply.github.com> Date: Fri, 11 Sep 2026 14:56:20 +0200 Subject: [PATCH 2/3] test(app-server): pin the seams that would silently disable the secret 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) --- tests/unit/test_agent_profiles.py | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/unit/test_agent_profiles.py b/tests/unit/test_agent_profiles.py index c392e1eaa8..420b39c63a 100644 --- a/tests/unit/test_agent_profiles.py +++ b/tests/unit/test_agent_profiles.py @@ -22,6 +22,7 @@ MAX_AGENT_PROFILES, AgentProfiles, ) +from openhands.app_server.settings.settings_models import Settings from openhands.app_server.user.user_models import UserInfo from openhands.app_server.user_auth import get_user_id from openhands.sdk.profiles import ( @@ -1383,6 +1384,37 @@ def test_a_ref_matching_no_supplied_secret_is_a_no_op(self): ) assert got == {'DATADOG_API_KEY': 'dd'} + def test_secret_refs_survive_the_settings_to_user_info_projection(self): + """The seam that would silently disable the scope if it regressed. + + ``AuthUserContext._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 quietly launch unrestricted. + """ + settings = Settings( + active_agent_profile_id='pid', + active_agent_profile_secret_refs=['DATADOG_API_KEY'], + ) + user = UserInfo( + id='u1', **settings.model_dump(context={'expose_secrets': True}) + ) + assert user.active_agent_profile_secret_refs == ['DATADOG_API_KEY'] + assert _apply_profile_secret_scope( + {'DATADOG_API_KEY': 'dd', 'GITHUB_TOKEN': 'gh'}, user + ) == {'DATADOG_API_KEY': 'dd'} + + def test_a_deployment_without_agent_profiles_is_unrestricted(self): + """The file-backed store resolves no profile, so nothing is scoped. + + ``FileSettingsStore.load`` discards the resolve flags outright, so the + refs stay ``None`` there and the filter must be a pass-through rather + than an empty allow-list. + """ + user = UserInfo(id='u1') + sent = {'GITHUB_TOKEN': 'gh', 'DATADOG_API_KEY': 'dd'} + assert _apply_profile_secret_scope(dict(sent), user) == sent + def test_resolution_carries_secret_refs_out_of_the_store(self): """Without this the filter above would silently never fire.""" with patch('storage.database.a_session_maker'): From 9a9eac56b494092633e715023cccc18a57d510e3 Mon Sep 17 00:00:00 2001 From: Simon Rosenberg <157206163+simonrosenberg@users.noreply.github.com> Date: Fri, 2 Oct 2026 17:52:47 -0400 Subject: [PATCH 3/3] chore(lint): type-check against the pinned SDK release 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 --- dev_config/python/.pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev_config/python/.pre-commit-config.yaml b/dev_config/python/.pre-commit-config.yaml index cf2eda2ed9..717aec6346 100644 --- a/dev_config/python/.pre-commit-config.yaml +++ b/dev_config/python/.pre-commit-config.yaml @@ -56,8 +56,8 @@ repos: types-redis, pydantic, lxml, - "openhands-sdk==1.39.1", - "openhands-tools==1.39.1", + "openhands-sdk==1.50.1", + "openhands-tools==1.50.1", "sqlalchemy==2.0.48", "stripe==11.6.0", "pygithub==2.6.1",