Skip to content

Commit 5702978

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 a0237fe commit 5702978

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:
@@ -155,6 +170,11 @@ while [ "$#" -ge 1 ]; do
155170
;;
156171

157172
-p|--restrict-path)
173+
# check whether the pathname is absolute
174+
if ! is_pathname_absolute "$2"; then
175+
reject_and_die "pathname \"$2\" given to the \"--restrict-path\"-option is not absolute"
176+
fi
177+
158178
restrict_path_list="${restrict_path_list}|$(print_normalised_pathname "$2")"
159179
shift # past argument
160180
;;

0 commit comments

Comments
 (0)