瀏覽代碼

serve: fix forced command lines containing BORG_ env vars

Marian Beermann 8 年之前
父節點
當前提交
88dfb3e9c5
共有 2 個文件被更改,包括 11 次插入0 次删除
  1. 4 0
      src/borg/archiver.py
  2. 7 0
      src/borg/testsuite/archiver.py

+ 4 - 0
src/borg/archiver.py

@@ -4,6 +4,7 @@ import faulthandler
 import functools
 import functools
 import hashlib
 import hashlib
 import inspect
 import inspect
+import itertools
 import json
 import json
 import logging
 import logging
 import os
 import os
@@ -3306,6 +3307,9 @@ class Archiver:
         if cmd is not None and result.func == self.do_serve:
         if cmd is not None and result.func == self.do_serve:
             forced_result = result
             forced_result = result
             argv = shlex.split(cmd)
             argv = shlex.split(cmd)
+            # Drop environment variables (do *not* interpret them) before trying to parse
+            # the borg command line.
+            argv = list(itertools.dropwhile(lambda arg: '=' in arg, argv))
             result = self.parse_args(argv[1:])
             result = self.parse_args(argv[1:])
             if result.func != forced_result.func:
             if result.func != forced_result.func:
                 # someone is trying to execute a different borg subcommand, don't do that!
                 # someone is trying to execute a different borg subcommand, don't do that!

+ 7 - 0
src/borg/testsuite/archiver.py

@@ -2841,6 +2841,13 @@ def test_get_args():
                              'borg init --encryption=repokey /')
                              'borg init --encryption=repokey /')
     assert args.func == archiver.do_serve
     assert args.func == archiver.do_serve
 
 
+    # Check that environment variables in the forced command don't cause issues. If the command
+    # were not forced, environment variables would be interpreted by the shell, but this does not
+    # happen for forced commands - we get the verbatim command line and need to deal with env vars.
+    args = archiver.get_args(['borg', 'serve', ],
+                             'BORG_HOSTNAME_IS_UNIQUE=yes borg serve --info')
+    assert args.func == archiver.do_serve
+
 
 
 def test_compare_chunk_contents():
 def test_compare_chunk_contents():
     def ccc(a, b):
     def ccc(a, b):