From 54f36dd79961f14a4cc7ec5998553c23aac421dd Mon Sep 17 00:00:00 2001 From: Jonathan Mohrbacher Date: Tue, 1 Dec 2026 13:29:05 -0500 Subject: [PATCH 1/2] Unlink Chrome-Agent 1Password host and add share-to-agent CLI helper - Remove Chrome-Agent from home.activation.linkOnePasswordNativeHost in users/dev/home.darwin.nix and clean up any existing manifest file/symlink so Chrome-Agent's 1Password extension can run standalone against a dedicated vault. - Install pkgs._1password-cli and a share-to-agent helper on macOS to copy a single 1Password item into the Agent vault via interactive approval, without printing secrets, calling op signout on exit to close the CLI session immediately. - Add unit tests in scripts/test-share-to-agent.sh and wire into CI. --- .github/workflows/ci.yml | 1 + assets/scripts/share-to-agent.sh | 163 +++++++++++++++++++++++++++++++ scripts/test-share-to-agent.sh | 115 ++++++++++++++++++++++ users/dev/home.darwin.nix | 29 +++--- 4 files changed, 297 insertions(+), 11 deletions(-) create mode 100755 assets/scripts/share-to-agent.sh create mode 100755 scripts/test-share-to-agent.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 56fd0b22..8d8abadc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -102,6 +102,7 @@ jobs: set -euo pipefail python3 scripts/check-sops-encrypted.py bash scripts/test-check-sops-encrypted.sh + bash scripts/test-share-to-agent.sh nix shell --inputs-from . nixpkgs#gitleaks -c \ gitleaks detect --source . --no-git --redact --no-banner diff --git a/assets/scripts/share-to-agent.sh b/assets/scripts/share-to-agent.sh new file mode 100755 index 00000000..32fc5ec8 --- /dev/null +++ b/assets/scripts/share-to-agent.sh @@ -0,0 +1,163 @@ +#!/usr/bin/env bash +# Copy a single 1Password item into a dedicated vault (default: "Agent") using +# the 1Password CLI (`op`). Designed for interactive approval (Touch ID or +# account password via desktop-app integration) and never prints secret values. +# +# Note on session lifetime: 1Password CLI app integration establishes a ~10-min +# sliding session scoped to the calling tty. This helper runs `op signout` in +# an EXIT trap immediately after the copy (success or failure) to end the CLI +# session right away; locking the 1Password desktop app also revokes it. +set -euo pipefail + +DEST_VAULT="Agent" +SRC_VAULT="" +ACCOUNT="" +DRY_RUN=0 +ITEM="" + +usage() { + cat >&2 <<'EOF' +Usage: share-to-agent [--vault ] [--dest ] [--account ] [--dry-run] + +Copies a single 1Password item (by title or ID) into the destination vault +(default: Agent) without modifying the original item or printing secret values. +EOF + exit 2 +} + +while [ "$#" -gt 0 ]; do + case "$1" in + --vault) + [ "$#" -ge 2 ] || usage + SRC_VAULT="$2" + shift 2 + ;; + --dest) + [ "$#" -ge 2 ] || usage + DEST_VAULT="$2" + shift 2 + ;; + --account) + [ "$#" -ge 2 ] || usage + ACCOUNT="$2" + shift 2 + ;; + --dry-run) + DRY_RUN=1 + shift + ;; + -h|--help) + usage + ;; + --) + shift + break + ;; + -*) + echo "share-to-agent: unknown option: $1" >&2 + usage + ;; + *) + if [ -n "$ITEM" ]; then + echo "share-to-agent: unexpected extra argument: $1" >&2 + usage + fi + ITEM="$1" + shift + ;; + esac +done + +if [ -z "$ITEM" ] && [ "$#" -eq 1 ]; then + ITEM="$1" +elif [ "$#" -gt 0 ]; then + usage +fi + +[ -n "$ITEM" ] || usage + +TMP_DIR="$(mktemp -d)" +chmod 700 "$TMP_DIR" + +ACCOUNT_ARGS=() +if [ -n "$ACCOUNT" ]; then + ACCOUNT_ARGS+=(--account "$ACCOUNT") +fi + +cleanup() { + rm -rf "$TMP_DIR" + if [ "${#ACCOUNT_ARGS[@]}" -gt 0 ]; then + op signout "${ACCOUNT_ARGS[@]}" >/dev/null 2>&1 || true + else + op signout >/dev/null 2>&1 || true + fi +} +trap cleanup EXIT + +RAW_JSON="$TMP_DIR/raw.json" +CLEAN_JSON="$TMP_DIR/clean.json" +CREATED_JSON="$TMP_DIR/created.json" +( + umask 077 + : >"$RAW_JSON" + : >"$CLEAN_JSON" + : >"$CREATED_JSON" +) + +GET_ARGS=("${ACCOUNT_ARGS[@]}" --format json --reveal) +if [ -n "$SRC_VAULT" ]; then + GET_ARGS+=(--vault "$SRC_VAULT") +fi + +op item get "$ITEM" "${GET_ARGS[@]}" >"$RAW_JSON" + +TITLE="$(jq -r '.title // ""' "$RAW_JSON")" +CATEGORY="$(jq -r '.category // ""' "$RAW_JSON")" +ACTUAL_SRC_VAULT="$(jq -r '.vault.name // ""' "$RAW_JSON")" +ACTUAL_SRC_VAULT_ID="$(jq -r '.vault.id // ""' "$RAW_JSON")" +FILE_COUNT="$(jq -r '(.files // []) | length' "$RAW_JSON")" + +if [ "${ACTUAL_SRC_VAULT,,}" = "${DEST_VAULT,,}" ] || [ "$ACTUAL_SRC_VAULT_ID" = "$DEST_VAULT" ]; then + echo "share-to-agent: item '$TITLE' is already in vault '$ACTUAL_SRC_VAULT'; refusing to duplicate." >&2 + exit 1 +fi + +if [ "$FILE_COUNT" -gt 0 ]; then + echo "share-to-agent: warning: item '$TITLE' has $FILE_COUNT file attachment(s); JSON template copy does not copy attachments." >&2 +fi + +if [ "$DRY_RUN" = "1" ]; then + FIELD_LABELS="$(jq -r '[.fields[]? | .label // .id // empty] | join(", ")' "$RAW_JSON")" + echo "share-to-agent (dry-run): would copy '$TITLE' ($CATEGORY) from '$ACTUAL_SRC_VAULT' -> '$DEST_VAULT'" + echo " fields: ${FIELD_LABELS:-}" + exit 0 +fi + +if jq -e 'any(.fields[]?; (.value | type == "string") and (.value | test("^\\[use '\''op item get ")))' "$RAW_JSON" >/dev/null; then + echo "share-to-agent: refusing to copy item with concealed placeholder field values." >&2 + exit 1 +fi + +jq ' + del( + .id, + .vault, + .created_at, + .updated_at, + .last_edited_by, + .version, + .additional_information, + .files + ) + | if .fields then + .fields |= map(del(.totp, .reference, .password_details)) + else + . + end +' "$RAW_JSON" >"$CLEAN_JSON" + +op item create "${ACCOUNT_ARGS[@]}" --vault "$DEST_VAULT" --format json - <"$CLEAN_JSON" >"$CREATED_JSON" + +NEW_ID="$(jq -r '.id // ""' "$CREATED_JSON")" +NEW_VAULT="$(jq -r --arg d "$DEST_VAULT" '.vault.name // $d' "$CREATED_JSON")" +echo "share-to-agent: copied '$TITLE' ($CATEGORY) from '$ACTUAL_SRC_VAULT' -> '$NEW_VAULT' (id: $NEW_ID)" diff --git a/scripts/test-share-to-agent.sh b/scripts/test-share-to-agent.sh new file mode 100755 index 00000000..d44f0118 --- /dev/null +++ b/scripts/test-share-to-agent.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" +HELPER="$ROOT/assets/scripts/share-to-agent.sh" + +TMP="$(mktemp -d)" +trap 'rm -rf "$TMP"' EXIT + +pass() { echo "PASS: $1"; } +fail() { echo "FAIL: $1" >&2; exit 1; } + +SECRET_PW="super-secret-password-987654321" +SECRET_TOTP_SEED="otpauth://totp/Example:user?secret=JBSWY3DPEHPK3PXP" + +# Mock `op` CLI that records calls and stdin without ever needing 1Password +mkdir -p "$TMP/bin" +cat >"$TMP/bin/op" <<'EOF' +#!/usr/bin/env bash +set -euo pipefail +LOG="$MOCK_OP_LOG" +printf '%s\n' "$*" >>"$LOG" + +case "${1:-} ${2:-}" in + "item get") + cat "$MOCK_OP_ITEM_JSON" + ;; + "item create") + cat >"$MOCK_OP_CREATED_STDIN" + printf '{"id":"new-item-id-42","title":"Example Login","vault":{"id":"v-agent","name":"Agent"}}\n' + ;; + "signout "*) + exit 0 + ;; + "signout") + exit 0 + ;; + *) + echo "unexpected op call: $*" >&2 + exit 1 + ;; +esac +EOF +chmod +x "$TMP/bin/op" + +cat >"$TMP/item.json" <"$TMP/op.log" +: >"$TMP/created.json" +out="$(PATH="$TMP/bin:$PATH" MOCK_OP_LOG="$TMP/op.log" MOCK_OP_ITEM_JSON="$TMP/item.json" MOCK_OP_CREATED_STDIN="$TMP/created.json" \ + bash "$HELPER" --vault Personal "Example Login" 2>&1)" +echo "$out" | grep -q "copied 'Example Login' (LOGIN) from 'Personal' -> 'Agent' (id: new-item-id-42)" || fail "unexpected copy output: $out" +if echo "$out" | grep -qF "$SECRET_PW"; then fail "secret password leaked to output"; fi +if echo "$out" | grep -qF "JBSWY3DPEHPK3PXP"; then fail "TOTP seed leaked to output"; fi +grep -q "^item get .*--reveal" "$TMP/op.log" || fail "op item get did not pass --reveal" +grep -q "^signout" "$TMP/op.log" || fail "op signout was not called" +jq -e 'has("id") | not' "$TMP/created.json" >/dev/null || fail ".id was not stripped" +jq -e 'has("vault") | not' "$TMP/created.json" >/dev/null || fail ".vault was not stripped" +jq -e 'has("version") | not' "$TMP/created.json" >/dev/null || fail ".version was not stripped" +jq -e '(.fields | map(has("totp") or has("reference") or has("password_details")) | any) | not' "$TMP/created.json" >/dev/null || fail "computed field keys not stripped" +jq -e --arg pw "$SECRET_PW" '.fields[] | select(.id=="password") | .value == $pw' "$TMP/created.json" >/dev/null || fail "password value not preserved in payload" +pass "copy strips metadata, preserves fields, prints no secrets, and signs out" + +# 2. Dry-run: prints non-secret metadata only, does not call `op item create`, still signs out. +: >"$TMP/op.log" +: >"$TMP/created.json" +out="$(PATH="$TMP/bin:$PATH" MOCK_OP_LOG="$TMP/op.log" MOCK_OP_ITEM_JSON="$TMP/item.json" MOCK_OP_CREATED_STDIN="$TMP/created.json" \ + bash "$HELPER" --dry-run --vault Personal "Example Login" 2>&1)" +echo "$out" | grep -q "dry-run" || fail "dry-run output missing: $out" +if echo "$out" | grep -qF "$SECRET_PW"; then fail "secret leaked in dry-run"; fi +if grep -q "^item create" "$TMP/op.log"; then fail "dry-run invoked op item create"; fi +grep -q "^signout" "$TMP/op.log" || fail "op signout not called on dry-run" +pass "dry-run prints metadata only and skips item create" + +# 3. Same-vault guard: refuses to duplicate when source vault == dest vault, still signs out. +: >"$TMP/op.log" +set +e +out="$(PATH="$TMP/bin:$PATH" MOCK_OP_LOG="$TMP/op.log" MOCK_OP_ITEM_JSON="$TMP/item.json" MOCK_OP_CREATED_STDIN="$TMP/created.json" \ + bash "$HELPER" --dest Personal "Example Login" 2>&1)" +rc=$? +set -e +[ "$rc" -ne 0 ] || fail "same-vault copy should have failed" +echo "$out" | grep -q "already in vault 'Personal'" || fail "missing same-vault error: $out" +grep -q "^signout" "$TMP/op.log" || fail "op signout not called on error exit" +pass "same-vault guard refuses duplication and signs out" + +# 4. File attachment warning +jq '.files = [{"id":"f1","name":"cert.pem"}]' "$TMP/item.json" >"$TMP/item-files.json" +: >"$TMP/op.log" +out="$(PATH="$TMP/bin:$PATH" MOCK_OP_LOG="$TMP/op.log" MOCK_OP_ITEM_JSON="$TMP/item-files.json" MOCK_OP_CREATED_STDIN="$TMP/created.json" \ + bash "$HELPER" --vault Personal "Example Login" 2>&1)" +echo "$out" | grep -q "1 file attachment(s)" || fail "missing file attachment warning: $out" +jq -e 'has("files") | not' "$TMP/created.json" >/dev/null || fail ".files was not stripped" +pass "file attachment warning emitted and .files stripped" diff --git a/users/dev/home.darwin.nix b/users/dev/home.darwin.nix index 343a106b..d4f731e2 100644 --- a/users/dev/home.darwin.nix +++ b/users/dev/home.darwin.nix @@ -245,6 +245,12 @@ lib.mkIf isDarwin { name = "screenshot-to-devbox"; text = builtins.readFile "${assetsPath}/scripts/screenshot-to-devbox.sh"; }) + pkgs._1password-cli + (pkgs.writeShellApplication { + name = "share-to-agent"; + runtimeInputs = [ pkgs._1password-cli pkgs.jq ]; + text = builtins.readFile "${assetsPath}/scripts/share-to-agent.sh"; + }) # What the `mosh` alias actually runs. Exists to force # --experimental-remote-ip=local. # @@ -494,12 +500,9 @@ lib.mkIf isDarwin { # rely on 9222 being up. It does not hold up logout or restart; launchd # does not respawn agents while the GUI session is ending. Its profile is # set to clear cookies on exit, so a quit is also how you sign it out of - # everything. SECURITY: 9222 is forwarded to devbox and cloudbox, and this - # profile's 1Password is linked to the desktop app (see - # linkOnePasswordNativeHost), so agents there can drive a browser whose - # vault is open whenever the app is, at any hour. That is the exposure the - # 9223 forward was removed for; for 9222 the human accepted it on - # 2026-11-23. Stop it for real with: + # everything. Chrome-Agent is kept unlinked from the 1Password desktop app + # (see linkOnePasswordNativeHost) so its extension runs standalone against + # a dedicated agent vault. Stop it for real with: # launchctl bootout "gui/$(id -u)/org.nix-community.home.agent-chrome" agent-chrome = { enable = true; @@ -1966,17 +1969,21 @@ EOF # 1Password's Touch ID unlock in a Chrome started with --user-data-dir. # The extension unlocks by asking the desktop app over native messaging, and # Chrome looks for the host manifest under /NativeMessagingHosts - # rather than the default profile's directory, so in the agent profiles the - # extension could not reach the app and fell back to the account password. - # Symlinked, not copied: 1Password rewrites the default-dir manifest when it - # starts. Chrome-gcloud is left alone on purpose (nobody browses in it). + # rather than the default profile's directory. Symlinked, not copied: 1Password + # rewrites the default-dir manifest when it starts. Chrome-gcloud is left alone + # (nobody browses in it), and Chrome-Agent is explicitly unlinked so its + # 1Password extension runs standalone against a dedicated agent vault. home.activation.linkOnePasswordNativeHost = lib.hm.dag.entryAfter [ "writeBoundary" ] '' appsup="$HOME/Library/Application Support" manifest="$appsup/Google/Chrome/NativeMessagingHosts/com.1password.1password.json" + agent_manifest="$appsup/Chrome-Agent/NativeMessagingHosts/com.1password.1password.json" + if [ -L "$agent_manifest" ] || [ -e "$agent_manifest" ]; then + run rm -f "$agent_manifest" + fi if [ ! -e "$manifest" ]; then echo "linkOnePasswordNativeHost: skipping ($manifest missing; is the 1Password app installed?)" else - for p in Chrome-Agent Chrome-VQA-Citadels; do + for p in Chrome-VQA-Citadels; do [ -d "$appsup/$p" ] || continue run mkdir -p "$appsup/$p/NativeMessagingHosts" run ln -sfn "$manifest" "$appsup/$p/NativeMessagingHosts/com.1password.1password.json" From 35b66206174230c7b21a8a358d662ea515393f21 Mon Sep 17 00:00:00 2001 From: Jonathan Mohrbacher Date: Tue, 1 Dec 2026 13:59:44 -0500 Subject: [PATCH 2/2] Use here-strings in test-share-to-agent.sh to satisfy pipefail-inversion guard Rewrite echo "$out" | grep -q ... to grep -q ... <<<"$out" so the pipefail-inversion check passes under set -o pipefail. X-Shepherd-Wake: johnnymo87/workstation#1058 --- scripts/test-share-to-agent.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/test-share-to-agent.sh b/scripts/test-share-to-agent.sh index d44f0118..e25ef8d5 100755 --- a/scripts/test-share-to-agent.sh +++ b/scripts/test-share-to-agent.sh @@ -70,9 +70,9 @@ EOF : >"$TMP/created.json" out="$(PATH="$TMP/bin:$PATH" MOCK_OP_LOG="$TMP/op.log" MOCK_OP_ITEM_JSON="$TMP/item.json" MOCK_OP_CREATED_STDIN="$TMP/created.json" \ bash "$HELPER" --vault Personal "Example Login" 2>&1)" -echo "$out" | grep -q "copied 'Example Login' (LOGIN) from 'Personal' -> 'Agent' (id: new-item-id-42)" || fail "unexpected copy output: $out" -if echo "$out" | grep -qF "$SECRET_PW"; then fail "secret password leaked to output"; fi -if echo "$out" | grep -qF "JBSWY3DPEHPK3PXP"; then fail "TOTP seed leaked to output"; fi +grep -q "copied 'Example Login' (LOGIN) from 'Personal' -> 'Agent' (id: new-item-id-42)" <<<"$out" || fail "unexpected copy output: $out" +if grep -qF "$SECRET_PW" <<<"$out"; then fail "secret password leaked to output"; fi +if grep -qF "JBSWY3DPEHPK3PXP" <<<"$out"; then fail "TOTP seed leaked to output"; fi grep -q "^item get .*--reveal" "$TMP/op.log" || fail "op item get did not pass --reveal" grep -q "^signout" "$TMP/op.log" || fail "op signout was not called" jq -e 'has("id") | not' "$TMP/created.json" >/dev/null || fail ".id was not stripped" @@ -87,8 +87,8 @@ pass "copy strips metadata, preserves fields, prints no secrets, and signs out" : >"$TMP/created.json" out="$(PATH="$TMP/bin:$PATH" MOCK_OP_LOG="$TMP/op.log" MOCK_OP_ITEM_JSON="$TMP/item.json" MOCK_OP_CREATED_STDIN="$TMP/created.json" \ bash "$HELPER" --dry-run --vault Personal "Example Login" 2>&1)" -echo "$out" | grep -q "dry-run" || fail "dry-run output missing: $out" -if echo "$out" | grep -qF "$SECRET_PW"; then fail "secret leaked in dry-run"; fi +grep -q "dry-run" <<<"$out" || fail "dry-run output missing: $out" +if grep -qF "$SECRET_PW" <<<"$out"; then fail "secret leaked in dry-run"; fi if grep -q "^item create" "$TMP/op.log"; then fail "dry-run invoked op item create"; fi grep -q "^signout" "$TMP/op.log" || fail "op signout not called on dry-run" pass "dry-run prints metadata only and skips item create" @@ -101,7 +101,7 @@ out="$(PATH="$TMP/bin:$PATH" MOCK_OP_LOG="$TMP/op.log" MOCK_OP_ITEM_JSON="$TMP/i rc=$? set -e [ "$rc" -ne 0 ] || fail "same-vault copy should have failed" -echo "$out" | grep -q "already in vault 'Personal'" || fail "missing same-vault error: $out" +grep -q "already in vault 'Personal'" <<<"$out" || fail "missing same-vault error: $out" grep -q "^signout" "$TMP/op.log" || fail "op signout not called on error exit" pass "same-vault guard refuses duplication and signs out" @@ -110,6 +110,6 @@ jq '.files = [{"id":"f1","name":"cert.pem"}]' "$TMP/item.json" >"$TMP/item-files : >"$TMP/op.log" out="$(PATH="$TMP/bin:$PATH" MOCK_OP_LOG="$TMP/op.log" MOCK_OP_ITEM_JSON="$TMP/item-files.json" MOCK_OP_CREATED_STDIN="$TMP/created.json" \ bash "$HELPER" --vault Personal "Example Login" 2>&1)" -echo "$out" | grep -q "1 file attachment(s)" || fail "missing file attachment warning: $out" +grep -q "1 file attachment(s)" <<<"$out" || fail "missing file attachment warning: $out" jq -e 'has("files") | not' "$TMP/created.json" >/dev/null || fail ".files was not stripped" pass "file attachment warning emitted and .files stripped"