|
| 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 |
0 commit comments