Skip to content

fix: issue 1641 database session service leaks events #1644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/google/adk/sessions/database_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ async def get_session(
storage_events = (
session_factory.query(StorageEvent)
.filter(StorageEvent.session_id == storage_session.id)
.filter(StorageEvent.user_id == user_id)
.filter(timestamp_filter)
.order_by(StorageEvent.timestamp.desc())
.limit(
Expand Down
12 changes: 12 additions & 0 deletions tests/unittests/sessions/test_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ async def test_session_state(service_type):
app_name = 'my_app'
user_id_1 = 'user1'
user_id_2 = 'user2'
user_id_malicious = 'malicious'
session_id_11 = 'session11'
session_id_12 = 'session12'
session_id_2 = 'session2'
Expand All @@ -148,6 +149,10 @@ async def test_session_state(service_type):
app_name=app_name, user_id=user_id_2, session_id=session_id_2
)

await session_service.create_session(
app_name=app_name, user_id=user_id_malicious, session_id=session_id_11
)

assert session_11.state.get('key11') == 'value11'

event = Event(
Expand Down Expand Up @@ -196,6 +201,13 @@ async def test_session_state(service_type):
assert session_11.state.get('user:key1') == 'value1'
assert not session_11.state.get('temp:key')

# Make sure a malicious user can obtain a session and events not belonging to them
session_mismatch = await session_service.get_session(
app_name=app_name, user_id=user_id_malicious, session_id=session_id_11
)

assert len(session_mismatch.events) == 0


@pytest.mark.asyncio
@pytest.mark.parametrize(
Expand Down