From 21d9d62cf8e8df1894d7ecdf6652b95e363d8f5a Mon Sep 17 00:00:00 2001 From: Janik von Rotz Date: Mon, 8 Dec 2025 12:43:58 +0100 Subject: [PATCH] fix(keepass): validate database files and improve error handling in get-pass-from-keepass and save-pass-to-keepass --- bin/get-pass-from-keepass | 10 ++++++++++ bin/save-pass-to-keepass | 39 ++++++++++++++++++++++++++++++--------- library.md | 7 +++++++ task.template | 7 +++++++ 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/bin/get-pass-from-keepass b/bin/get-pass-from-keepass index ac87701..4b60a43 100755 --- a/bin/get-pass-from-keepass +++ b/bin/get-pass-from-keepass @@ -4,6 +4,16 @@ get-pass-from-keepass() { if [[ -z "$1" ]]; then echo "\$1 is empty."; exit; fi local keepass_entry="$1" + if [[ ! -f "$KEEPASS_DATABASE_FILE" ]]; then + echo "Keepass database file not found: $KEEPASS_DATABASE_FILE" + exit 1 + fi + + if [[ ! -f "$KEEPASS_DATABASE_KEY_FILE" ]]; then + echo "Keepass database key file not found: $KEEPASS_DATABASE_KEY_FILE" + exit 1 + fi + keepassxc-cli show --show-protected --attributes Password \ --key-file "$KEEPASS_DATABASE_KEY_FILE" \ "$KEEPASS_DATABASE_FILE" \ diff --git a/bin/save-pass-to-keepass b/bin/save-pass-to-keepass index 8617a8a..74bb914 100755 --- a/bin/save-pass-to-keepass +++ b/bin/save-pass-to-keepass @@ -1,15 +1,36 @@ #!/usr/bin/env bash save-pass-to-keepass() { - if [[ -z "$1" ]]; then echo "\$1 is empty."; exit; fi - if [[ -z "$2" ]]; then echo "\$2 is empty."; exit; fi + if [[ -z "$1" ]]; then + echo "\$1 (password) is empty." + exit 1 + fi + if [[ -z "$2" ]]; then + echo "\$2 (entry path) is empty." + exit 1 + fi + local password="$1" local keepass_entry="$2" - echo "Save password to keepass entry: $keepass_entry" - keepassxc-cli add \ - --key-file "$KEEPASS_DATABASE_KEY_FILE" \ - --password "$password" \ - "$KEEPASS_DATABASE_FILE" \ - "$KEEPASS_ENTRY" -} + if [[ ! -f "$KEEPASS_DATABASE_FILE" ]]; then + echo "Keepass database file not found: $KEEPASS_DATABASE_FILE" + exit 1 + fi + + if [[ ! -f "$KEEPASS_DATABASE_KEY_FILE" ]]; then + echo "Keepass database key file not found: $KEEPASS_DATABASE_KEY_FILE" + exit 1 + fi + + # Prompt for KeePass database password silently + read -s -p "Enter password to unlock $KEEPASS_DATABASE_FILE: " keepass_database_pass + echo # Add a newline after password input + + echo "Saving password to keepass entry: $keepass_entry" + echo "$keepass_database_pass" | keepassxc-cli add \ + --key-file "$KEEPASS_DATABASE_KEY_FILE" \ + --password-prompt \ + "$KEEPASS_DATABASE_FILE" \ + "$keepass_entry" <<< "$password" +} \ No newline at end of file diff --git a/library.md b/library.md index 8e8389d..87cf8d3 100644 --- a/library.md +++ b/library.md @@ -48,6 +48,13 @@ OPEN_BROWSER=False # Default: True TASK_DOTENV_DIR="$HOME/Odoo-Build/config" # Default: "$HOME/.config/taskfile.build" ``` +### clone-taskfile + +```bash +printf "$column" 'clone-taskfile' '' 'Clone the taskfile.build repo.' +``` + + ### init-config-dir ```bash diff --git a/task.template b/task.template index 64a1ae9..acf7737 100755 --- a/task.template +++ b/task.template @@ -1,5 +1,11 @@ #!/usr/bin/env bash +set -e + +if [[ -a ".env" ]]; then + source .env +fi + help-table() { local cmd_width=24 local opt_width=18 @@ -9,6 +15,7 @@ help-table() { printf "$column" "Command" "Option" "Description" echo "|$(printf '%*s' $((cmd_width + 2)) '' | tr ' ' '-')|$(printf '%*s' $((opt_width + 2)) '' | tr ' ' '-')|$(printf '%*s' $((desc_width + 2)) '' | tr ' ' '-')|" # Task + printf "$column" 'clone-taskfile' '' 'Clone the taskfile.build repo.' printf "$column" 'init-dotenv-dir' '' 'Setup task dotenv dir.' printf "$column" 'template-dotenv' '' 'Generate .env from .env.template.' printf "$column" 'open-with-os' '' 'Open file or url with default application.'