Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

fix(keychain): check presence without requesting passwords (#1111)

Matt Van Hornfb2ddbe73bae
diff --git a/changelog.d/+keychain-presence.security.md b/changelog.d/+keychain-presence.security.md
new file mode 100644
index 00000000..5a66e423
--- /dev/null
+++ b/changelog.d/+keychain-presence.security.md
@@ -0,0 +1 @@
+Keychain setup and listing now check existence without requesting plaintext passwords, and count existing entries by exit status. Selectively retains the safe presence-check changes from #1061; the existing credential writer is unchanged and its command-line exposure remains a separate follow-up.
diff --git a/skills/last30days/scripts/setup-keychain.sh b/skills/last30days/scripts/setup-keychain.sh
index b22e0f72..2d476c3d 100755
--- a/skills/last30days/scripts/setup-keychain.sh
+++ b/skills/last30days/scripts/setup-keychain.sh
@@ -72,7 +72,8 @@ case "$ACTION" in
list)
echo "Stored last30days-* keychain items:"
for key in "${ALL_KEYS[@]}"; do
- if security find-generic-password -a "$USER" -s "${PREFIX}${key}" -w >/dev/null 2>&1; then
+ # Existence checks do not need to decrypt or return the password.
+ if security find-generic-password -a "$USER" -s "${PREFIX}${key}" >/dev/null 2>&1; then
echo " $key"
fi
done
@@ -99,8 +100,12 @@ fi
added=0; skipped=0; replaced=0
for key in "${TARGETS[@]}"; do
- existing="$(security find-generic-password -a "$USER" -s "${PREFIX}${key}" -w 2>/dev/null || true)"
- if [[ -n "$existing" && "$REPLACE" -eq 0 ]]; then
+ if security find-generic-password -a "$USER" -s "${PREFIX}${key}" >/dev/null 2>&1; then
+ existed=1
+ else
+ existed=0
+ fi
+ if [[ "$existed" -eq 1 && "$REPLACE" -eq 0 ]]; then
printf " %-28s (set, skipping — use --replace to overwrite)\n" "$key"
skipped=$((skipped + 1))
continue
@@ -113,7 +118,7 @@ for key in "${TARGETS[@]}"; do
continue
fi
security add-generic-password -U -a "$USER" -s "${PREFIX}${key}" -w "$value"
- if [[ -n "$existing" ]]; then
+ if [[ "$existed" -eq 1 ]]; then
replaced=$((replaced + 1))
else
added=$((added + 1))
diff --git a/tests/test_env_keychain.py b/tests/test_env_keychain.py
index 72cec721..b1737201 100644
--- a/tests/test_env_keychain.py
+++ b/tests/test_env_keychain.py
@@ -10,7 +10,9 @@ Covers:
from __future__ import annotations
+import os
import re
+import shlex
import subprocess
from pathlib import Path
from unittest import mock
@@ -21,6 +23,66 @@ from lib import env
SETUP_KEYCHAIN_SH = Path(__file__).resolve().parents[1] / "skills" / "last30days" / "scripts" / "setup-keychain.sh"
+
+def _plaintext_presence_checks(script: str) -> list[str]:
+ # Join shell continuations before tokenizing so a split -w cannot hide.
+ logical = re.sub(r"\\\r?\n", " ", script)
+ offenders = []
+ for line in logical.splitlines():
+ code = " ".join(shlex.split(line, comments=True))
+ if re.search(r"find-generic-password\b.*\s-[wg](?=\s|[;>|&]|$)", code):
+ offenders.append(line)
+ return offenders
+
+
+def test_setup_presence_checks_do_not_request_plaintext():
+ assert not _plaintext_presence_checks(SETUP_KEYCHAIN_SH.read_text(encoding="utf-8"))
+
+
+def test_presence_guard_detects_multiline_password_flags():
+ script = (
+ "security find-generic-password \\\n"
+ ' -a "$USER" \\\n'
+ " -w >/dev/null"
+ )
+ assert _plaintext_presence_checks(script)
+ assert not _plaintext_presence_checks('# security find-generic-password -w\n')
+
+
[email protected]("existed,replace,value,summary", [
+ (True, False, "", "added=0 replaced=0 skipped=1"),
+ (False, False, "DUMMY-VALUE\n", "added=1 replaced=0 skipped=0"),
+ (True, True, "DUMMY-VALUE\n", "added=0 replaced=1 skipped=0"),
+])
[email protected](os.name == "nt", reason="POSIX shell fixture")
+def test_setup_presence_and_counters_use_stub_security(tmp_path, existed, replace, value, summary):
+ stub = tmp_path / "security"
+ stub.write_text(
+ '#!/bin/sh\n'
+ 'printf "%s\\n" "$1" >> "$STUB_LOG"\n'
+ 'case "$1" in\n'
+ ' find-generic-password) exit "$STUB_STATUS" ;;\n'
+ ' add-generic-password) exit 0 ;;\n'
+ ' *) exit 99 ;;\n'
+ 'esac\n', encoding="utf-8",
+ )
+ stub.chmod(0o755)
+ log_path = tmp_path / "calls"
+ command = ["/bin/bash", str(SETUP_KEYCHAIN_SH)]
+ if replace:
+ command.append("--replace")
+ command.append("OPENAI_API_KEY")
+ result = subprocess.run(
+ command, input=value, text=True, capture_output=True, timeout=5,
+ env={"PATH": str(tmp_path), "USER": "fixture-user", "OSTYPE": "darwin",
+ "STUB_LOG": str(log_path), "STUB_STATUS": "0" if existed else "44"},
+ )
+ assert result.returncode == 0, result.stderr
+ assert summary in result.stdout
+ assert "DUMMY-VALUE" not in result.stdout + result.stderr
+ calls = log_path.read_text().splitlines()
+ assert calls == ["find-generic-password"] + ([] if existed and not replace else ["add-generic-password"])
+
# ---------------------------------------------------------------------------
# _load_keychain unit tests
# ---------------------------------------------------------------------------