From 653f6a977d8df2b75519ea2ecd2ec796dd13dea9 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Tue, 3 Nov 2026 05:19:19 +0000 Subject: [PATCH] fix: restrict timeline event changes to the author or an admin (#8969) --- mealie/routes/recipe/timeline_events.py | 14 ++- .../test_recipe_timeline_events.py | 87 +++++++++++++++++++ 2 files changed, 98 insertions(+), 3 deletions(-) diff --git a/mealie/routes/recipe/timeline_events.py b/mealie/routes/recipe/timeline_events.py index 1c82c2199..0d6b7c9cd 100644 --- a/mealie/routes/recipe/timeline_events.py +++ b/mealie/routes/recipe/timeline_events.py @@ -8,6 +8,7 @@ from mealie.lang.providers import get_locale_provider from mealie.repos.all_repositories import get_repositories from mealie.routes._base import BaseCrudController, controller from mealie.routes._base.mixins import HttpRepo +from mealie.routes._base.permissions import forbidden from mealie.routes._base.routers import MealieCrudRoute, UserAPIRouter from mealie.schema.recipe.recipe_timeline_events import ( RecipeTimelineEventCreate, @@ -45,6 +46,12 @@ class RecipeTimelineEventsController(BaseCrudController): self.registered_exceptions, ) + def _check_event_belongs_to_user(self, item_id: UUID4) -> RecipeTimelineEventOut: + event = self.mixins.get_one(item_id) + if event.user_id != self.user.id and not self.user.admin: + raise forbidden("Timeline event does not belong to user") + return event + def _translate_event_subject(self, event: RecipeTimelineEventOut) -> None: """Translate auto-generated event subjects into the request's locale. @@ -80,8 +87,7 @@ class RecipeTimelineEventsController(BaseCrudController): @router.post("", response_model=RecipeTimelineEventOut, status_code=201) def create_one(self, data: RecipeTimelineEventIn): - # if the user id is not specified, use the currently-authenticated user - data.user_id = data.user_id or self.user.id + data.user_id = self.user.id recipe = self.group_recipes.get_one(data.recipe_id, "id") if not recipe: @@ -114,6 +120,7 @@ class RecipeTimelineEventsController(BaseCrudController): @router.put("/{item_id}", response_model=RecipeTimelineEventOut) def update_one(self, item_id: UUID4, data: RecipeTimelineEventUpdate): + self._check_event_belongs_to_user(item_id) event = self.mixins.patch_one(data, item_id) if data.image == TimelineEventImage.does_not_have_image.value: RecipeDataService(event.recipe_id).delete_image(event.image_dir) @@ -138,6 +145,7 @@ class RecipeTimelineEventsController(BaseCrudController): @router.delete("/{item_id}", response_model=RecipeTimelineEventOut) def delete_one(self, item_id: UUID4): + self._check_event_belongs_to_user(item_id) event = self.mixins.delete_one(item_id) if event.image_dir.exists(): try: @@ -168,7 +176,7 @@ class RecipeTimelineEventsController(BaseCrudController): @router.put("/{item_id}/image", response_model=UpdateImageResponse) def update_event_image(self, item_id: UUID4, image: bytes = File(...), extension: str = Form(...)): - event = self.mixins.get_one(item_id) + event = self._check_event_belongs_to_user(item_id) data_service = RecipeDataService(event.recipe_id) data_service.write_image(image, extension, event.image_dir) diff --git a/tests/integration_tests/user_recipe_tests/test_recipe_timeline_events.py b/tests/integration_tests/user_recipe_tests/test_recipe_timeline_events.py index aca26257c..334b27dec 100644 --- a/tests/integration_tests/user_recipe_tests/test_recipe_timeline_events.py +++ b/tests/integration_tests/user_recipe_tests/test_recipe_timeline_events.py @@ -478,3 +478,90 @@ def test_invalid_recipe_id( event_response = api_client.post(api_routes.recipes_timeline_events, json=new_event_data, headers=user.token) assert event_response.status_code == 404 + + +def _create_owned_event(api_client: TestClient, owner: TestUser, recipe: Recipe) -> RecipeTimelineEventOut: + response = api_client.post( + api_routes.recipes_timeline_events, + json={"recipe_id": str(recipe.id), "subject": random_string(), "event_type": "info"}, + headers=owner.token, + ) + assert response.status_code == 201 + return RecipeTimelineEventOut.model_validate(response.json()) + + +def test_create_timeline_event_ignores_forged_user_id( + api_client: TestClient, unique_user: TestUser, h2_user: TestUser, recipes: list[Recipe] +): + response = api_client.post( + api_routes.recipes_timeline_events, + json={ + "recipe_id": str(recipes[0].id), + "user_id": str(unique_user.user_id), + "subject": random_string(), + "event_type": "info", + }, + headers=h2_user.token, + ) + assert response.status_code == 201 + assert str(RecipeTimelineEventOut.model_validate(response.json()).user_id) == str(h2_user.user_id) + + +@pytest.mark.parametrize("other_user_fixture", ["h2_user", "unprivileged_user"]) +def test_non_author_cannot_modify_timeline_event( + request: pytest.FixtureRequest, + api_client: TestClient, + unique_user: TestUser, + recipes: list[Recipe], + test_image_jpg: str, + other_user_fixture: str, +): + other_user: TestUser = request.getfixturevalue(other_user_fixture) + event = _create_owned_event(api_client, unique_user, recipes[0]) + item_url = api_routes.recipes_timeline_events_item_id(event.id) + + response = api_client.put(item_url, json={"subject": random_string()}, headers=other_user.token) + assert response.status_code == 403 + + with open(test_image_jpg, "rb") as f: + response = api_client.put( + api_routes.recipes_timeline_events_item_id_image(event.id), + files={"image": ("test_image_jpg.jpg", f, "image/jpeg")}, + data={"extension": "jpg"}, + headers=other_user.token, + ) + assert response.status_code == 403 + assert not any(event.image_dir.rglob("*.*")) + + response = api_client.delete(item_url, headers=other_user.token) + assert response.status_code == 403 + + response = api_client.get(item_url, headers=unique_user.token) + assert response.status_code == 200 + unchanged = RecipeTimelineEventOut.model_validate(response.json()) + assert unchanged.subject == event.subject + assert unchanged.image == TimelineEventImage.does_not_have_image.value + + +def test_other_group_user_cannot_see_timeline_event( + api_client: TestClient, unique_user: TestUser, g2_user: TestUser, recipes: list[Recipe] +): + event = _create_owned_event(api_client, unique_user, recipes[0]) + item_url = api_routes.recipes_timeline_events_item_id(event.id) + + assert api_client.put(item_url, json={"subject": random_string()}, headers=g2_user.token).status_code == 404 + assert api_client.delete(item_url, headers=g2_user.token).status_code == 404 + + +def test_admin_can_modify_and_delete_other_users_timeline_event( + api_client: TestClient, unique_user: TestUser, unique_admin: TestUser, recipes: list[Recipe] +): + event = _create_owned_event(api_client, unique_user, recipes[0]) + item_url = api_routes.recipes_timeline_events_item_id(event.id) + + new_subject = random_string() + response = api_client.put(item_url, json={"subject": new_subject}, headers=unique_admin.token) + assert response.status_code == 200 + assert response.json()["subject"] == new_subject + + assert api_client.delete(item_url, headers=unique_admin.token).status_code == 200