Skip to content

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.

fix(api): delegate remaining board and recall read gates to shared access asserts

joshistoast340a3399f600
diff --git a/invokeai/app/api/routers/boards.py b/invokeai/app/api/routers/boards.py
index 067936834e..ca5e400c1b 100644
--- a/invokeai/app/api/routers/boards.py
+++ b/invokeai/app/api/routers/boards.py
@@ -6,8 +6,9 @@ from pydantic import BaseModel, Field
from invokeai.app.api.auth_dependencies import CurrentUserOrDefault
from invokeai.app.api.dependencies import ApiDependencies
+from invokeai.app.api.routers._access import assert_board_read_access as _assert_board_read_access
from invokeai.app.api.routers.image_move_maintenance import assert_image_move_maintenance_inactive
-from invokeai.app.services.board_records.board_records_common import BoardChanges, BoardRecordOrderBy, BoardVisibility
+from invokeai.app.services.board_records.board_records_common import BoardChanges, BoardRecordOrderBy
from invokeai.app.services.boards.boards_common import BoardDTO
from invokeai.app.services.image_records.image_records_common import ImageCategory
from invokeai.app.services.shared.pagination import OffsetPaginatedResults
@@ -52,23 +53,13 @@ async def get_board(
) -> BoardDTO:
"""Gets a board (user must have access to it)"""
+ _assert_board_read_access(board_id, current_user)
+
try:
- result = ApiDependencies.invoker.services.boards.get_dto(board_id=board_id)
+ return ApiDependencies.invoker.services.boards.get_dto(board_id=board_id)
except Exception:
raise HTTPException(status_code=404, detail="Board not found")
- # Admins can access any board.
- # Owners can access their own boards.
- # Shared and public boards are visible to all authenticated users.
- if (
- not current_user.is_admin
- and result.user_id != current_user.user_id
- and result.board_visibility == BoardVisibility.Private
- ):
- raise HTTPException(status_code=403, detail="Not authorized to access this board")
-
- return result
-
@boards_router.patch(
"/{board_id}",
@@ -194,17 +185,7 @@ async def list_all_board_image_names(
"""Gets a list of images for a board"""
if board_id != "none":
- try:
- board = ApiDependencies.invoker.services.boards.get_dto(board_id=board_id)
- except Exception:
- raise HTTPException(status_code=404, detail="Board not found")
-
- if (
- not current_user.is_admin
- and board.user_id != current_user.user_id
- and board.board_visibility == BoardVisibility.Private
- ):
- raise HTTPException(status_code=403, detail="Not authorized to access this board")
+ _assert_board_read_access(board_id, current_user)
image_names = ApiDependencies.invoker.services.board_images.get_all_board_image_names_for_board(
board_id,
diff --git a/invokeai/app/api/routers/recall_parameters.py b/invokeai/app/api/routers/recall_parameters.py
index 1f96280f4f..dce63b24bd 100644
--- a/invokeai/app/api/routers/recall_parameters.py
+++ b/invokeai/app/api/routers/recall_parameters.py
@@ -9,6 +9,7 @@ from pydantic import BaseModel, ConfigDict, Field
from invokeai.app.api.auth_dependencies import CurrentUserOrDefault
from invokeai.app.api.dependencies import ApiDependencies
+from invokeai.app.api.routers._access import assert_image_read_access as _assert_image_read_access
from invokeai.app.api.routers.image_move_maintenance import assert_image_move_maintenance_inactive
from invokeai.backend.image_util.controlnet_processor import process_controlnet_image
from invokeai.backend.model_manager.taxonomy import ModelType
@@ -353,8 +354,6 @@ def _assert_recall_image_access(parameters: "RecallParameter", current_user: Cur
endpoint to extract image dimensions and — for ControlNet preprocessors — mint
a derived processed image they can then fetch.
"""
- from invokeai.app.services.board_records.board_records_common import BoardVisibility
-
image_names: list[str] = []
if parameters.control_layers:
for layer in parameters.control_layers:
@@ -369,29 +368,8 @@ def _assert_recall_image_access(parameters: "RecallParameter", current_user: Cur
if ref.image_name is not None:
image_names.append(ref.image_name)
- if not image_names:
- return
-
- # Admin can access all images
- if current_user.is_admin:
- return
-
for image_name in image_names:
- owner = ApiDependencies.invoker.services.image_records.get_user_id(image_name)
- if owner is not None and owner == current_user.user_id:
- continue
-
- # Check board visibility
- board_id = ApiDependencies.invoker.services.board_image_records.get_board_for_image(image_name)
- if board_id is not None:
- try:
- board = ApiDependencies.invoker.services.boards.get_dto(board_id=board_id)
- if board.board_visibility in (BoardVisibility.Shared, BoardVisibility.Public):
- continue
- except Exception:
- pass
-
- raise HTTPException(status_code=403, detail=f"Not authorized to access image {image_name}")
+ _assert_image_read_access(image_name, current_user)
@recall_parameters_router.post(
diff --git a/tests/app/routers/test_boards_multiuser.py b/tests/app/routers/test_boards_multiuser.py
index ab64ac8a9b..d72e252385 100644
--- a/tests/app/routers/test_boards_multiuser.py
+++ b/tests/app/routers/test_boards_multiuser.py
@@ -79,6 +79,7 @@ def enable_multiuser_for_tests(monkeypatch: Any, mock_invoker: Invoker):
monkeypatch.setattr("invokeai.app.api.routers.auth.ApiDependencies", mock_deps)
monkeypatch.setattr("invokeai.app.api.auth_dependencies.ApiDependencies", mock_deps)
monkeypatch.setattr("invokeai.app.api.routers.boards.ApiDependencies", mock_deps)
+ monkeypatch.setattr("invokeai.app.api.routers._access.ApiDependencies", mock_deps)
yield
diff --git a/tests/app/routers/test_multiuser_authorization.py b/tests/app/routers/test_multiuser_authorization.py
index 7440e5fdd1..2719351673 100644
--- a/tests/app/routers/test_multiuser_authorization.py
+++ b/tests/app/routers/test_multiuser_authorization.py
@@ -535,6 +535,27 @@ class TestImageReadAuth:
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_explicitly_shared_private_board_dto_and_image_names_allowed_for_member(
+ self, client: TestClient, mock_invoker: Invoker, user1_token: str, user2_token: str
+ ):
+ """User2 should be able to fetch the board DTO and board image_names of a private
+ board explicitly shared with them; a non-member (user1's other boards aside) stays 403."""
+ user2 = mock_invoker.services.users.get_by_email("[email protected]")
+ assert user2 is not None
+ board_id = _create_board(client, user1_token, "Explicit Share DTO Board")
+
+ r = client.get(f"/api/v1/boards/{board_id}", headers=_auth(user2_token))
+ assert r.status_code == status.HTTP_403_FORBIDDEN
+ r = client.get(f"/api/v1/boards/{board_id}/image_names", 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(f"/api/v1/boards/{board_id}", headers=_auth(user2_token))
+ assert r.status_code == status.HTTP_200_OK
+ r = client.get(f"/api/v1/boards/{board_id}/image_names", 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
):
@@ -1257,6 +1278,35 @@ class TestRecallImageAccess:
)
assert r.status_code != status.HTTP_403_FORBIDDEN
+ def test_recall_explicitly_shared_board_image_allowed_for_member(
+ self, client: TestClient, mock_invoker: Invoker, user1_token: str, user2_token: str
+ ):
+ """User2 should be able to reference an image on a private board explicitly shared with them."""
+ 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-recall-img", user1.user_id)
+ board_id = _create_board(client, user1_token, "Explicit Share Recall Board")
+ mock_invoker.services.board_image_records.add_image_to_board(
+ board_id=board_id, image_name="explicit-share-recall-img"
+ )
+
+ r = client.post(
+ "/api/v1/recall/default",
+ json={"ip_adapters": [{"model_name": "some-ip-adapter", "image_name": "explicit-share-recall-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.post(
+ "/api/v1/recall/default",
+ json={"ip_adapters": [{"model_name": "some-ip-adapter", "image_name": "explicit-share-recall-img"}]},
+ headers=_auth(user2_token),
+ )
+ assert r.status_code != status.HTTP_403_FORBIDDEN
+
def test_recall_admin_can_reference_any_image(
self, client: TestClient, mock_invoker: Invoker, admin_token: str, user1_token: str
):