Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/google/adk/sessions/vertex_ai_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@
_SESSION_ID_PATTERN = re.compile(r'^[A-Za-z0-9_-]+$')


def _extract_short_session_id(
session_id: str, expected_engine_id: str | None = None
) -> str:
"""Extracts the short session ID if a full resource name is provided."""
if isinstance(session_id, str) and '/' in session_id:
parts = session_id.split('/')
if len(parts) >= 2 and parts[-2] == 'sessions':
if (
len(parts) >= 4
and parts[-4] == 'reasoningEngines'
and expected_engine_id
):
passed_engine_id = parts[-3]
if passed_engine_id != expected_engine_id:
raise ValueError(
'Session resource name mismatch: session belongs to '
f'reasoningEngine {passed_engine_id!r}, but service is '
f'configured for {expected_engine_id!r}.'
)
return parts[-1]
return session_id


def _validate_session_id(session_id: str) -> None:
"""Rejects session IDs that could escape the URL path segment."""
if not isinstance(session_id, str) or not _SESSION_ID_PATTERN.fullmatch(
Expand Down Expand Up @@ -140,6 +163,9 @@ async def create_session(

config = {'session_state': state} if state else {}
if session_id:
session_id = _extract_short_session_id(
session_id, expected_engine_id=reasoning_engine_id
)
_validate_session_id(session_id)
config['session_id'] = session_id
config.update(kwargs)
Expand Down Expand Up @@ -171,8 +197,11 @@ async def get_session(
session_id: str,
config: Optional[GetSessionConfig] = None,
) -> Optional[Session]:
_validate_session_id(session_id)
reasoning_engine_id = self._get_reasoning_engine_id(app_name)
session_id = _extract_short_session_id(
session_id, expected_engine_id=reasoning_engine_id
)
_validate_session_id(session_id)
session_resource_name = (
f'reasoningEngines/{reasoning_engine_id}/sessions/{session_id}'
)
Expand Down Expand Up @@ -271,8 +300,11 @@ async def list_sessions(
async def delete_session(
self, *, app_name: str, user_id: str, session_id: str
) -> None:
_validate_session_id(session_id)
reasoning_engine_id = self._get_reasoning_engine_id(app_name)
session_id = _extract_short_session_id(
session_id, expected_engine_id=reasoning_engine_id
)
_validate_session_id(session_id)
session_resource_name = (
f'reasoningEngines/{reasoning_engine_id}/sessions/{session_id}'
)
Expand Down
74 changes: 74 additions & 0 deletions tests/unittests/sessions/test_vertex_ai_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
from google.adk.models.cache_metadata import CacheMetadata
from google.adk.sessions.base_session_service import GetSessionConfig
from google.adk.sessions.session import Session
from google.adk.sessions.vertex_ai_session_service import _extract_short_session_id
from google.adk.sessions.vertex_ai_session_service import _validate_session_id
from google.adk.sessions.vertex_ai_session_service import VertexAiSessionService
from google.api_core import exceptions as api_core_exceptions
from google.genai import types as genai_types
Expand Down Expand Up @@ -764,6 +766,78 @@ async def test_session_id_path_traversal_rejected():
)


def test_extract_short_session_id_short_id():
assert _extract_short_session_id('123') == '123'
assert _extract_short_session_id('session-123_abc') == 'session-123_abc'


def test_extract_short_session_id_strips_full_resource_name():
resource_name = (
'projects/test-project/locations/test-location/'
'reasoningEngines/123/sessions/3'
)
assert _extract_short_session_id(resource_name) == '3'
assert (
_extract_short_session_id(resource_name, expected_engine_id='123') == '3'
)


def test_extract_short_session_id_mismatch():
resource_name = (
'projects/test-project/locations/test-location/'
'reasoningEngines/wrong/sessions/3'
)
with pytest.raises(ValueError, match='Session resource name mismatch'):
_extract_short_session_id(resource_name, expected_engine_id='123')


def test_validate_session_id_rejects_invalid_chars():
with pytest.raises(ValueError, match='Invalid session_id'):
_validate_session_id('invalid@id')
with pytest.raises(ValueError, match='Invalid session_id'):
_validate_session_id('invalid/id')


@pytest.mark.asyncio
@pytest.mark.usefixtures('mock_get_api_client')
async def test_get_session_accepts_a_full_resource_name(
mock_api_client_instance,
):
"""Agent Engine passes the full resource name, not the short id."""
session_service = mock_vertex_ai_session_service()
resource_name = (
'projects/test-project/locations/test-location/'
'reasoningEngines/123/sessions/1'
)

session = await session_service.get_session(
app_name='123', user_id='user', session_id=resource_name
)

assert session.id == '1'
Comment thread
GWeale marked this conversation as resolved.
# The resolved session id alone cannot tell a correctly built resource name
# from one that embedded the full name and happens to end in the same
# segment, so assert the name that actually went out.
mock_api_client_instance.agent_engines.sessions.get.assert_called_once_with(
name='reasoningEngines/123/sessions/1'
)


@pytest.mark.asyncio
@pytest.mark.usefixtures('mock_get_api_client')
async def test_get_session_rejects_a_resource_name_for_another_engine():
session_service = mock_vertex_ai_session_service()
resource_name = (
'projects/test-project/locations/test-location/'
'reasoningEngines/456/sessions/1'
)

with pytest.raises(ValueError, match='Session resource name mismatch'):
await session_service.get_session(
app_name='123', user_id='user', session_id=resource_name
)


@pytest.mark.asyncio
@pytest.mark.usefixtures('mock_get_api_client')
async def test_get_session_with_page_token():
Expand Down
Loading