fix: start pull events when the pull begins and reject unsafe image names

The pull started event was emitted by the route before the pull took
its per compute lock, so a request queued behind another pull claimed
to be running while it was still waiting. The pull now reports the
started state itself, once it owns the lock.

Deleting an image interpolates the name into the daemon API path. The
pattern on the path parameter limits it to the characters of an image
reference and validate_docker_image_name also rejects "..", so a name
cannot escape that path. Applied on both the controller and compute
routes.
This commit is contained in:
YueGuobin committed 2026-11-06 15:42:51 +08:00
1 parent d5d25eba06
commit 04a8ba06a8
7 files changed
+99 -18

No files matched your search

+20 -2
View File
@@ -27,7 +27,12 @@ pytestmark = pytest.mark.asyncio
class TestImagesRoutes:
async def test_pull_docker_image(self, app: FastAPI, compute_client: AsyncClient) -> None:
with asyncio_patch("gns3server.compute.docker.Docker.pull_image") as mock:
async def fake_pull(image, progress_callback=None, force=False):
progress_callback(
{"layers_total": 0, "layers_done": 0, "message": "Pulling 'nginx:latest'", "state": "started"}
)
with asyncio_patch("gns3server.compute.docker.Docker.pull_image", side_effect=fake_pull) as mock:
with asyncio_patch("gns3server.compute.notification_manager.NotificationManager.emit") as emit_mock:
response = await compute_client.post(
app.url_path_for("compute:pull_docker_image"), json={"image": "nginx:latest"}
@@ -44,7 +49,11 @@ class TestImagesRoutes:
async def test_pull_docker_image_progress_id(self, app: FastAPI, compute_client: AsyncClient) -> None:
with asyncio_patch("gns3server.compute.docker.Docker.pull_image"):
async def fake_pull(image, progress_callback=None, force=False):
progress_callback({"layers_total": 1, "layers_done": 0, "message": "Pulling", "state": "started"})
progress_callback({"layers_total": 1, "layers_done": 0, "message": "[==> ] 10/100"})
with asyncio_patch("gns3server.compute.docker.Docker.pull_image", side_effect=fake_pull):
with asyncio_patch("gns3server.compute.notification_manager.NotificationManager.emit") as emit_mock:
response = await compute_client.post(
app.url_path_for("compute:pull_docker_image"),
@@ -57,6 +66,7 @@ class TestImagesRoutes:
async def test_pull_docker_image_reports_downloading(self, app: FastAPI, compute_client: AsyncClient) -> None:
async def fake_pull(image, progress_callback=None, force=False):
progress_callback({"layers_total": 2, "layers_done": 0, "message": "Pulling", "state": "started"})
progress_callback({"layers_total": 2, "layers_done": 1, "message": "[==> ] 40/100"})
with asyncio_patch("gns3server.compute.docker.Docker.pull_image", side_effect=fake_pull):
@@ -118,6 +128,14 @@ class TestImagesRoutes:
mock.assert_called_once_with("ios-xr/xrd-control-plane:24.4.1", force=False)
assert response.status_code == status.HTTP_204_NO_CONTENT
async def test_delete_docker_image_rejects_path_traversal(self, app: FastAPI, compute_client: AsyncClient) -> None:
# percent encoded so the client does not normalize the ".." segments
# away before the request reaches the server
with asyncio_patch("gns3server.compute.docker.Docker.delete_image") as mock:
response = await compute_client.delete("/v3/compute/docker/images/a%2F..%2F..%2Fversion")
mock.assert_not_called()
assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT
async def test_delete_docker_image_force(self, app: FastAPI, compute_client: AsyncClient) -> None:
with asyncio_patch("gns3server.compute.docker.Docker.delete_image") as mock:
@@ -245,6 +245,21 @@ class TestComputeFeatures:
)
assert response.status_code == status.HTTP_204_NO_CONTENT
async def test_compute_delete_docker_image_rejects_path_traversal(
self, app: FastAPI, client: AsyncClient, test_compute: Compute
) -> None:
# percent encoded so the client does not normalize the ".." segments
# away before the request reaches the server
compute = MagicMock()
compute.http_query = AsyncioMagicMock(return_value={})
with patch("gns3server.api.routes.controller.computes.Controller.instance") as controller:
controller.return_value.get_compute.return_value = compute
response = await client.delete(
f"/v3/computes/{test_compute.compute_id}/docker/images/a%2F..%2F..%2Fversion"
)
compute.http_query.assert_not_called()
assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT
async def test_compute_load_docker_image(self, app: FastAPI, client: AsyncClient, test_compute: Compute) -> None:
compute = MagicMock()