From 390c88d7cd3e2b598137c3ca23af308ff6aab1e0 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 9 Nov 2015 19:02:10 +0100 Subject: [PATCH] Raise an error if user send a non local path to remote server --- gns3server/modules/base_manager.py | 8 ++++++++ tests/modules/test_manager.py | 25 +++++++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index fc2b74c4..65afda8c 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -39,6 +39,7 @@ from .nios.nio_tap import NIOTAP from .nios.nio_nat import NIONAT from .nios.nio_generic_ethernet import NIOGenericEthernet from ..utils.images import md5sum, remove_checksum +from .vm_error import VMError class BaseManager: @@ -413,6 +414,13 @@ class BaseManager: return force_unix_path(old_path) return force_unix_path(path) + else: + # For non local server we disallow using absolute path outside image directory + if Config.instance().get_section_config("Server").get("local", False) is False: + img_directory = self.config.get_section_config("Server").get("images_path", "~/GNS3/images") + if len(os.path.commonprefix([img_directory, path])) < len(img_directory): + raise VMError("%s is not allowed on this remote server. Please use only the image filename.".format(path)) + return force_unix_path(path) def get_relative_image_path(self, path): diff --git a/tests/modules/test_manager.py b/tests/modules/test_manager.py index 6bc32726..018d3a9f 100644 --- a/tests/modules/test_manager.py +++ b/tests/modules/test_manager.py @@ -23,6 +23,7 @@ from unittest.mock import patch from gns3server.modules.vpcs import VPCS from gns3server.modules.qemu import Qemu +from gns3server.modules.vm_error import VMError @pytest.fixture(scope="function") @@ -101,10 +102,26 @@ def test_get_abs_image_path(qemu, tmpdir): assert qemu.get_abs_image_path("test2.bin") == path2 assert qemu.get_abs_image_path("../test1.bin") == path1 - # We look at first in new location - path2 = str(tmpdir / "QEMU" / "test1.bin") - open(path2, 'w+').close() - assert qemu.get_abs_image_path("test1.bin") == path2 + +def test_get_abs_image_path_non_local(qemu, tmpdir): + path1 = tmpdir / "images" / "QEMU" / "test1.bin" + path1.write("1", ensure=True) + path1 = str(path1) + + path2 = tmpdir / "private" / "QEMU" / "test2.bin" + path2.write("1", ensure=True) + path2 = str(path2) + + # If non local we can't use path outside images directory + with patch("gns3server.config.Config.get_section_config", return_value={"images_path": str(tmpdir / "images"), "local": False}): + assert qemu.get_abs_image_path(path1) == path1 + with pytest.raises(VMError): + qemu.get_abs_image_path(path2) + # with pytest.raises(VMError): + # qemu.get_abs_image_path("C:\\test2.bin") + + with patch("gns3server.config.Config.get_section_config", return_value={"images_path": str(tmpdir / "images"), "local": True}): + assert qemu.get_abs_image_path(path2) == path2 def test_get_relative_image_path(qemu, tmpdir):