From 949871d46345a8d0184f076839b2dfb60e32f4a6 Mon Sep 17 00:00:00 2001 From: Marian Beermann Date: Sat, 1 Apr 2017 21:10:31 +0200 Subject: [PATCH] serve: fix forced command lines containing BORG_ env vars (cherry picked from commit 88dfb3e9c53bb335e339307232849e9ad0426bba) --- borg/archiver.py | 4 ++++ borg/testsuite/archiver.py | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/borg/archiver.py b/borg/archiver.py index 20eb49c4f..e0e54c7b6 100644 --- a/borg/archiver.py +++ b/borg/archiver.py @@ -6,6 +6,7 @@ import argparse import faulthandler import functools import inspect +import itertools import os import re import shlex @@ -2060,6 +2061,9 @@ class Archiver: if cmd is not None and result.func == self.do_serve: forced_result = result 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:]) if result.func != forced_result.func: # someone is trying to execute a different borg subcommand, don't do that! diff --git a/borg/testsuite/archiver.py b/borg/testsuite/archiver.py index 2100808d8..028b7f43f 100644 --- a/borg/testsuite/archiver.py +++ b/borg/testsuite/archiver.py @@ -1800,6 +1800,13 @@ def test_get_args(): 'borg init /') 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 + class TestBuildFilter: def test_basic(self):