From 731c468a3506e2767b67a1eb672bb5bf399a55ea Mon Sep 17 00:00:00 2001 From: Nathan V Date: Fri, 17 Jul 2026 12:51:02 -0700 Subject: [PATCH] Prevent PUT /cameras/{id} rename from overwriting a different camera PUT /cameras/{id} with a body camera_id different from the path treats it as a rename: it deletes the old file and writes the new id. It never checked whether that new id already belonged to a different camera, so a rename onto an existing id silently deleted and overwrote that camera's config. Guard the rename branch: if the destination id already exists, return 409 and change nothing. Adds tests for the collision (both cameras survive) and the happy-path rename to a free id. --- src/clockd/routers/cameras.py | 7 ++++++- tests/test_api_cameras.py | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/clockd/routers/cameras.py b/src/clockd/routers/cameras.py index 440b7b3..b6c0c58 100644 --- a/src/clockd/routers/cameras.py +++ b/src/clockd/routers/cameras.py @@ -45,8 +45,13 @@ async def update_camera(camera_id: str, camera: CameraConfig, request: Request) if camera_id not in cameras: raise HTTPException(status_code=404, detail=f"Camera '{camera_id}' not found") cameras_dir = request.app.state.server_cfg.cameras_dir - # If camera_id in the path differs from the body, rename the file + # If camera_id in the path differs from the body, rename the file — but + # never overwrite a different existing camera in the process. if camera.camera_id != camera_id: + if camera.camera_id in cameras: + raise HTTPException( + status_code=409, detail=f"Camera '{camera.camera_id}' already exists" + ) delete_camera_file(cameras_dir, camera_id) del cameras[camera_id] save_camera(cameras_dir, camera) diff --git a/tests/test_api_cameras.py b/tests/test_api_cameras.py index b0a8e05..f74ee0e 100644 --- a/tests/test_api_cameras.py +++ b/tests/test_api_cameras.py @@ -31,3 +31,28 @@ async def test_create_camera_under_limit(client): resp = await client.post("/cameras", json=_camera_body("under_limit")) assert resp.status_code == 201 assert resp.json()["camera_id"] == "under_limit" + + +@pytest.mark.asyncio +async def test_update_camera_rename_onto_existing_conflicts(client): + # A second camera exists; renaming test_cam onto its id must not clobber it. + app.state.server_cfg.max_cameras = 10 + assert (await client.post("/cameras", json=_camera_body("other_cam"))).status_code == 201 + + resp = await client.put("/cameras/test_cam", json=_camera_body("other_cam")) + assert resp.status_code == 409 + assert "already exists" in resp.json()["detail"] + + # Both cameras survive unchanged — no silent data loss. + assert (await client.get("/cameras/test_cam")).status_code == 200 + assert (await client.get("/cameras/other_cam")).status_code == 200 + + +@pytest.mark.asyncio +async def test_update_camera_rename_to_free_id_succeeds(client): + resp = await client.put("/cameras/test_cam", json=_camera_body("renamed_cam")) + assert resp.status_code == 200 + assert resp.json()["camera_id"] == "renamed_cam" + # Old id is gone, new id resolves. + assert (await client.get("/cameras/test_cam")).status_code == 404 + assert (await client.get("/cameras/renamed_cam")).status_code == 200