Skip to content
Closed
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
15 changes: 12 additions & 3 deletions bin/omarchy-agent-usage-codex
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,19 @@ def fetch_codex_rpc():
rpc_request(proc, 1, "initialize", {"clientInfo": {"name": "omarchy-agent-usage", "version": "1"}}, timeout=8)
proc.stdin.write(json.dumps({"method": "initialized", "params": {}}) + "\n")
proc.stdin.flush()
account_msg = rpc_request(proc, 2, "account/read", timeout=4)
limits_msg = rpc_request(proc, 3, "account/rateLimits/read", timeout=4)

account = (account_msg.get("result") or {}).get("account") or {}
# rateLimits first: account/read is only a fallback plan label, and on some
# machines app-server never answers it while rateLimits always does. A
# shared try around both used to discard good limits whenever account/read
# timed out (issues #13266 / #13425).
limits_msg = rpc_request(proc, 2, "account/rateLimits/read", timeout=10)
account = {}
try:
account_msg = rpc_request(proc, 3, "account/read", timeout=4)
account = (account_msg.get("result") or {}).get("account") or {}
except Exception:
pass

limits = (limits_msg.get("result") or {}).get("rateLimits") or {}
plan = limits.get("planType") or account.get("planType") or account.get("type") or ""
result["tierLabel"] = str(plan) if plan else ""
Expand Down
9 changes: 7 additions & 2 deletions bin/omarchy-windows-vm
Original file line number Diff line number Diff line change
Expand Up @@ -580,14 +580,18 @@ prepare_caller_mounts() {
# Privacy is an explicit preflight step for both already-pinned sources, not
# a side effect halfway through the two-mount transaction. Old umask-022
# installs are hardened together before either Docker-facing anchor changes.
chmod 0700 -- "/proc/$BASHPID/fd/$storage_fd" "/proc/$BASHPID/fd/$shared_fd" || {
# Use a leading 0 in the mode so GNU chmod clears setuid/setgid: the Windows
# container leaves ~/Windows at 2777, and `chmod 0700` would keep setgid
# (2700), which then fails the exact-700 check below on every relaunch.
chmod 00700 -- "/proc/$BASHPID/fd/$storage_fd" "/proc/$BASHPID/fd/$shared_fd" || {
exec {storage_fd}<&-
exec {shared_fd}<&-
return 1
}
storage_mode=$(stat -Lc '%a' "/proc/$BASHPID/fd/$storage_fd" 2>/dev/null) || storage_mode=""
shared_mode=$(stat -Lc '%a' "/proc/$BASHPID/fd/$shared_fd" 2>/dev/null) || shared_mode=""
if [[ $storage_mode != 700 || $shared_mode != 700 ]]; then
echo "omarchy-windows-vm: VM data folders must be mode 700 (storage=$storage_mode shared=$shared_mode)" >&2
exec {storage_fd}<&-
exec {shared_fd}<&-
return 1
Expand Down Expand Up @@ -1015,7 +1019,8 @@ prepare_user_mount_sources() {
echo "omarchy-windows-vm: storage and shared must be different directories" >&2
return 1
}
chmod 0700 -- "$storage" "$shared"
# Leading 0 clears setuid/setgid; see prepare_caller_mounts.
chmod 00700 -- "$storage" "$shared"
}

storage_space_path() {
Expand Down
39 changes: 39 additions & 0 deletions test/shell.d/agent-usage-codex-scanner-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,42 @@ result=$(HOME="$INTERRUPTED_HOME" CODEX_HOME="$INTERRUPTED_HOME/.codex" XDG_CACH
[[ $(jq -r '.todayTotalTokens' <<<"$result") == "9" ]] ||
fail "Codex collector does not reuse a snapshot from an interrupted scan" "$result"
pass "Codex collector does not cache an interrupted opencode scan"

# account/read may never answer while rateLimits/read always does (issue #13266).
# Limits must still be returned instead of "Codex limits unavailable".
HANG_HOME=$(mktemp -d)
trap 'rm -rf "$TEST_HOME" "$PI_HOME" "$HANG_HOME"' EXIT
mkdir -p "$HANG_HOME/bin" "$HANG_HOME/.codex/sessions/$(date +%Y/%m/%d)"
cp "$session" "$HANG_HOME/.codex/sessions/$(date +%Y/%m/%d)/rollout.jsonl"
cat >"$HANG_HOME/bin/codex" <<'HANG'
#!/bin/bash
while read -r request; do
id=$(jq -r '.id // empty' <<<"$request")
method=$(jq -r '.method // empty' <<<"$request")
case "$method" in
initialize)
jq -cn --argjson id "$id" '{id: $id, result: {}}'
;;
account/read)
# Never answer — the collector must still surface rate limits.
sleep 30
;;
account/rateLimits/read)
jq -cn --argjson id "$id" \
'{id: $id, result: {rateLimits: {planType: "plus", primary: {usedPercent: 12, windowDurationMins: 10080, resetsAt: 1893456000}}}}'
;;
esac
done
HANG
chmod +x "$HANG_HOME/bin/codex"

result=$(HOME="$HANG_HOME" CODEX_HOME="$HANG_HOME/.codex" XDG_DATA_HOME="$HANG_HOME/.local/share" \
PATH="$HANG_HOME/bin:$PATH" "$ROOT/bin/omarchy-agent-usage-codex" --limits-only)

[[ $(jq -r '.usageStatusText // empty' <<<"$result") != "Codex limits unavailable" ]] ||
fail "Codex collector keeps limits when account/read hangs" "$result"
[[ $(jq -r '.limits|length' <<<"$result") == "1" ]] ||
fail "Codex collector returns rateLimits when account/read hangs" "$result"
[[ $(jq -r '.tierLabel' <<<"$result") == "plus" ]] ||
fail "Codex collector uses planType from rateLimits when account/read hangs" "$result"
pass "Codex collector keeps limits when account/read hangs"
9 changes: 9 additions & 0 deletions test/shell.d/windows-vm-compose-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ grep -q 'PROTECT: "Y"' "$COMPOSE" || fail "web console is not password protected
grep -q -- '- /:/' "$COMPOSE" && fail "compose contains host-root bind"
pass "writer emits fixed anchors bound to exact private source inodes"

# Container leaves setgid on ~/Windows; user prep must clear it on relaunch.
unmount_all
chmod 2777 "$HOME/Windows"
[[ $(stat -Lc '%a' "$HOME/Windows") == 2777 ]] || fail "could not seed setgid shared folder"
prepare_user_mount_sources || fail "user prep rejected a setgid shared folder"
[[ $(stat -Lc '%a' "$HOME/Windows") == 700 ]] || fail "user prep did not clear setgid from shared folder"
[[ $(stat -Lc '%a' "$HOME/.windows") == 700 ]] || fail "user prep left storage off 700"
pass "user mount prep clears setgid left by the Windows container"

# Input cannot widen a mount or compose field.
rm -f "$COMPOSE"
write 4G 2 64G 'x -v /:/h' p UTC 2>/dev/null && fail "malicious username accepted"
Expand Down
12 changes: 12 additions & 0 deletions test/shell.d/windows-vm-mount-boundary-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ if setpriv --reuid=1001 --regid=1001 --clear-groups cat "$EXPECTED_SHARED/shared
fi
pass "cross-filesystem symlink sources bind by identity and migrated 0700 leaves deny another account"

# The Windows container sets ~/Windows to 2777 (including setgid). GNU chmod
# with a 4-digit mode leaves an existing setgid bit alone, so a bare 0700
# would leave 2700 and the exact-700 check would reject every relaunch.
umount "$EXPECTED_SHARED"
umount "$EXPECTED_STORAGE"
chmod 2777 /home/shared-target
[[ $(command stat -Lc '%a' /home/shared-target) == 2777 ]] || fail "could not seed setgid shared folder"
with_vm_lock prepare_caller_mounts || fail "root could not clear setgid from the shared folder"
[[ $(command stat -Lc '%a' /home/shared-target) == 700 ]] || fail "setgid shared folder was not normalized to 700"
[[ $(command stat -Lc '%u:%a' "$EXPECTED_SHARED") == 1000:700 ]] || fail "shared anchor retained setgid after relaunch prep"
pass "prepare_caller_mounts clears setgid left by the Windows container"

# Existing production boundary components are never repaired in place when
# their ownership or write permissions are unsafe. Both the preparation path
# and the final pre-Docker guard must fail closed without disturbing the binds.
Expand Down
24 changes: 24 additions & 0 deletions test/shell.d/windows-vm-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,27 @@ rg -q 'tag = "-default-opacity"' "$windows_vm_rules" ||
rg -q 'opacity = "1 1"' "$windows_vm_rules" ||
fail "Windows VM stays fully opaque"
pass "Windows VM stays fully opaque"

# chmod 0700 leaves setgid on directories; the container sets ~/Windows to 2777,
# so relaunch prep must use 00700 to clear it before the exact-700 check.
rg -q 'chmod 00700 -- "/proc/\$BASHPID/fd/\$storage_fd" "/proc/\$BASHPID/fd/\$shared_fd"' "$windows_vm_command" ||
fail "caller-mount prep clears setgid when hardening VM folders"
rg -q 'chmod 00700 -- "\$storage" "\$shared"' "$windows_vm_command" ||
fail "user-mount prep clears setgid when hardening VM folders"
if rg -q 'chmod 0700 -- "/proc/\$BASHPID/fd/\$storage_fd"' "$windows_vm_command" ||
rg -q 'chmod 0700 -- "\$storage" "\$shared"' "$windows_vm_command"; then
fail "VM folder hardening still uses chmod 0700, which preserves setgid"
fi
pass "Windows VM folder hardening clears setgid on relaunch"

# Document the GNU chmod quirk the hardening sites rely on: a 4-digit mode
# leaves setgid on directories, while a leading 0 clears it.
setgid_dir=$(mktemp -d)
chmod 2777 "$setgid_dir"
[[ $(stat -c %a "$setgid_dir") == 2777 ]] || fail "could not seed setgid directory"
chmod 0700 -- "$setgid_dir"
[[ $(stat -c %a "$setgid_dir") == 2700 ]] || fail "chmod 0700 unexpectedly cleared setgid"
chmod 00700 -- "$setgid_dir"
[[ $(stat -c %a "$setgid_dir") == 700 ]] || fail "chmod 00700 did not clear setgid"
rmdir "$setgid_dir"
pass "GNU chmod 00700 clears directory setgid; 0700 does not"