Skip to content

Commit a30f995

Browse files
authored
🐛 fix(create): keep pyvenv.cfg values on a single line (#3247)
1 parent 469dd28 commit a30f995

8 files changed

Lines changed: 192 additions & 3 deletions

File tree

‎.github/workflows/check.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ jobs:
194194
tox_env:
195195
- dev
196196
- docs
197+
- property
197198
- readme
198199
- type
199200
- type-3.9

‎docs/changelog/3247.bugfix.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Write ``pyvenv.cfg`` values on a single line, so a prompt carrying a line boundary can no longer inject configuration.
2+
``--prompt``, the ``VIRTUALENV_PROMPT`` environment variable and the config file all set the prompt, and ``pyvenv.cfg``
3+
has no escape syntax, so a newline, a carriage return, or any other boundary ``str.splitlines`` recognizes, such as
4+
``U+2028``, started a new configuration line. Reading the file back picked up those lines as keys, and since the last
5+
value for a key wins, they replaced anything written earlier, including ``home``.

‎pyproject.toml‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ docs = [
138138
"sphinxcontrib-towncrier>=0.2.1a0",
139139
"towncrier>=23.6",
140140
]
141+
fuzz = [
142+
# atheris only ships prebuilt Linux wheels for these CPython versions; everywhere else it needs a
143+
# custom clang+libFuzzer build, so skip it there rather than triggering a doomed source build.
144+
"atheris>=2.3; sys_platform=='linux' and python_version>='3.12' and python_version<'3.15'",
145+
]
141146
lint = [
142147
"pre-commit-uv>=4.2",
143148
]
@@ -146,6 +151,10 @@ pkg-meta = [
146151
"twine>=6.2",
147152
"uv>=0.10.2",
148153
]
154+
property = [
155+
"hypothesis>=6.140",
156+
{ include-group = "test" },
157+
]
149158

150159
[tool.hatch]
151160
version.source = "vcs"
@@ -229,9 +238,10 @@ max_supported_python = "3.15"
229238
rules.unused-ignore-comment = "ignore" # some ignores are platform/version-specific (e.g., ctypes.windll on Linux)
230239

231240
[tool.pytest]
232-
ini_options.addopts = "--showlocals"
241+
ini_options.addopts = "--showlocals -m 'not property'"
233242
ini_options.markers = [
234243
"graalpy: minimal test suite for GraalPy validation",
244+
"property: hypothesis property tests, run by their own tox environment",
235245
"slow",
236246
]
237247
ini_options.env = [

‎src/virtualenv/create/pyenv_cfg.py‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,29 @@
33
import logging
44
import os
55
from collections import OrderedDict
6-
from typing import TYPE_CHECKING
6+
from typing import TYPE_CHECKING, Final
77

88
if TYPE_CHECKING:
99
from pathlib import Path
1010

1111
LOGGER = logging.getLogger(__name__)
1212

13+
# pyvenv.cfg is a line-based format with no escape syntax, so a value carrying any character that
14+
# str.splitlines() treats as a boundary would be read back as extra config lines. These are exactly
15+
# the boundaries splitlines() recognizes, per the Python string documentation.
16+
_LINE_BOUNDARIES: Final[tuple[str, ...]] = (
17+
"\n",
18+
"\r",
19+
"\v",
20+
"\f",
21+
"\x1c",
22+
"\x1d",
23+
"\x1e",
24+
"\x85",
25+
chr(0x2028), # line separator
26+
chr(0x2029), # paragraph separator
27+
)
28+
1329

1430
class PyEnvCfg:
1531
def __init__(self, content: OrderedDict[str, str], path: Path) -> None:
@@ -48,7 +64,7 @@ def write(self) -> None:
4864
normalized_value = f'"{value}"'
4965
else:
5066
normalized_value = os.path.abspath(value) if value and os.path.exists(value) else value
51-
line = f"{key} = {normalized_value}"
67+
line = f"{_one_line(key)} = {_one_line(normalized_value)}"
5268
LOGGER.debug("\t%s", line)
5369
text += line
5470
text += "\n"
@@ -75,6 +91,13 @@ def __repr__(self) -> str:
7591
return f"{self.__class__.__name__}(path={self.path})"
7692

7793

94+
def _one_line(text: str) -> str:
95+
"""Collapse line boundaries so a value can never be read back as extra config lines."""
96+
for boundary in _LINE_BOUNDARIES:
97+
text = text.replace(boundary, " ")
98+
return text
99+
100+
78101
__all__ = [
79102
"PyEnvCfg",
80103
]

‎tasks/fuzz_pyenv_cfg.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Fuzz the pyvenv.cfg writer and reader for configuration injection.
2+
3+
This asserts the same invariant as tests/property/test_pyenv_cfg.py, with a coverage-guided driver rather than
4+
hypothesis: a value must never be read back as an extra configuration key. That is the property that #3247 violated,
5+
where a prompt containing a line boundary could override ``home``.
6+
7+
Run directly: python tasks/fuzz_pyenv_cfg.py -runs=100000
8+
9+
"""
10+
11+
from __future__ import annotations
12+
13+
import sys
14+
import tempfile
15+
from collections import OrderedDict
16+
from pathlib import Path
17+
18+
import atheris
19+
20+
from virtualenv.create.pyenv_cfg import PyEnvCfg
21+
22+
23+
def test_one_input(data: bytes) -> None:
24+
provider = atheris.FuzzedDataProvider(data)
25+
keys = ("home", "prompt", "version", "base-prefix")
26+
content = OrderedDict(
27+
(keys[i], provider.ConsumeUnicodeNoSurrogates(provider.remaining_bytes() // (len(keys) - i or 1)))
28+
for i in range(len(keys))
29+
)
30+
with tempfile.TemporaryDirectory() as folder:
31+
path = Path(folder) / "pyvenv.cfg"
32+
PyEnvCfg(content, path).write()
33+
read_back = PyEnvCfg.from_file(path).content
34+
extra = set(read_back) - set(content)
35+
assert not extra, (extra, dict(content)) # ruff:ignore[assert] atheris treats AssertionError as the crash signal
36+
37+
38+
if __name__ == "__main__":
39+
atheris.Setup(sys.argv, test_one_input)
40+
atheris.Fuzz()

‎tests/property/conftest.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Keep the property tests out of collection when hypothesis is absent.
2+
3+
The marker deselection in addopts runs after import, so without this the default test environments, which do not install
4+
hypothesis, fail at collection rather than skipping.
5+
6+
"""
7+
8+
from __future__ import annotations
9+
10+
from importlib.util import find_spec
11+
12+
collect_ignore_glob = [] if find_spec("hypothesis") else ["*.py"]

‎tests/property/test_pyenv_cfg.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""Property tests for the pyvenv.cfg reader and writer.
2+
3+
pyvenv.cfg is a line-based format with no escape syntax, so the invariant that matters is that a value can never be read
4+
back as extra configuration. These properties are what caught #3247: a prompt containing a line boundary used to inject
5+
arbitrary keys, overriding any key written before it, including ``home``.
6+
7+
"""
8+
9+
from __future__ import annotations
10+
11+
import os
12+
from collections import OrderedDict
13+
14+
import pytest
15+
from hypothesis import example, given
16+
from hypothesis import strategies as st
17+
18+
from virtualenv.create.pyenv_cfg import PyEnvCfg
19+
20+
pytestmark = pytest.mark.property
21+
22+
# Anything str.splitlines() treats as a boundary is what makes a value escape its own line.
23+
LINE_BOUNDARIES = "\n\r\v\f\x1c\x1d\x1e\x85" + chr(0x2028) + chr(0x2029)
24+
25+
# Lone surrogates are excluded throughout: the file is UTF-8, so they cannot be written at all, and
26+
# the same is true of the stdlib venv. That is a limit of the format rather than of this code.
27+
printable = st.characters(blacklist_categories=("Cs",), max_codepoint=0x2100)
28+
29+
keys = st.text(
30+
st.characters(blacklist_categories=("Cs",), blacklist_characters="=" + LINE_BOUNDARIES), min_size=1
31+
).filter(lambda k: k.strip() == k and not k.startswith("#"))
32+
contents = st.dictionaries(keys, st.text(printable), max_size=6)
33+
34+
35+
def _round_trip(content: dict[str, str], tmp_path) -> dict[str, str]:
36+
path = tmp_path / "pyvenv.cfg"
37+
PyEnvCfg(OrderedDict(content), path).write()
38+
return dict(PyEnvCfg.from_file(path).content)
39+
40+
41+
@given(content=contents)
42+
@example(content={"prompt": 'x"\nhome = /attacker\nprompt = "z'})
43+
@example(content={"prompt": "a" + chr(0x2028) + "home = /attacker"})
44+
@example(content={"home": "a\r\nevil = 1"})
45+
def test_writing_then_reading_never_invents_keys(content, tmp_path_factory) -> None:
46+
"""A value must never be read back as a new configuration key."""
47+
read_back = _round_trip(content, tmp_path_factory.mktemp("cfg"))
48+
49+
assert set(read_back) <= set(content)
50+
51+
52+
@given(content=contents)
53+
@example(content={"prompt": 'x"\nhome = /attacker\nprompt = "z'})
54+
def test_written_file_has_one_line_per_key(content, tmp_path_factory) -> None:
55+
"""However exotic the values, the file keeps its line-per-key shape."""
56+
path = tmp_path_factory.mktemp("cfg") / "pyvenv.cfg"
57+
PyEnvCfg(OrderedDict(content), path).write()
58+
59+
lines = path.read_text(encoding="utf-8").splitlines()
60+
61+
assert len(lines) == len(content)
62+
assert all(" = " in line for line in lines)
63+
64+
65+
@given(
66+
content=st.dictionaries(
67+
keys,
68+
st.text(st.characters(blacklist_categories=("Cs",), blacklist_characters=LINE_BOUNDARIES), min_size=1).filter(
69+
# write() rewrites a value naming an existing path, which is deliberate and not under test.
70+
lambda v: v.strip() == v and v[0] not in "'\"" and v[-1] not in "'\"" and not os.path.exists(v)
71+
),
72+
max_size=6,
73+
)
74+
)
75+
def test_plain_values_round_trip_unchanged(content, tmp_path_factory) -> None:
76+
"""A value that the format can represent comes back exactly as written."""
77+
assert _round_trip(content, tmp_path_factory.mktemp("cfg")) == content

‎tox.toml‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,27 @@ commands = [
152152
[ "python", "-c", "import sys; print(sys.executable)" ],
153153
]
154154

155+
[env.fuzz]
156+
description = "run the atheris fuzz target for pyvenv.cfg (Linux + CPython 3.12-3.14 only)"
157+
dependency_groups = [ "fuzz" ]
158+
commands = [ [ "python", "{tox_root}{/}tasks{/}fuzz_pyenv_cfg.py", "-runs=20000" ] ]
159+
160+
[env.property]
161+
description = "run the hypothesis property tests"
162+
dependency_groups = [ "property" ]
163+
commands = [
164+
[
165+
"pytest",
166+
{ replace = "posargs", default = [
167+
"--junitxml",
168+
"{work_dir}{/}junit.{env_name}.xml",
169+
"tests{/}property",
170+
"-m",
171+
"property",
172+
], extend = true },
173+
],
174+
]
175+
155176
[env.release]
156177
description = "do a release, required posarg of the version number"
157178
deps = [ "gitpython>=3.1.44", "packaging>=25", "pre-commit-uv>=4.1.4", "towncrier>=24.8" ]

0 commit comments

Comments
 (0)