Skip to content

Commit 016e945

Browse files
committed
ssh_filter_btrbk.sh: forbid non-absolute pathnames to --restrict-path
This commit adds a function which checks whether a pathname is absolute and rejects and values to the `--restrict-path`-option which are not. The idea here is mostly a safeguard for users to prevent accidentally specified non-absolute pathnames, which would be taken relative to the executing user’s home-directory. Signed-off-by: Christoph Anton Mitterer <[email protected]>
1 parent 7729be4 commit 016e945

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

ssh_filter_btrbk.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ file_match_sane='/[0-9a-zA-Z_@+./-]*' # matches file path (equal to ${file_match
2323
file_match="/[^']*" # btrbk >= 0.32.0 quotes file arguments: match all but single quote
2424
file_arg_match="('${file_match}'|${file_match_sane})" # support btrbk < 0.32.0
2525

26+
is_pathname_absolute()
27+
{
28+
# Checks whether a string is an absolute pathname (that is: one that is non-
29+
# empty and starts with either exactly one or more than two `/`).
30+
31+
local pathname="$1"
32+
33+
[ "${pathname}" != '//' ] || return 1
34+
[ -n "${pathname##//[!/]*}" ] || return 1
35+
[ -z "${pathname##/*}" ] || return 1
36+
[ -n "${pathname}" ] || return 1
37+
38+
return 0
39+
}
40+
2641
print_normalised_pathname()
2742
{
2843
# Normalises a pathname given via the positional parameter #1 as follows:
@@ -161,6 +176,11 @@ while [ "$#" -ge 1 ]; do
161176
;;
162177

163178
-p|--restrict-path)
179+
# check whether the pathname is absolute
180+
if ! is_pathname_absolute "$2"; then
181+
reject_and_die "pathname \"$2\" given to the \"--restrict-path\"-option is not absolute"
182+
fi
183+
164184
restrict_path_list="${restrict_path_list}|$(print_normalised_pathname "$2")"
165185
shift # past argument
166186
;;

0 commit comments

Comments
 (0)