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): honor explicit shared_boards grants in board and image read access
joshistoast68d23af05d8e
diff --git a/invokeai/app/api/routers/_access.py b/invokeai/app/api/routers/_access.pyindex 9e442c6dc2..1645542c9f 100644--- a/invokeai/app/api/routers/_access.py+++ b/invokeai/app/api/routers/_access.py@@ -49,6 +49,7 @@ def assert_image_read_access(image_name: str, current_user: CurrentUserOrDefault- The user is an admin.- The user owns the image.- The image sits on a shared or public board.+ - The image sits on a board explicitly shared with the user."""if current_user.is_admin:return@@ -67,6 +68,8 @@ def assert_image_read_access(image_name: str, current_user: CurrentUserOrDefaultreturnexcept Exception:pass+ if ApiDependencies.invoker.services.board_records.is_board_shared_with_user(board_id, current_user.user_id):+ returnraise HTTPException(status_code=403, detail="Not authorized to access this image")@@ -78,6 +81,7 @@ def assert_board_read_access(board_id: str, current_user: CurrentUserOrDefault)- The user is an admin.- The user owns the board.- The board visibility is Shared or Public.+ - The board is explicitly shared with the user."""if current_user.is_admin:return@@ -93,4 +97,7 @@ def assert_board_read_access(board_id: str, current_user: CurrentUserOrDefault)if board.board_visibility in (BoardVisibility.Shared, BoardVisibility.Public):return+ if ApiDependencies.invoker.services.board_records.is_board_shared_with_user(board_id, current_user.user_id):+ return+raise HTTPException(status_code=403, detail="Not authorized to access this board")diff --git a/invokeai/app/services/board_records/board_records_base.py b/invokeai/app/services/board_records/board_records_base.pyindex 20981f2c7d..811084ebf7 100644--- a/invokeai/app/services/board_records/board_records_base.py+++ b/invokeai/app/services/board_records/board_records_base.py@@ -39,6 +39,11 @@ class BoardRecordStorageBase(ABC):"""Updates a board record."""pass+ @abstractmethod+ def is_board_shared_with_user(self, board_id: str, user_id: str) -> bool:+ """Checks whether a board has been explicitly shared with a specific user."""+ pass+@abstractmethoddef get_many(self,diff --git a/invokeai/app/services/board_records/board_records_sqlite.py b/invokeai/app/services/board_records/board_records_sqlite.pyindex 1e3e11c8a3..89f08cf76c 100644--- a/invokeai/app/services/board_records/board_records_sqlite.py+++ b/invokeai/app/services/board_records/board_records_sqlite.py@@ -76,6 +76,19 @@ class SqliteBoardRecordStorage(BoardRecordStorageBase):raise BoardRecordNotFoundExceptionreturn BoardRecord(**dict(result))+ def is_board_shared_with_user(self, board_id: str, user_id: str) -> bool:+ with self._db.transaction() as cursor:+ cursor.execute(+ """--sql+ SELECT 1+ FROM shared_boards+ WHERE board_id = ? AND user_id = ?;+ """,+ (board_id, user_id),+ )++ return cursor.fetchone() is not None+def update(self,board_id: str,diff --git a/tests/app/routers/test_multiuser_authorization.py b/tests/app/routers/test_multiuser_authorization.pyindex 17a49916cc..7440e5fdd1 100644--- a/tests/app/routers/test_multiuser_authorization.py+++ b/tests/app/routers/test_multiuser_authorization.py@@ -214,6 +214,16 @@ def _share_board(client: TestClient, token: str, board_id: str) -> None:assert r.status_code == status.HTTP_201_CREATED+def _share_board_with_user(mock_invoker: Invoker, board_id: str, user_id: str) -> None:+ """Insert an explicit per-user share row directly into the shared_boards table."""+ board_records: Any = mock_invoker.services.board_records+ with board_records._db.transaction() as cursor:+ cursor.execute(+ "INSERT OR IGNORE INTO shared_boards (board_id, user_id) VALUES (?, ?)",+ (board_id, user_id),+ )++def _set_board_visibility(client: TestClient, token: str, board_id: str, visibility: str) -> None:r = client.patch(f"/api/v1/boards/{board_id}", json={"board_visibility": visibility}, headers=_auth(token))assert r.status_code == status.HTTP_201_CREATED@@ -459,6 +469,26 @@ class TestImageReadAuth:# Should not be 403 — image is on a shared boardassert r.status_code != status.HTTP_403_FORBIDDEN+ def test_explicit_share_grants_image_read_to_member(+ self, client: TestClient, mock_invoker: Invoker, user1_token: str, user2_token: str+ ):+ """An image on a private board explicitly shared with user2 should be readable+ by user2 — matching the visibility the board_id="all" listing already grants."""+ user1 = mock_invoker.services.users.get_by_email("[email protected]")+ user2 = mock_invoker.services.users.get_by_email("[email protected]")+ assert user1 is not None and user2 is not None+ _save_image(mock_invoker, "explicit-share-img", user1.user_id)+ board_id = _create_board(client, user1_token, "Explicit Share Board")+ mock_invoker.services.board_image_records.add_image_to_board(board_id=board_id, image_name="explicit-share-img")++ r = client.get("/api/v1/images/i/explicit-share-img", headers=_auth(user2_token))+ assert r.status_code == status.HTTP_403_FORBIDDEN++ _share_board_with_user(mock_invoker, board_id, user2.user_id)++ r = client.get("/api/v1/images/i/explicit-share-img", headers=_auth(user2_token))+ assert r.status_code != status.HTTP_403_FORBIDDEN+def test_non_owner_cannot_read_image_metadata(self, client: TestClient, mock_invoker: Invoker, user1_token: str, user2_token: str):@@ -489,6 +519,22 @@ class TestImageReadAuth:r = client.get(f"/api/v1/images/?board_id={board_id}", headers=_auth(user2_token))assert r.status_code == status.HTTP_200_OK+ def test_list_images_explicitly_shared_private_board_allowed_for_member(+ self, client: TestClient, mock_invoker: Invoker, user1_token: str, user2_token: str+ ):+ """User2 should be able to list a private board explicitly shared with them,+ on both the DTO and names endpoints."""+ user2 = mock_invoker.services.users.get_by_email("[email protected]")+ assert user2 is not None+ board_id = _create_board(client, user1_token, "Explicit Share Enum Board")+ _share_board_with_user(mock_invoker, board_id, user2.user_id)++ r = client.get(f"/api/v1/images/?board_id={board_id}", headers=_auth(user2_token))+ assert r.status_code == status.HTTP_200_OK++ r = client.get(f"/api/v1/images/names?board_id={board_id}", headers=_auth(user2_token))+ assert r.status_code == status.HTTP_200_OK+def test_get_image_names_private_board_rejected_for_non_owner(self, client: TestClient, mock_invoker: Invoker, user1_token: str, user2_token: str):