From 19571d492abf3644222495e5803318184fc1143f Mon Sep 17 00:00:00 2001 From: Anna Petrasova Date: Tue, 16 Jun 2026 11:58:30 -0400 Subject: [PATCH] grass.script: Sanitize mapset region variables in runtime setup --- python/grass/script/setup.py | 6 +++++- .../script/tests/grass_script_setup_test.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/python/grass/script/setup.py b/python/grass/script/setup.py index 9f955e18ba5..0102131c423 100644 --- a/python/grass/script/setup.py +++ b/python/grass/script/setup.py @@ -221,10 +221,14 @@ def setup_runtime_env(gisbase=None, *, env=None): RuntimePaths, ) + from grass.script.core import sanitize_mapset_environment + # If environment is not provided, use the global one. if not env: env = os.environ - + # Remove mapset-specific variables that should not leak into the + # runtime environment being set up. + sanitize_mapset_environment(env) runtime_paths = RuntimePaths(env=env, prefix=gisbase) gisbase = runtime_paths.gisbase if not Path(gisbase).is_dir(): diff --git a/python/grass/script/tests/grass_script_setup_test.py b/python/grass/script/tests/grass_script_setup_test.py index 93a58605dac..88b0ca309ed 100644 --- a/python/grass/script/tests/grass_script_setup_test.py +++ b/python/grass/script/tests/grass_script_setup_test.py @@ -467,3 +467,19 @@ def test_grass_path_types_in_setup(tmp_path, path_type): gs.setup.setup_runtime_env(grass_path, env=env) # At least before FHS, GISBASE is the way to detect an active runtime. assert "GISBASE" in env + + +@pytest.mark.parametrize("variable", ["GRASS_REGION", "WIND_OVERRIDE"]) +@pytest.mark.usefixtures("mock_no_session") +def test_init_removes_mapset_variables(tmp_path, variable): + """Check that init removes mapset-specific variables from the environment. + + Variables tied to a specific mapset (such as region overrides) must not + leak from the provided environment into the initialized session. + """ + project = tmp_path / "test" + gs.create_project(project) + env = os.environ.copy() + env[variable] = "value" + with gs.setup.init(project, env=env) as session: + assert variable not in session.env