Skip to content

Commit cf9b8d3

Browse files
committed
fix(pg-node): prevent unzip from hanging on password prompts during restore
1 parent 6c246d9 commit cf9b8d3

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

‎pg-node/restore.sh‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -311,25 +311,23 @@ download_backup() {
311311
}
312312

313313
_extract_archive() {
314-
# Check if archive is a ZIP file (either unencrypted or testable without password)
315-
if unzip -t "${TMP_ARCHIVE}" &>/dev/null; then
314+
# Check if file is a ZIP archive (starts with PK magic bytes or passes unzip -t)
315+
if [[ "$(head -c 2 "${TMP_ARCHIVE}" 2>/dev/null)" == "PK" ]] || unzip -t "${TMP_ARCHIVE}" < /dev/null &>/dev/null; then
316+
local unzip_cmd=(unzip -o)
316317
if [[ -n "${BACKUP_PASS}" ]]; then
317-
unzip -o -P "${BACKUP_PASS}" "${TMP_ARCHIVE}" -d /
318-
else
319-
unzip -o "${TMP_ARCHIVE}" -d /
318+
unzip_cmd+=(-P "${BACKUP_PASS}")
320319
fi
321-
# If unzip -t fails but password is provided, try extracting with password
322-
elif [[ -n "${BACKUP_PASS}" ]] && unzip -o -P "${BACKUP_PASS}" -t "${TMP_ARCHIVE}" &>/dev/null; then
323-
unzip -o -P "${BACKUP_PASS}" "${TMP_ARCHIVE}" -d /
324-
# Check magic bytes for ZIP header (PK)
325-
elif [[ "$(head -c 2 "${TMP_ARCHIVE}" 2>/dev/null)" == "PK" ]]; then
326-
if [[ -n "${BACKUP_PASS}" ]]; then
327-
unzip -o -P "${BACKUP_PASS}" "${TMP_ARCHIVE}" -d /
328-
else
329-
if ! unzip -o "${TMP_ARCHIVE}" -d /; then
330-
echo "ZIP extraction failed. Password may be required (-p <password>)." >&2
331-
return 1
320+
unzip_cmd+=("${TMP_ARCHIVE}" -d /)
321+
322+
# Redirecting stdin from /dev/null ensures unzip never hangs waiting for password input
323+
if ! "${unzip_cmd[@]}" < /dev/null; then
324+
echo "ZIP extraction failed." >&2
325+
if [[ -z "${BACKUP_PASS}" ]]; then
326+
echo "The backup file appears to be password-protected. Please pass the password using: -p <password>" >&2
327+
else
328+
echo "The provided password (-p) may be incorrect." >&2
332329
fi
330+
return 1
333331
fi
334332
else
335333
tar -xzpf "${TMP_ARCHIVE}" -C /

0 commit comments

Comments
 (0)