diff --git a/gns3server/api/routes/controller/symbols.py b/gns3server/api/routes/controller/symbols.py index afa004398..3ff82b045 100644 --- a/gns3server/api/routes/controller/symbols.py +++ b/gns3server/api/routes/controller/symbols.py @@ -21,6 +21,7 @@ API routes for symbols. import logging import os +from os.path import commonpath from typing import Dict, List from fastapi import APIRouter, Depends, Request, Response, status @@ -170,7 +171,7 @@ async def delete_symbol(symbol_id: str) -> None: # Check if it's a built-in symbol (in resource directory) symbols_resource_dir = get_resource("symbols") - if symbols_resource_dir and os.path.commonprefix([symbols_resource_dir, symbol_path]) == symbols_resource_dir: + if symbols_resource_dir and commonpath([symbols_resource_dir, symbol_path]) == symbols_resource_dir: raise ControllerForbiddenError("Cannot delete built-in symbols") # Delete the file diff --git a/gns3server/compute/base_manager.py b/gns3server/compute/base_manager.py index 758e59679..e915b0d51 100644 --- a/gns3server/compute/base_manager.py +++ b/gns3server/compute/base_manager.py @@ -28,6 +28,7 @@ import aiofiles log = logging.getLogger(__name__) +from os.path import commonpath from typing import Type from uuid import UUID, uuid4 @@ -480,7 +481,7 @@ class BaseManager: path = force_unix_path(path) for directory in valid_directory_prefices: log.debug(f"Searching for image '{orig_path}' in '{directory}'") - if os.path.commonprefix([directory, path]) == directory: + if commonpath([directory, path]) == directory: if os.path.exists(path): return path raise ImageMissingError(orig_path) @@ -525,7 +526,7 @@ class BaseManager: valid_directory_prefices.append(extra_dir) for directory in valid_directory_prefices: - if os.path.commonprefix([directory, path]) == directory: + if commonpath([directory, path]) == directory: relpath = os.path.relpath(path, directory) # We don't allow to recurse search from the top image directory just for image type directory (compatibility with old releases) if os.sep not in relpath or directory == img_directory: @@ -557,7 +558,7 @@ class BaseManager: directory = self.get_images_directory() path = os.path.abspath(os.path.join(directory, *os.path.split(filename))) - if os.path.commonprefix([directory, path]) != directory: + if commonpath([directory, path]) != directory: raise ComputeForbiddenError(f"Could not write image: {filename}, '{path}' is forbidden") log.info(f"Writing image file to '{path}'") try: diff --git a/gns3server/controller/node.py b/gns3server/controller/node.py index 419950839..471580f47 100644 --- a/gns3server/controller/node.py +++ b/gns3server/controller/node.py @@ -22,6 +22,7 @@ import logging import os import re import uuid +from os.path import commonpath from ..config import Config from ..utils import int_to_macaddress, macaddress_to_int @@ -420,7 +421,7 @@ class Node: try: if not val.startswith(":") and os.path.isabs(val): default_symbol_directory = Config.instance().settings.Server.symbols_path - if os.path.commonprefix([default_symbol_directory, val]) != default_symbol_directory: + if commonpath([default_symbol_directory, val]) != default_symbol_directory: val = os.path.basename(val) except OSError: pass diff --git a/gns3server/utils/images.py b/gns3server/utils/images.py index 829fe78d2..f61b80b61 100644 --- a/gns3server/utils/images.py +++ b/gns3server/utils/images.py @@ -38,6 +38,7 @@ except ImportError: import logging from io import DEFAULT_BUFFER_SIZE +from os.path import commonpath from typing import AsyncGenerator, List import gns3server.db.models as models @@ -73,7 +74,7 @@ async def list_images(image_type): # the reason is in the default directory manage file organization and # it should be flatten to keep things simple recurse = True - if os.path.commonprefix([directory, general_images_directory]) == general_images_directory: + if commonpath([directory, general_images_directory]) == general_images_directory: recurse = False directory = os.path.normpath(directory) @@ -88,7 +89,7 @@ async def list_images(image_type): files.add(filename) # It the image is located in the standard directory the path is relative - if os.path.commonprefix([root, default_directory]) != default_directory: + if commonpath([root, default_directory]) != default_directory: path = os.path.join(root, filename) else: path = os.path.relpath(os.path.join(root, filename), default_directory) diff --git a/gns3server/utils/path.py b/gns3server/utils/path.py index 35d7cc142..ebb06bfb8 100644 --- a/gns3server/utils/path.py +++ b/gns3server/utils/path.py @@ -16,6 +16,7 @@ # along with this program. If not, see . import os +from os.path import commonpath from pathlib import Path from fastapi import HTTPException, status @@ -58,7 +59,7 @@ def check_path_allowed(path: str): """ project_directory = get_default_project_directory() - if len(os.path.commonprefix([project_directory, path])) == len(project_directory): + if len(commonpath([project_directory, path])) == len(project_directory): return raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=f"The path {path} is not allowed")