From a0237fe540594ef8426e9d7395fa9b12767901dc Mon Sep 17 00:00:00 2001 From: Christoph Anton Mitterer Date: Tue, 22 Nov 2022 04:11:31 +0100 Subject: [PATCH] ssh_filter_btrbk.sh: properly normalise pathnames Previously, pathnames specified via the `--restrict-path`-option had only a single trailing `/`, if any, stripped. This commit adds (and utilises) a function which normalises pathnames as described in its comments. Signed-off-by: Christoph Anton Mitterer --- ssh_filter_btrbk.sh | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ssh_filter_btrbk.sh b/ssh_filter_btrbk.sh index c1616e5..b8ac75e 100755 --- a/ssh_filter_btrbk.sh +++ b/ssh_filter_btrbk.sh @@ -23,6 +23,20 @@ file_match_sane='/[0-9a-zA-Z_@+./-]*' # matches file path (equal to ${file_match file_match="/[^']*" # btrbk >= 0.32.0 quotes file arguments: match all but single quote file_arg_match="('${file_match}'|${file_match_sane})" # support btrbk < 0.32.0 +print_normalised_pathname() +{ + # Normalises a pathname given via the positional parameter #1 as follows: + # * Folds any >=3 leading `/` into 1. + # POSIX specifies that implementations may treat exactly 2 leading `//` + # specially and therefore such are not folded here. + # * Folds any >=2 non-leading `/` into 1. + # * Strips any trailing `/`. + + local pathname="$1" + + printf '%s' "${pathname}" | sed -E 's%^///+%/%; s%(.)//+%\1/%g; s%/+$%%' +} + log_cmd() { local priority="$1" @@ -141,7 +155,7 @@ while [ "$#" -ge 1 ]; do ;; -p|--restrict-path) - restrict_path_list="${restrict_path_list}|${2%/}" # add to list while removing trailing slash + restrict_path_list="${restrict_path_list}|$(print_normalised_pathname "$2")" shift # past argument ;;