diff --git a/.jules/sentinel.md b/.jules/sentinel.md new file mode 100644 index 0000000..cb6d463 --- /dev/null +++ b/.jules/sentinel.md @@ -0,0 +1,4 @@ +## 2024-05-22 - Secure File Creation in Shell Scripts +**Vulnerability:** TOCTOU race condition when creating sensitive files (like SSH keys) using redirection (`>`) followed by `chmod`. +**Learning:** Files created via redirection inherit default permissions (usually 644/666) before `chmod` runs, leaving a window where they are world-readable. +**Prevention:** Use `umask` inside a subshell to strictly control permissions at creation time: `(umask 077; command > file)`. diff --git a/tools/setup-ssh-keys.sh b/tools/setup-ssh-keys.sh index bde52fd..aef9fe0 100755 --- a/tools/setup-ssh-keys.sh +++ b/tools/setup-ssh-keys.sh @@ -153,12 +153,16 @@ cmd_restore() { chmod 700 "$SSH_DIR" # Read private key from 1Password and save locally - op read "op://$VAULT/$KEY_NAME/private_key" > "$PRIVATE_KEY_FILE" - chmod 600 "$PRIVATE_KEY_FILE" + ( + umask 077 + op read "op://$VAULT/$KEY_NAME/private_key" > "$PRIVATE_KEY_FILE" + ) # Read public key from 1Password and save locally - op read "op://$VAULT/$KEY_NAME/public_key" > "$PUBLIC_KEY_FILE" - chmod 644 "$PUBLIC_KEY_FILE" + ( + umask 022 + op read "op://$VAULT/$KEY_NAME/public_key" > "$PUBLIC_KEY_FILE" + ) say "SSH key restored to $SSH_DIR" echo ""