Browse Source

format_line: deny conversions (!r, !s, !a)

Marian Beermann 8 năm trước cách đây
mục cha
commit
1bd381a13a
2 tập tin đã thay đổi với 8 bổ sung3 xóa
  1. 4 3
      src/borg/helpers.py
  2. 4 0
      src/borg/testsuite/helpers.py

+ 4 - 3
src/borg/helpers.py

@@ -784,9 +784,10 @@ class DatetimeWrapper:
 
 
 def format_line(format, data):
-    keys = [f[1] for f in Formatter().parse(format)]
-    for key in keys:
-        if '.' in key or '__' in key:
+    for _, key, _, conversion in Formatter().parse(format):
+        if not key:
+            continue
+        if '.' in key or '__' in key or conversion:
             raise InvalidPlaceholder(key, format)
     try:
         return format.format(**data)

+ 4 - 0
src/borg/testsuite/helpers.py

@@ -1213,6 +1213,10 @@ def test_format_line_erroneous():
         assert format_line('{invalid}', data)
     with pytest.raises(PlaceholderError):
         assert format_line('{}', data)
+    with pytest.raises(PlaceholderError):
+        assert format_line('{now!r}', data)
+    with pytest.raises(PlaceholderError):
+        assert format_line('{now.__class__.__module__.__builtins__}', data)
 
 
 def test_replace_placeholders():