Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/installer/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ def parse_record_file(rows: Iterable[str]) -> Iterator[tuple[str, str, str]]:
Returns an iterable of 3-value tuples, that can be passed to
:any:`RecordEntry.from_elements`.

:param rows: iterator providing lines of a RECORD (no trailing newlines).
:param rows: iterator providing lines of a RECORD (no trailing newlines),
or a text stream opened with ``newline=""``.
"""
reader = csv.reader(rows, delimiter=",", quotechar='"', lineterminator="\n")
for row_index, elements in enumerate(reader):
Expand Down
10 changes: 6 additions & 4 deletions src/installer/sources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Source of information about a wheel file."""

import io
import posixpath
import stat
import zipfile
Expand Down Expand Up @@ -242,9 +243,10 @@ def validate_record(self, *, validate_contents: bool = True) -> None:
:param validate_contents: Whether to validate content integrity.
"""
try:
record_lines = self.read_dist_info("RECORD").splitlines()
record_text = self.read_dist_info("RECORD")
record_mapping = {
record[0]: record for record in parse_record_file(record_lines)
record[0]: record
for record in parse_record_file(io.StringIO(record_text, newline=""))
}
except Exception as exc:
raise _WheelFileValidationError(
Expand Down Expand Up @@ -317,8 +319,8 @@ def get_contents(self) -> Iterator[WheelContentElement]:
:any:`AssertionError` will be raised.
"""
# Convert the record file into a useful mapping
record_lines = self.read_dist_info("RECORD").splitlines()
records = parse_record_file(record_lines)
record_text = self.read_dist_info("RECORD")
records = parse_record_file(io.StringIO(record_text, newline=""))
record_mapping = {record[0]: record for record in records}

for item in self._zipfile.infolist():
Expand Down
40 changes: 39 additions & 1 deletion tests/test_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import pytest

from installer.exceptions import InstallerError
from installer.records import parse_record_file
from installer.records import Hash, RecordEntry, parse_record_file
from installer.sources import WheelFile, WheelSource
from installer.utils import Scheme, construct_record_file


class TestWheelSource:
Expand Down Expand Up @@ -361,3 +362,40 @@ def test_rejects_record_containing_unknown_hash(self, fancy_wheel):
),
):
source.validate_record(validate_contents=True)

@pytest.mark.parametrize(
"odd_path",
[
pytest.param("fancy/we\nird.py", id="newline"),
pytest.param("fancy/we\u2028ird.py", id="line-separator"),
],
)
def test_reads_record_with_odd_characters_in_path(self, fancy_wheel, odd_path):
# RECORD is a CSV file, and the spec requires it to be "readable with the
# default reader of Python's csv module". str.splitlines() is not that
# reader: it drops the terminator inside a quoted field, and it also
# splits on characters csv does not treat as row separators.
contents = b"# odd\n"

with WheelFile.open(fancy_wheel) as source:
record_file_contents = source.read_dist_info("RECORD")

digest = urlsafe_b64encode(sha256(contents).digest()).decode().rstrip("=")
entry = RecordEntry(
path=odd_path, hash_=Hash("sha256", digest), size=len(contents)
)
new_record = construct_record_file([(Scheme("purelib"), entry)]).read().decode()

with zipfile.ZipFile(fancy_wheel, "a") as archive:
archive.writestr(odd_path, contents)
replace_file_in_zip(
fancy_wheel,
filename="fancy-1.0.0.dist-info/RECORD",
content=record_file_contents + new_record,
)

with WheelFile.open(fancy_wheel) as source:
source.validate_record(validate_contents=True)
paths = [record[0] for record, _, _ in source.get_contents()]

assert odd_path in paths
Loading