π OpenHands: AI-Driven Development
fix: send authenticated marketplace URLs to agent-server (#15187)
Hiep Le0828e04b3eb4
diff --git a/openhands/app_server/app_conversation/app_conversation_service_base.py b/openhands/app_server/app_conversation/app_conversation_service_base.pyindex c39171b252..9b6b4de4db 100644--- a/openhands/app_server/app_conversation/app_conversation_service_base.py+++ b/openhands/app_server/app_conversation/app_conversation_service_base.py@@ -23,6 +23,7 @@ from openhands.app_server.app_conversation.app_conversation_service import (AppConversationService,)from openhands.app_server.app_conversation.skill_loader import (+ authenticate_marketplace_sources,build_org_configs,build_sandbox_config,load_skills_from_agent_server,@@ -144,6 +145,13 @@ class AppConversationServiceBase(AppConversationService, ABC):# Build sandbox config (exposed URLs)sandbox_config = build_sandbox_config(sandbox)+ # Swap marketplace sources for authenticated git URLs so the+ # agent-server can clone private marketplace repos. Never raises;+ # unresolvable sources pass through unchanged.+ registered_marketplaces = await authenticate_marketplace_sources(+ registered_marketplaces, self.user_context+ )+# Single API call to agent-server for ALL skillsall_skills = await load_skills_from_agent_server(agent_server_url=agent_server_url,diff --git a/openhands/app_server/app_conversation/skill_loader.py b/openhands/app_server/app_conversation/skill_loader.pyindex 089878d53b..445ae71faf 100644--- a/openhands/app_server/app_conversation/skill_loader.py+++ b/openhands/app_server/app_conversation/skill_loader.py@@ -372,6 +372,116 @@ async def build_org_configs(return configs+def parse_marketplace_source(source: str) -> tuple[str, str]:+ """Parse marketplace source into provider and repo path.++ Args:+ source: Marketplace source (e.g., 'github:owner/repo', 'gitlab:owner/repo',+ 'https://github.com/owner/repo.git')++ Returns:+ Tuple of (provider, repo_path) where provider is 'github', 'gitlab', etc.+ """+ # Handle github:owner/repo format+ if source.startswith('github:'):+ return ('github', source[7:].lstrip('/'))+ if source.startswith('gitlab:'):+ return ('gitlab', source[7:].lstrip('/'))+ if source.startswith('bitbucket:'):+ return ('bitbucket', source[10:].lstrip('/'))+ if source.startswith('azure-devops:'):+ return ('azure-devops', source[13:].lstrip('/'))++ # Handle URL format+ lower = source.lower()+ if 'github.com' in lower:+ path = source.split('github.com', 1)[1].lstrip('/').rstrip('/')+ # Remove .git suffix if present (use removesuffix to avoid character-by-character stripping)+ if path.endswith('.git'):+ path = path[:-4]+ return ('github', path)+ if 'gitlab.com' in lower or 'gitlab' in lower:+ path = (+ source.split(('gitlab.com' if 'gitlab.com' in lower else 'gitlab'), 1)[1]+ .lstrip('/')+ .rstrip('/')+ )+ if path.endswith('.git'):+ path = path[:-4]+ return ('gitlab', path)+ if 'bitbucket.org' in lower:+ path = source.split('bitbucket.org', 1)[1].lstrip('/').rstrip('/')+ if path.endswith('.git'):+ path = path[:-4]+ return ('bitbucket', path)++ # Default to github+ path = source.rstrip('/')+ if path.endswith('.git'):+ path = path[:-4]+ return ('github', path)+++async def authenticate_marketplace_sources(+ registered_marketplaces: list[MarketplaceRegistration] | None,+ user_context: UserContext,+) -> list[MarketplaceRegistration] | None:+ """Swap auto-load marketplace sources for authenticated git URLs.++ The agent-server clones ``auto_load`` marketplace registrations itself but+ has no provider credentials, so private repositories fail to clone (and a+ bare ``owner/repo`` source is misread as a nonexistent local path). Resolve+ each such source to an authenticated URL via the user's provider tokens and+ return copies with ``source`` replaced. Registrations that cannot be+ resolved (local sources, missing tokens, inaccessible repos) pass through+ unchanged and failures never raise, so public/local behavior is unaffected.++ The rewritten URLs embed credentials (like ``OrgConfig.org_repo_url``):+ never log or persist them.++ Args:+ registered_marketplaces: Composed marketplace registrations, or None.+ user_context: UserContext to resolve authenticated URLs.++ Returns:+ New list with rewritten copies (input not mutated), or the input+ itself when None/empty.+ """+ if not registered_marketplaces:+ return registered_marketplaces++ sem = asyncio.Semaphore(_URL_RESOLVE_CONCURRENCY)++ async def _authenticate(reg: MarketplaceRegistration) -> MarketplaceRegistration:+ # Only auto_load registrations are cloned during /api/skills.+ if not reg.auto_load:+ return reg+ _, repo_name = parse_marketplace_source(reg.source)+ # Skip sources that are not owner/repo-shaped: single-segment local+ # paths and URLs on hosts that did not map to a provider repo path.+ if not repo_name or '/' not in repo_name or '://' in repo_name:+ return reg+ try:+ async with sem:+ url = await user_context.get_authenticated_git_url(+ repo_name, is_optional=True+ )+ except Exception as e:+ _logger.debug(+ f'Could not resolve authenticated URL for marketplace '+ f'{reg.name} ({repo_name}); keeping original source: {e}'+ )+ return reg+ # model_copy() intentionally skips re-validation: validate_source+ # rejects credentialed URLs, and the rewritten value is wire-only+ # (never persisted or returned via the settings API).+ return reg.model_copy(update={'source': url})++ return list(+ await asyncio.gather(*[_authenticate(reg) for reg in registered_marketplaces])+ )++def build_sandbox_config(sandbox: SandboxInfo) -> SandboxConfig | None:"""Build sandbox config for agent-server API request.diff --git a/openhands/app_server/user/skills_router.py b/openhands/app_server/user/skills_router.pyindex d757e6ebac..bb632422d9 100644--- a/openhands/app_server/user/skills_router.py+++ b/openhands/app_server/user/skills_router.py@@ -12,6 +12,9 @@ from fastapi import APIRouter, HTTPException, Queryfrom pydantic import BaseModelimport openhands+from openhands.app_server.app_conversation.skill_loader import (+ parse_marketplace_source as _parse_marketplace_source,+)from openhands.app_server.config import depends_user_contextfrom openhands.app_server.integrations.provider import (PROVIDER_TOKEN_TYPE,@@ -192,56 +195,6 @@ async def search_skills(return SkillPage(items=page, next_page_id=next_page_id)-def _parse_marketplace_source(source: str) -> tuple[str, str]:- """Parse marketplace source into provider and repo path.-- Args:- source: Marketplace source (e.g., 'github:owner/repo', 'gitlab:owner/repo',- 'https://github.com/owner/repo.git')-- Returns:- Tuple of (provider, repo_path) where provider is 'github', 'gitlab', etc.- """- # Handle github:owner/repo format- if source.startswith('github:'):- return ('github', source[7:].lstrip('/'))- if source.startswith('gitlab:'):- return ('gitlab', source[7:].lstrip('/'))- if source.startswith('bitbucket:'):- return ('bitbucket', source[10:].lstrip('/'))- if source.startswith('azure-devops:'):- return ('azure-devops', source[13:].lstrip('/'))-- # Handle URL format- lower = source.lower()- if 'github.com' in lower:- path = source.split('github.com', 1)[1].lstrip('/').rstrip('/')- # Remove .git suffix if present (use removesuffix to avoid character-by-character stripping)- if path.endswith('.git'):- path = path[:-4]- return ('github', path)- if 'gitlab.com' in lower or 'gitlab' in lower:- path = (- source.split(('gitlab.com' if 'gitlab.com' in lower else 'gitlab'), 1)[1]- .lstrip('/')- .rstrip('/')- )- if path.endswith('.git'):- path = path[:-4]- return ('gitlab', path)- if 'bitbucket.org' in lower:- path = source.split('bitbucket.org', 1)[1].lstrip('/').rstrip('/')- if path.endswith('.git'):- path = path[:-4]- return ('bitbucket', path)-- # Default to github- path = source.rstrip('/')- if path.endswith('.git'):- path = path[:-4]- return ('github', path)--async def _clone_marketplace_repo(marketplace: MarketplaceRegistration,user_context: UserContext,diff --git a/tests/unit/app_server/test_app_conversation_service_base.py b/tests/unit/app_server/test_app_conversation_service_base.pyindex a1d562802c..1919403634 100644--- a/tests/unit/app_server/test_app_conversation_service_base.py+++ b/tests/unit/app_server/test_app_conversation_service_base.py@@ -1404,6 +1404,65 @@ class TestLoadAndMergeAllSkills:# Assertassert result == []+ @pytest.mark.asyncio+ @patch(+ 'openhands.app_server.app_conversation.app_conversation_service_base.load_skills_from_agent_server'+ )+ @patch(+ 'openhands.app_server.app_conversation.app_conversation_service_base.build_org_configs'+ )+ @patch(+ 'openhands.app_server.app_conversation.app_conversation_service_base.build_sandbox_config'+ )+ async def test_sends_authenticated_marketplace_sources_to_agent_server(+ self,+ mock_build_sandbox_config,+ mock_build_org_configs,+ mock_load_skills,+ ):+ """Auto-load marketplace sources reach the agent-server as token URLs."""+ # Arrange+ from openhands.app_server.settings.settings_models import (+ MarketplaceRegistration,+ )++ authenticated_url = 'https://x-access-token:[email protected]/o/private.git'+ mock_user_context = Mock(spec=UserContext)+ mock_user_context.get_authenticated_git_url = AsyncMock(+ return_value=authenticated_url+ )+ with patch.object(AppConversationServiceBase, '__abstractmethods__', set()):+ service = AppConversationServiceBase(+ init_git_in_empty_workspace=True, user_context=mock_user_context+ )++ sandbox = Mock(spec=SandboxInfo)+ sandbox.exposed_urls = []+ sandbox.session_api_key = 'test-key'++ mock_load_skills.return_value = []+ mock_build_org_configs.return_value = []+ mock_build_sandbox_config.return_value = None++ marketplaces = [+ MarketplaceRegistration(+ name='private', source='github:o/private', auto_load=True+ )+ ]++ # Act+ await service.load_and_merge_all_skills(+ sandbox,+ 'owner/repo',+ '/workspace/repo',+ 'http://localhost:8000',+ registered_marketplaces=marketplaces,+ )++ # Assert+ sent = mock_load_skills.call_args.kwargs['registered_marketplaces']+ assert [reg.source for reg in sent] == [authenticated_url]+class TestLoadSkillsAndUpdateAgent:"""_load_skills_and_update_agent threads marketplaces into skill loading."""diff --git a/tests/unit/app_server/test_skill_loader.py b/tests/unit/app_server/test_skill_loader.pyindex 9566a581a2..8102ba523e 100644--- a/tests/unit/app_server/test_skill_loader.py+++ b/tests/unit/app_server/test_skill_loader.py@@ -21,6 +21,7 @@ from openhands.app_server.app_conversation.skill_loader import (_get_provider_type,_is_azure_devops_repository,_is_gitlab_repository,+ authenticate_marketplace_sources,build_org_configs,build_sandbox_config,load_skills_from_agent_server,@@ -1007,3 +1008,155 @@ class TestLoadSkillsWithMarketplaces:# Empty list is now preserved (semantic distinction from None)# Empty list = "explicitly no marketplaces", None = "not specified"assert payload.get('registered_marketplaces') == []+++# ===== Tests for authenticate_marketplace_sources =====+++AUTHENTICATED_URL = (+ 'https://x-access-token:[email protected]/OpenHands/extensions-private.git'+)+++def _make_registration(**overrides):+ """Build a MarketplaceRegistration with private-repo defaults."""+ from openhands.app_server.settings.settings_models import MarketplaceRegistration++ fields = {+ 'name': 'extensions-private',+ 'source': 'github:OpenHands/extensions-private',+ 'auto_load': True,+ }+ fields.update(overrides)+ return MarketplaceRegistration(**fields)+++class TestAuthenticateMarketplaceSources:+ """Test authenticate_marketplace_sources source rewriting."""++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ 'source',+ [+ 'github:OpenHands/extensions-private',+ 'OpenHands/extensions-private',+ 'https://github.com/OpenHands/extensions-private.git',+ ],+ )+ async def test_rewrites_auto_load_source_to_authenticated_url(self, source):+ """Any stored git source form is resolved and rewritten to a token URL."""+ # Arrange+ mock_user_context = AsyncMock(spec=UserContext)+ mock_user_context.get_authenticated_git_url.return_value = AUTHENTICATED_URL+ registrations = [_make_registration(source=source)]++ # Act+ result = await authenticate_marketplace_sources(+ registrations, mock_user_context+ )++ # Assert+ assert result is not None+ assert result[0].source == AUTHENTICATED_URL+ mock_user_context.get_authenticated_git_url.assert_awaited_once_with(+ 'OpenHands/extensions-private', is_optional=True+ )++ @pytest.mark.asyncio+ async def test_returns_copy_preserving_fields_without_mutating_input(self):+ """The rewrite lands on a copy; other fields and the input stay intact."""+ # Arrange+ mock_user_context = AsyncMock(spec=UserContext)+ mock_user_context.get_authenticated_git_url.return_value = AUTHENTICATED_URL+ registration = _make_registration(ref='main', repo_path='marketplaces/team')++ # Act+ result = await authenticate_marketplace_sources(+ [registration], mock_user_context+ )++ # Assert+ assert result is not None+ rewritten = result[0]+ assert (rewritten.name, rewritten.ref, rewritten.repo_path) == (+ 'extensions-private',+ 'main',+ 'marketplaces/team',+ )+ assert registration.source == 'github:OpenHands/extensions-private'++ @pytest.mark.asyncio+ async def test_keeps_source_when_resolution_fails(self):+ """A failed token resolution degrades to the original source, not an error."""+ # Arrange+ mock_user_context = AsyncMock(spec=UserContext)+ mock_user_context.get_authenticated_git_url.side_effect = Exception(+ 'no provider tokens'+ )+ registrations = [_make_registration()]++ # Act+ result = await authenticate_marketplace_sources(+ registrations, mock_user_context+ )++ # Assert+ assert result is not None+ assert result[0].source == 'github:OpenHands/extensions-private'++ @pytest.mark.asyncio+ async def test_skips_non_auto_load_registrations(self):+ """Registrations that are not auto-loaded are never resolved."""+ # Arrange+ mock_user_context = AsyncMock(spec=UserContext)+ registrations = [_make_registration(auto_load=False)]++ # Act+ result = await authenticate_marketplace_sources(+ registrations, mock_user_context+ )++ # Assert+ assert result is not None+ assert result[0].source == 'github:OpenHands/extensions-private'+ mock_user_context.get_authenticated_git_url.assert_not_awaited()++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ 'source',+ [+ 'localskills', # single segment: relative local path+ 'https://codeberg.org/owner/repo.git', # unrecognized host keeps '://'+ ],+ )+ async def test_skips_sources_without_provider_repo_shape(self, source):+ """Sources that do not map to an owner/repo path pass through untouched."""+ # Arrange+ mock_user_context = AsyncMock(spec=UserContext)+ registrations = [_make_registration(source=source)]++ # Act+ result = await authenticate_marketplace_sources(+ registrations, mock_user_context+ )++ # Assert+ assert result is not None+ assert result[0].source == source+ mock_user_context.get_authenticated_git_url.assert_not_awaited()++ @pytest.mark.asyncio+ @pytest.mark.parametrize('registrations', [None, []])+ async def test_passes_through_falsy_input_untouched(self, registrations):+ """None/empty inputs are returned as-is so wire semantics are preserved."""+ # Arrange+ mock_user_context = AsyncMock(spec=UserContext)++ # Act+ result = await authenticate_marketplace_sources(+ registrations, mock_user_context+ )++ # Assert+ assert result is registrations+ mock_user_context.get_authenticated_git_url.assert_not_awaited()