Skip to content

Commit 53a0fd5

Browse files
authored
fix: neutralize control characters in lcov report fields (#2226)
1 parent b64d53d commit 53a0fd5

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎CHANGES.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ Unreleased
2828
and prev/next links, so it could close the attribute early and inject markup.
2929
Page URLs are now escaped. Thanks, `Rajath Mohare <pull 2227_>`_.
3030

31+
- Fix: the LCOV report wrote file names and other fields into its
32+
line-oriented records without neutralizing control characters. A measured
33+
file whose name contained a newline (legal on POSIX) could forge extra
34+
records, inflating the coverage seen by tools that read the report. Control
35+
characters in a field are now replaced. Thanks, `Rajath Mohare <pull
36+
2226_>`_.
37+
3138
- Wheels are now provided for Python 3.15.
3239

40+
.. _pull 2226: https://github.com/coveragepy/coveragepy/pull/2226
3341
.. _pull 2227: https://github.com/coveragepy/coveragepy/pull/2227
3442

3543

‎coverage/lcovreport.py‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@
1919
from coverage import Coverage
2020

2121

22+
def lcov_field(text: str) -> str:
23+
"""Make `text` safe to write as a field in a single LCOV record.
24+
25+
LCOV records are newline-delimited and the format has no escaping, so a
26+
value carrying a newline (a legal character in a POSIX file name) would
27+
spill into fabricated records. Replace any control characters, which can't
28+
appear in a valid one-line field, and leave printable text untouched.
29+
"""
30+
if text.isprintable():
31+
return text
32+
return "".join(ch if ch.isprintable() else "?" for ch in text)
33+
34+
2235
def line_hash(line: str) -> str:
2336
"""Produce a hash of a source line for use in the LCOV file."""
2437
# The LCOV file format optionally allows each line to be MD5ed as a
@@ -97,8 +110,9 @@ def lcov_functions(
97110
hit = int(analysis.numbers.n_executed > 0)
98111
functions_hit += hit
99112

100-
outfile.write(f"FN:{first_line},{last_line},{region.name}\n")
101-
outfile.write(f"FNDA:{hit},{region.name}\n")
113+
name = lcov_field(region.name)
114+
outfile.write(f"FN:{first_line},{last_line},{name}\n")
115+
outfile.write(f"FNDA:{hit},{name}\n")
102116

103117
outfile.write(f"FNF:{functions_found}\n")
104118
outfile.write(f"FNH:{functions_hit}\n")
@@ -145,7 +159,7 @@ def lcov_arcs(
145159
destinations.sort(key=lambda d: (d[0] < 0, d))
146160

147161
for dst, hit in destinations:
148-
branch = fr.arc_description(line, dst)
162+
branch = lcov_field(fr.arc_description(line, dst))
149163
outfile.write(f"BRDA:{line},0,{branch},{hit}\n")
150164

151165
# Summary of the branch coverage.
@@ -207,7 +221,7 @@ def lcov_file(
207221
if self.config.skip_empty:
208222
return
209223

210-
outfile.write(f"SF:{rel_fname}\n")
224+
outfile.write(f"SF:{lcov_field(rel_fname)}\n")
211225

212226
lines = sorted(analysis.statements)
213227
if self.config.lcov_line_checksums:

‎tests/test_lcov.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
from __future__ import annotations
77

88
import math
9+
import os
910
import textwrap
1011

12+
import pytest
13+
1114
import coverage
15+
from coverage import env
1216

1317
from tests.coveragetest import CoverageTest
1418

@@ -125,6 +129,27 @@ def IsItTrue():
125129
actual_result = self.get_lcov_report_content()
126130
assert expected_result == actual_result
127131

132+
@pytest.mark.skipif(env.WINDOWS, reason="Newlines are disallowed in Windows filenames.")
133+
def test_newline_in_filename_does_not_inject_records(self) -> None:
134+
# A POSIX file name may contain a newline. Written verbatim into the
135+
# line-oriented LCOV format it would forge extra records (a fake file,
136+
# inflated hit counts), so control characters in a field are replaced.
137+
evil = "a\nLH:9999\nSF:injected.py\nend_of_record\nreal.py"
138+
self.make_file(evil, "a = 1\nb = 2\n")
139+
self.make_data_file(lines={os.path.abspath(evil): [1, 2]})
140+
cov = coverage.Coverage()
141+
cov.load()
142+
cov.lcov_report()
143+
expected_result = textwrap.dedent("""\
144+
SF:a?LH:9999?SF:injected.py?end_of_record?real.py
145+
DA:1,1
146+
DA:2,1
147+
LF:2
148+
LH:2
149+
end_of_record
150+
""")
151+
assert expected_result == self.get_lcov_report_content()
152+
128153
def test_simple_line_coverage_two_files(self) -> None:
129154
# Test that line coverage is created when coverage is run,
130155
# and matches the output of the file below.

0 commit comments

Comments
 (0)