thayen

thayen / agent-apt

Last active 1 month ago

Like 0

Small wrapper script for giving LLMs access to installing/removing apt packages while reducing their ability to bypass these restrictions

thayen revised this gist 1 month ago · 5bceb0a

No changes

thayen revised this gist 1 month ago · 779897a

1 file changed, 88 insertions

agent-apt (file created)
@@ -0,0 +1,88 @@
1 + #!/usr/bin/bash
2 + set -euo pipefail
3 +
4 + export PATH=/usr/sbin:/usr/bin:/sbin:/bin
5 + export DEBIAN_FRONTEND=noninteractive
6 + export DEBIAN_PRIORITY=critical
7 +
8 + APT=/usr/bin/apt-get
9 +
10 + if (( EUID != 0 )); then
11 + echo "Must be run as root (via sudo)." >&2
12 + exit 1
13 + fi
14 +
15 + if (( $# < 1 )); then
16 + echo "Usage: agent-apt {update|install|remove} [packages...]" >&2
17 + exit 2
18 + fi
19 +
20 + action=$1
21 + shift
22 +
23 + validate_packages() {
24 + if (( $# == 0 )); then
25 + echo "At least one package is required." >&2
26 + exit 2
27 + fi
28 +
29 + if (( $# > 30 )); then
30 + echo "Maximum 30 packages per operation." >&2
31 + exit 2
32 + fi
33 +
34 + for pkg in "$@"; do
35 + # Only plain Debian package names.
36 + # No paths, versions, wildcards, options, or APT patterns.
37 + # install/remove also set APT::Cmd::Pattern-Only to prevent implicit
38 + # regex fallback for names containing dots or plus signs. Explicit
39 + # regex anchors (^ and $) are rejected by this validator.
40 + if [[ ! "$pkg" =~ ^[a-z0-9][a-z0-9.+-]*$ ]]; then
41 + echo "Invalid package name: $pkg" >&2
42 + exit 2
43 + fi
44 + done
45 + }
46 +
47 + case "$action" in
48 + update)
49 + if (( $# != 0 )); then
50 + echo "update takes no arguments." >&2
51 + exit 2
52 + fi
53 + exec "$APT" update
54 + ;;
55 +
56 + install)
57 + validate_packages "$@"
58 + exec "$APT" \
59 + -o APT::Cmd::Pattern-Only=true \
60 + --assume-yes \
61 + --no-install-recommends \
62 + --no-remove \
63 + install -- "$@"
64 + ;;
65 +
66 + remove)
67 + validate_packages "$@"
68 +
69 + # APT treats trailing + and - as operation modifiers.
70 + # Refuse ambiguous removal operands.
71 + for pkg in "$@"; do
72 + if [[ "$pkg" == *+ || "$pkg" == *- ]]; then
73 + echo "Ambiguous removal operand: $pkg" >&2
74 + exit 2
75 + fi
76 + done
77 +
78 + exec "$APT" \
79 + -o APT::Cmd::Pattern-Only=true \
80 + --assume-yes \
81 + remove -- "$@"
82 + ;;
83 +
84 + *)
85 + echo "Unknown operation: $action" >&2
86 + exit 2
87 + ;;
88 + esac