Kaynağa Gözat

Merge pull request #2015 from ThomasWaldmann/fix-location-regex

fix bad parsing of wrong syntax
TW 8 yıl önce
ebeveyn
işleme
ca0c1dab11
2 değiştirilmiş dosya ile 14 ekleme ve 3 silme
  1. 9 3
      borg/helpers.py
  2. 5 0
      borg/testsuite/helpers.py

+ 9 - 3
borg/helpers.py

@@ -823,11 +823,17 @@ class Location:
     """
 
     # path must not contain :: (it ends at :: or string end), but may contain single colons.
-    # to avoid ambiguities with other regexes, it must also not start with ":".
+    # to avoid ambiguities with other regexes, it must also not start with ":" nor with "//" nor with "ssh://".
     path_re = r"""
-        (?!:)                                               # not starting with ":"
+        (?!(:|//|ssh://))                                   # not starting with ":" or // or ssh://
         (?P<path>([^:]|(:(?!:)))+)                          # any chars, but no "::"
         """
+    # abs_path must not contain :: (it ends at :: or string end), but may contain single colons.
+    # it must start with a / and that slash is part of the path.
+    abs_path_re = r"""
+        (?P<path>(/([^:]|(:(?!:)))+))                       # start with /, then any chars, but no "::"
+        """
+
     # optional ::archive_name at the end, archive name must not contain "/".
     # borg mount's FUSE filesystem creates one level of directories from
     # the archive names and of course "/" is not valid in a directory name.
@@ -842,7 +848,7 @@ class Location:
         (?P<proto>ssh)://                                   # ssh://
         """ + optional_user_re + r"""                       # user@  (optional)
         (?P<host>[^:/]+)(?::(?P<port>\d+))?                 # host or host:port
-        """ + path_re + optional_archive_re, re.VERBOSE)    # path or path::archive
+        """ + abs_path_re + optional_archive_re, re.VERBOSE)  # path or path::archive
 
     file_re = re.compile(r"""
         (?P<proto>file)://                                  # file://

+ 5 - 0
borg/testsuite/helpers.py

@@ -134,6 +134,11 @@ class TestLocationWithoutEnv:
         location_time2 = Location('/some/path::archive{now:%s}')
         assert location_time1.archive != location_time2.archive
 
+    def test_bad_syntax(self):
+        with pytest.raises(ValueError):
+            # this is invalid due to the 2nd colon, correct: 'ssh://user@host/path'
+            Location('ssh://user@host:/path')
+
 
 class TestLocationWithEnv:
     def test_ssh(self, monkeypatch):