Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(scripts): refuse a directory as a database or key file target
Yudhi Armyndharisd31d6a70887e
diff --git a/scripts/restore.sh b/scripts/restore.shindex 0c78bc85b..50c2b14c7 100755--- a/scripts/restore.sh+++ b/scripts/restore.sh@@ -164,6 +164,16 @@ refuse_root_target() {fi}+# refuse_directory_target <path> <label>: a database or key file is a file. Given a directory (the workspace's+# parent, say, from a typo in a path) the restore copied it whole into the safety snapshot, wrote the file+# inside it, and then took the search bit off it with the chmod 0600 that follows a database or key file.+refuse_directory_target() {+ if [ -d "$1" ]; then+ log "ERROR: refusing to replace $2 target that is a directory: $1"+ exit 1+ fi+}+replace_tree() {source_dir="$1"target_dir="$2"@@ -254,6 +264,7 @@ restore_db() {resolved_db="$(resolve_path "$2")"if [ "$PHASE" = snapshot ]; thenrefuse_root_target "$2" "$3"+ refuse_directory_target "$2" "$3"openwa_writable "$2" || refuse_unwritable "$2" "$3"for db in "$2" "$resolved_db"; dofor sfx in -wal -shm -journal; do@@ -483,6 +494,7 @@ restore_targets() {restore_admin_key() {if [ "$PHASE" = snapshot ]; thenrefuse_root_target "$ADMIN_KEY_FILE" "admin key (BOOTSTRAP_KEY_FILE)"+ refuse_directory_target "$ADMIN_KEY_FILE" "admin key (BOOTSTRAP_KEY_FILE)"openwa_writable "$ADMIN_KEY_FILE" || refuse_unwritable "$ADMIN_KEY_FILE" "admin key (BOOTSTRAP_KEY_FILE)"snapshot_external "$ADMIN_KEY_FILE"returndiff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.shindex 12a2a8545..866aba15f 100755--- a/scripts/smoke-test-backup-restore.sh+++ b/scripts/smoke-test-backup-restore.sh@@ -89,6 +89,9 @@# link to /, one of those followed by .. or a link whose target ends in one, or a link to / whose# name is not valid UTF-8, ahead of the writability check a non-root user would stop at (skipped# where a user can write /)+# (as) restore refuses a MAIN_DATABASE_NAME, DATABASE_NAME or BOOTSTRAP_KEY_FILE that names an+# existing directory (the workspace's parent, the data dir, another directory, a link to one),+# before it copies, writes into or changes the mode of it## Usage: ./scripts/smoke-test-backup-restore.sh# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).@@ -2701,5 +2704,34 @@ elseecho "SKIP: (ar) the / cases, which need a user who cannot write /"fi+echo ""+echo "==> (as) restore refuses a database or key file target that is a directory"+# These targets are files. A directory there was copied whole into the safety snapshot, received the file inside+# it, and then lost its search bit to the chmod 0600 that follows a database or key file, so the workspace's own+# parent could become untraversable for its owner. tree_digest does not see a mode, so it is read separately.+AS="$WORK/as"+mkdir -p "$AS/top/proj/data" "$AS/other/sub" "$AS/home"+printf 'as-keep\n' >"$AS/top/keep.txt"+printf 'as-keep\n' >"$AS/other/keep.txt"+ln -s other "$AS/otherlink"+dir_mode() {+ for bit in 400 200 100 040 020 010 004 002 001; do+ if [ -n "$(find "$1/." -maxdepth 0 -perm -"$bit")" ]; then printf 1; else printf 0; fi+ done+}+for target in 'MAIN_DATABASE_NAME auth/audit DB' 'DATABASE_NAME data store' 'BOOTSTRAP_KEY_FILE admin key (BOOTSTRAP_KEY_FILE)'; do+ as_var="${target%% *}"+ as_label="${target#* }"+ for as_dir in "$AS/top" "$AS/top/proj/data" "$AS/other" "$AS/otherlink"; do+ as_mode="$(dir_mode "$as_dir")"+ refused_restore "$AS" "as: $as_var=${as_dir#"$AS"/}" "refusing to replace $as_label target that is a directory" "$AS/top/proj" "$ARCHIVE_AP" \+ OPENWA_DATA_DIR="$AS/top/proj/data" "$as_var=$as_dir"+ if [ "$(dir_mode "$as_dir")" != "$as_mode" ]; then+ fail "(as: $as_var=${as_dir#"$AS"/}) the refused restore changed the mode of the directory: $as_mode to $(dir_mode "$as_dir")"+ fi+ done+done+pass "(as) MAIN_DATABASE_NAME, DATABASE_NAME and BOOTSTRAP_KEY_FILE naming a directory are refused before anything is copied or changed"+echo ""echo "All smoke tests passed!"