Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(api): scope workflow last_run_at updates to the owning user
joshistoast9f8457b98a78
diff --git a/invokeai/app/api/routers/workflows.py b/invokeai/app/api/routers/workflows.pyindex 43743686d8..feefa0a176 100644--- a/invokeai/app/api/routers/workflows.py+++ b/invokeai/app/api/routers/workflows.py@@ -484,6 +484,19 @@ async def update_opened_at(operation_id="update_workflow_last_run_at",status_code=204,)-async def update_workflow_last_run_at(workflow_id: str = Path(description="The workflow to update")) -> None:+async def update_workflow_last_run_at(+ current_user: CurrentUserOrDefault,+ workflow_id: str = Path(description="The workflow to update"),+) -> None:"""Updates the last_run_at field of a workflow"""- ApiDependencies.invoker.services.workflow_records.update_last_run_at(workflow_id)+ try:+ existing = ApiDependencies.invoker.services.workflow_records.get(workflow_id)+ except WorkflowNotFoundError:+ raise HTTPException(status_code=404, detail="Workflow not found")++ config = ApiDependencies.invoker.services.configuration+ if config.multiuser and not current_user.is_admin and existing.user_id != current_user.user_id:+ raise HTTPException(status_code=403, detail="Not authorized to update this workflow")++ user_id = None if current_user.is_admin else current_user.user_id+ ApiDependencies.invoker.services.workflow_records.update_last_run_at(workflow_id, user_id=user_id)diff --git a/invokeai/app/services/workflow_records/workflow_records_base.py b/invokeai/app/services/workflow_records/workflow_records_base.pyindex 85740e4916..eca954a0d5 100644--- a/invokeai/app/services/workflow_records/workflow_records_base.py+++ b/invokeai/app/services/workflow_records/workflow_records_base.py@@ -88,8 +88,8 @@ class WorkflowRecordsStorageBase(ABC):pass@abstractmethod- def update_last_run_at(self, workflow_id: str) -> None:- """Records that a workflow was just run by setting its last_run_at timestamp to now."""+ def update_last_run_at(self, workflow_id: str, user_id: Optional[str] = None) -> None:+ """Records that a workflow was just run. When user_id is provided, the UPDATE is scoped to that user."""pass@abstractmethoddiff --git a/invokeai/app/services/workflow_records/workflow_records_sqlite.py b/invokeai/app/services/workflow_records/workflow_records_sqlite.pyindex 99ebe7961b..b8edf6feaf 100644--- a/invokeai/app/services/workflow_records/workflow_records_sqlite.py+++ b/invokeai/app/services/workflow_records/workflow_records_sqlite.py@@ -443,16 +443,26 @@ class SqliteWorkflowRecordsStorage(WorkflowRecordsStorageBase):(workflow_id,),)- def update_last_run_at(self, workflow_id: str) -> None:+ def update_last_run_at(self, workflow_id: str, user_id: Optional[str] = None) -> None:with self._db.transaction() as cursor:- cursor.execute(- f"""--sql- UPDATE workflow_library- SET last_run_at = STRFTIME('{SQL_TIME_FORMAT}', 'NOW')- WHERE workflow_id = ?;- """,- (workflow_id,),- )+ if user_id is not None:+ cursor.execute(+ f"""--sql+ UPDATE workflow_library+ SET last_run_at = STRFTIME('{SQL_TIME_FORMAT}', 'NOW')+ WHERE workflow_id = ? AND user_id = ?;+ """,+ (workflow_id, user_id),+ )+ else:+ cursor.execute(+ f"""--sql+ UPDATE workflow_library+ SET last_run_at = STRFTIME('{SQL_TIME_FORMAT}', 'NOW')+ WHERE workflow_id = ?;+ """,+ (workflow_id,),+ )def get_all_tags(self,diff --git a/tests/app/routers/test_multiuser_authorization.py b/tests/app/routers/test_multiuser_authorization.pyindex f4fa7cc8b1..dee3d1920c 100644--- a/tests/app/routers/test_multiuser_authorization.py+++ b/tests/app/routers/test_multiuser_authorization.py@@ -1124,6 +1124,33 @@ class TestWorkflowMutationAuth:)assert r.status_code == 200+ def test_update_last_run_at_requires_auth(self, enable_multiuser: Any, client: TestClient):+ r = client.put("/api/v1/workflows/i/some-id/last_run_at")+ assert r.status_code == status.HTTP_401_UNAUTHORIZED++ def test_update_last_run_at_missing_workflow_404s(self, client: TestClient, user1_token: str):+ r = client.put(+ "/api/v1/workflows/i/some-id/last_run_at",+ headers=_auth(user1_token),+ )+ assert r.status_code == status.HTTP_404_NOT_FOUND++ def test_non_owner_cannot_update_last_run_at(self, client: TestClient, user1_token: str, user2_token: str):+ workflow_id = _create_workflow(client, user1_token)+ r = client.put(+ f"/api/v1/workflows/i/{workflow_id}/last_run_at",+ headers=_auth(user2_token),+ )+ assert r.status_code == status.HTTP_403_FORBIDDEN++ def test_owner_can_update_last_run_at(self, client: TestClient, user1_token: str):+ workflow_id = _create_workflow(client, user1_token)+ r = client.put(+ f"/api/v1/workflows/i/{workflow_id}/last_run_at",+ headers=_auth(user1_token),+ )+ assert r.status_code == 204+# ===========================================================================# 4. Workflow thumbnail authorizationdiff --git a/tests/app/services/workflow_records/test_workflow_last_run_at.py b/tests/app/services/workflow_records/test_workflow_last_run_at.pyindex 6a77546d56..1492a45e46 100644--- a/tests/app/services/workflow_records/test_workflow_last_run_at.py+++ b/tests/app/services/workflow_records/test_workflow_last_run_at.py@@ -59,3 +59,26 @@ def test_update_last_run_at_sets_timestamp(workflow_records_service: SqliteWorkf)row = next(item for item in listed.items if item.workflow_id == created.workflow_id)assert row.last_run_at is not None+++def test_update_last_run_at_scoped_to_owning_user(workflow_records_service: SqliteWorkflowRecordsStorage) -> None:+ """Mirrors update_opened_at's ownership scoping: passing a mismatched user_id is a no-op,+ and the row updates only once the correct owner's user_id is supplied."""+ workflow = create_minimal_user_workflow()+ created = workflow_records_service.create(workflow=workflow, user_id="user-a")++ workflow_records_service.update_last_run_at(created.workflow_id, user_id="user-b")+ assert workflow_records_service.get(created.workflow_id).last_run_at is None++ workflow_records_service.update_last_run_at(created.workflow_id, user_id="user-a")+ assert workflow_records_service.get(created.workflow_id).last_run_at is not None+++def test_update_last_run_at_missing_workflow_is_a_no_op(+ workflow_records_service: SqliteWorkflowRecordsStorage,+) -> None:+ """The service method mirrors update_opened_at: it has no existence check and silently does+ nothing for an unknown workflow_id. The router turns a missing workflow into a 404 by calling+ `get()` first (see tests/app/routers/test_multiuser_authorization.py::TestWorkflowMutationAuth+ ::test_update_last_run_at_missing_workflow_404s)."""+ workflow_records_service.update_last_run_at("does-not-exist")