diff --git a/AUTHORS.txt b/AUTHORS.txt index ed59dd68..05ab6af5 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -68,6 +68,7 @@ Code Contributors - Nguyễn Hồng Quân - haoran3160-afk (@haoran3160-afk) - Eric3-jp (@Eric3-jp) (with OpenAI Codex assistance) +- Dhanraj E (@Dhanu-dynamic) And a few more "anonymous" contributors. diff --git a/jedi/api/project.py b/jedi/api/project.py index 8927e7ea..66ec35fc 100644 --- a/jedi/api/project.py +++ b/jedi/api/project.py @@ -63,6 +63,9 @@ class Project: Additionally there are functions to search a whole project. """ _environment = None + # Set for projects loaded from a ``.jedi/project.json``. That file can be + # part of a checked out repository, so it is not trusted to run binaries. + _loaded_from_file = False @staticmethod def _get_config_folder_path(base_path): @@ -86,7 +89,13 @@ class Project: version, data = json.load(f) if version == 1: - return cls(**data) + # A code base must not be able to enable the loading of its own + # extensions, see the docs about security. + if data.pop('load_unsafe_extensions', False): + debug.warning('load_unsafe_extensions is ignored for loaded projects') + project = cls(**data) + project._loaded_from_file = True + return project else: raise WrongVersion( "The Jedi version of this project seems newer than what we can handle." @@ -98,6 +107,7 @@ class Project: """ data = dict(self.__dict__) data.pop('_environment', None) + data.pop('_loaded_from_file', None) data.pop('_django', None) # TODO make django setting public? data = {k.lstrip('_'): v for k, v in data.items()} data['path'] = str(data['path']) @@ -242,7 +252,10 @@ class Project: def get_environment(self): if self._environment is None: if self._environment_path is not None: - self._environment = create_environment(self._environment_path, safe=False) + # An environment path from a loaded project file is checked + # like a scanned virtualenv, see find_virtualenvs. + self._environment = create_environment( + self._environment_path, safe=self._loaded_from_file) else: self._environment = get_cached_default_environment() return self._environment diff --git a/test/test_api/test_project.py b/test/test_api/test_project.py index ddf8d2c6..fd90f150 100644 --- a/test/test_api/test_project.py +++ b/test/test_api/test_project.py @@ -1,4 +1,5 @@ import os +import sys from pathlib import Path import pytest @@ -6,6 +7,7 @@ import pytest from ..helpers import get_example_dir, set_cwd, root_dir, test_dir from jedi import Interpreter from jedi.api import Project, get_default_project +from jedi.api.environment import InvalidPythonEnvironment from jedi.api.project import _is_potential_project, _CONTAINS_POTENTIAL_PROJECT @@ -54,6 +56,33 @@ def test_load_save_project(tmpdir): assert loaded.added_sys_path == ['/foo'] +def test_load_project_checks_environment_path(tmpdir, monkeypatch): + # The project file can be part of a checked out repository, so the binary + # it points to has to pass the same check as in find_virtualenvs. + monkeypatch.setattr('jedi.api.environment._is_safe', lambda executable_path: False) + + def _get_subprocess(self): + raise RuntimeError('Should not get called!') + + monkeypatch.setattr('jedi.api.environment.Environment._get_subprocess', + _get_subprocess) + + Project(tmpdir.strpath, environment_path=sys.executable).save() + with pytest.raises(InvalidPythonEnvironment): + Project.load(tmpdir.strpath).get_environment() + + +def test_load_project_safe_environment_path(tmpdir): + Project(tmpdir.strpath, environment_path=sys.executable).save() + environment = Project.load(tmpdir.strpath).get_environment() + assert environment.executable == sys.executable + + +def test_load_project_ignores_unsafe_extensions(tmpdir): + Project(tmpdir.strpath, load_unsafe_extensions=True).save() + assert Project.load(tmpdir.strpath).load_unsafe_extensions is False + + @pytest.mark.parametrize( 'string, full_names, kwargs', [ ('test_load_save_project', ['test_api.test_project.test_load_save_project'], {}),