Skip to content

Commit 096148f

Browse files
escape or reject a bare carriage return in multiline strings (#539)
* escape or reject a bare carriage return in multiline strings * fold the lone CR rejection into the invalid_sequences check
1 parent 5fe4329 commit 096148f

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎tests/test_api.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,10 @@ def test_create_super_table_with_aot() -> None:
578578
({"multiline": True}, 'My""String', '"""My""String"""'),
579579
({"multiline": True}, 'My"""String', '"""My""\\"String"""'),
580580
({"multiline": True}, 'My""""String', '"""My""\\""String"""'),
581+
# a lone CR is a control char and is escaped, but a CRLF line ending is
582+
# left intact
583+
({"multiline": True}, "My\rString", '"""My\\rString"""'),
584+
({"multiline": True}, "My\r\nString", '"""My\r\nString"""'),
581585
(
582586
{"multiline": True},
583587
'"""My"""Str"""ing"""',
@@ -639,6 +643,9 @@ def test_create_multiline_string_with_leading_newline_round_trips(
639643
({"literal": True}, "My\x0cString"),
640644
({"literal": True}, "My\x7fString"),
641645
({"multiline": True, "literal": True}, "My'''String"),
646+
# a lone CR cannot be represented in a multiline literal string
647+
({"multiline": True, "literal": True}, "My\rString"),
648+
({"multiline": True, "literal": True}, "My\r\nMy\rString"),
642649
],
643650
)
644651
def test_create_string_with_invalid_characters(

‎tomlkit/items.py‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ def _format_time(value: time) -> str:
255255
return s
256256

257257

258+
# A carriage return that is not the CR of a CRLF line ending. Inside a multiline
259+
# string only a raw line feed or a CRLF pair is a valid line ending; a lone CR is
260+
# a control character that a basic string must escape.
261+
_BARE_CR = re.compile(r"\r(?!\n)")
262+
263+
258264
class StringType(Enum):
259265
# Single Line Basic
260266
SLB = '"'
@@ -290,12 +296,13 @@ def escaped_sequences(self) -> Collection[str]:
290296
def invalid_sequences(self) -> Collection[str]:
291297
# https://toml.io/en/v1.0.0#string
292298
forbidden_in_literal = CONTROL_CHARS - {"\t"}
293-
allowed_in_multiline = {"\n", "\r"}
299+
# CR stays forbidden in multiline: the value is checked with CRLF
300+
# pairs collapsed, so any remaining CR is a lone control character
294301
return {
295302
StringType.SLB: (),
296303
StringType.MLB: (),
297304
StringType.SLL: forbidden_in_literal | {"'"},
298-
StringType.MLL: (forbidden_in_literal | {"'''"}) - allowed_in_multiline,
305+
StringType.MLL: (forbidden_in_literal | {"'''"}) - {"\n"},
299306
}[self]
300307

301308
@property
@@ -2293,12 +2300,17 @@ def from_raw(
22932300
value = decode(value)
22942301

22952302
invalid = type_.invalid_sequences
2296-
if any(c in value for c in invalid):
2303+
# collapse CRLF line endings so only a lone CR trips the CR check
2304+
checked = value.replace("\r\n", "\n")
2305+
if any(c in checked for c in invalid):
22972306
raise InvalidStringError(value, invalid, type_.value)
22982307

22992308
escaped = type_.escaped_sequences
23002309
string_value = escape_string(value, escaped) if escape and escaped else value
23012310

2311+
if type_ is StringType.MLB and escape:
2312+
string_value = _BARE_CR.sub(r"\\r", string_value)
2313+
23022314
if type_.is_multiline() and string_value[:1] in ("\n", "\r"):
23032315
# A newline immediately following the opening delimiter of a
23042316
# multiline string is trimmed by the parser, so a value that

0 commit comments

Comments
 (0)