Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
163 changes: 163 additions & 0 deletions assets/scripts/share-to-agent.sh
Original file line number Diff line number Diff line change
@@ -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 <source-vault>] [--dest <dest-vault>] [--account <account>] [--dry-run] <item>

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:-<none>}"
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 // "<unknown>"' "$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)"
115 changes: 115 additions & 0 deletions scripts/test-share-to-agent.sh
Original file line number Diff line number Diff line change
@@ -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" <<EOF
{
"id": "orig-id-1",
"title": "Example Login",
"version": 7,
"vault": { "id": "v-personal", "name": "Personal" },
"category": "LOGIN",
"last_edited_by": "user-1",
"created_at": "2026-01-01T00:00:00Z",
"updated_at": "2026-06-01T00:00:00Z",
"additional_information": "alice@example.com",
"urls": [{ "primary": true, "href": "https://example.com/login" }],
"fields": [
{ "id": "username", "type": "STRING", "purpose": "USERNAME", "label": "username", "value": "alice@example.com", "reference": "op://Personal/Example/username" },
{ "id": "password", "type": "CONCEALED", "purpose": "PASSWORD", "label": "password", "value": "$SECRET_PW", "password_details": { "strength": "FANTASTIC" }, "reference": "op://Personal/Example/password" },
{ "id": "totp", "type": "OTP", "label": "one-time password", "value": "$SECRET_TOTP_SEED", "totp": "123456" }
]
}
EOF

# 1. Normal copy: strips server/computed fields, preserves secrets in stdin to `op item create`,
# never prints secrets to stdout/stderr, and signs out on exit.
: >"$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)"
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"
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)"
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"

# 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"
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"

# 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)"
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"
29 changes: 18 additions & 11 deletions users/dev/home.darwin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 <user-data-dir>/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"
Expand Down
Loading