|
17 | 17 |
|
18 | 18 | from datetime import datetime |
19 | 19 | from datetime import timezone |
| 20 | +import os |
| 21 | +import pickle |
20 | 22 |
|
21 | 23 | from google.adk.events.event_actions import EventActions |
22 | 24 | from google.adk.sessions.migration import _schema_check_utils |
|
25 | 27 | from google.adk.sessions.schemas import v1 |
26 | 28 | import pytest |
27 | 29 | from sqlalchemy import create_engine |
| 30 | +from sqlalchemy import text |
28 | 31 | from sqlalchemy.orm import sessionmaker |
29 | 32 |
|
30 | 33 |
|
@@ -184,6 +187,68 @@ def test_migrate_from_sqlalchemy_pickle(tmp_path): |
184 | 187 | dest_session.close() |
185 | 188 |
|
186 | 189 |
|
| 190 | +def test_migrate_from_sqlalchemy_pickle_blocks_unsafe_actions_pickle( |
| 191 | + tmp_path, monkeypatch |
| 192 | +): |
| 193 | + """Migration should not execute arbitrary globals from a pickled actions blob.""" |
| 194 | + monkeypatch.delenv("ADK_MIGRATION_PICKLE_RCE", raising=False) |
| 195 | + |
| 196 | + source_db_path = tmp_path / "source_pickle_unsafe_actions.db" |
| 197 | + dest_db_path = tmp_path / "dest_json_unsafe_actions.db" |
| 198 | + source_db_url = f"sqlite:///{source_db_path}" |
| 199 | + dest_db_url = f"sqlite:///{dest_db_path}" |
| 200 | + |
| 201 | + source_engine = create_engine(source_db_url) |
| 202 | + v0.Base.metadata.create_all(source_engine) |
| 203 | + SourceSession = sessionmaker(bind=source_engine) |
| 204 | + |
| 205 | + # Populate source DB with a valid session row to satisfy the FK constraint, |
| 206 | + # then insert a malicious pickled actions blob directly as raw bytes. |
| 207 | + now = datetime.now(timezone.utc) |
| 208 | + with SourceSession() as source_session: |
| 209 | + source_session.add( |
| 210 | + v0.StorageSession( |
| 211 | + app_name="app1", |
| 212 | + user_id="user1", |
| 213 | + id="session1", |
| 214 | + state={}, |
| 215 | + create_time=now, |
| 216 | + update_time=now, |
| 217 | + ) |
| 218 | + ) |
| 219 | + source_session.commit() |
| 220 | + |
| 221 | + class Evil: |
| 222 | + def __reduce__(self): |
| 223 | + # This is intentionally non-destructive: it only sets an env var. |
| 224 | + return ( |
| 225 | + exec, |
| 226 | + ("import os; os.environ['ADK_MIGRATION_PICKLE_RCE']='1'",), |
| 227 | + ) |
| 228 | + |
| 229 | + source_session.execute( |
| 230 | + text( |
| 231 | + "INSERT INTO events (id, app_name, user_id, session_id, invocation_id, author, actions, timestamp) " |
| 232 | + "VALUES (:id, :app_name, :user_id, :session_id, :invocation_id, :author, :actions, :timestamp)" |
| 233 | + ), |
| 234 | + { |
| 235 | + "id": "event1", |
| 236 | + "app_name": "app1", |
| 237 | + "user_id": "user1", |
| 238 | + "session_id": "session1", |
| 239 | + "invocation_id": "invoke1", |
| 240 | + "author": "user", |
| 241 | + "actions": pickle.dumps(Evil()), |
| 242 | + "timestamp": now, |
| 243 | + }, |
| 244 | + ) |
| 245 | + source_session.commit() |
| 246 | + |
| 247 | + mfsp.migrate(source_db_url, dest_db_url) |
| 248 | + |
| 249 | + assert os.environ.get("ADK_MIGRATION_PICKLE_RCE") is None |
| 250 | + |
| 251 | + |
187 | 252 | def test_migrate_from_sqlalchemy_pickle_with_async_driver_urls(tmp_path): |
188 | 253 | """Tests that migration works with async driver URLs (fixes issue #4176). |
189 | 254 |
|
|
0 commit comments