-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·306 lines (277 loc) · 9.69 KB
/
install.sh
File metadata and controls
executable file
·306 lines (277 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env bash
#
# Install agent hooks for Cursor, GitHub Copilot, Claude Code, or Windsurf.
# Always creates a bundle (hook files). With --target-dir, installs that bundle into DIR and creates hooks.json from template if missing (never overwrites existing hooks.json).
# Run from this repo.
#
# Usage: ./install.sh --agent cursor|github-copilot|claude-code|windsurf [--target-dir DIR]
#
set -euo pipefail
CONFIG_NAME="install-client-config.json"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="${SCRIPT_DIR}"
if [[ ! -f "${REPO_ROOT}/${CONFIG_NAME}" ]]; then
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
fi
CONFIG_PATH="${REPO_ROOT}/${CONFIG_NAME}"
usage() {
echo "Usage: $0 --agent cursor|github-copilot|claude-code|windsurf [--target-dir DIR]"
echo ""
echo " --agent (required) Agent (cursor, github-copilot, claude-code, or windsurf)."
echo " --target-dir If set: install bundle into DIR (creates hooks.json from template if missing). If unset: create bundle in current directory only (no hooks.json)."
echo ""
exit 1
}
# ---- Config parsing ----
# Get the JSON object value for key (first occurrence), by brace counting.
get_json_block() {
local content="$1"
local key="$2"
local rest
rest="${content#*\"${key}\"*:}"
[[ "$rest" == "$content" ]] && return 1
rest="${rest#"${rest%%[![:space:]]*}"}"
[[ "${rest:0:1}" != "{" ]] && return 1
local depth=1 i=1
local len=${#rest}
while (( i < len && depth > 0 )); do
local c="${rest:$i:1}"
[[ "$c" == "{" ]] && (( depth++ ))
[[ "$c" == "}" ]] && (( depth-- ))
(( i++ ))
done
echo "${rest:0:$i}"
}
# Get first string value for key in a JSON fragment: "key": "value"
get_string_key() {
local block="$1"
local key="$2"
if [[ "$block" =~ \"$key\"[[:space:]]*:[[:space:]]*\"([^\"]*)\" ]]; then
echo "${BASH_REMATCH[1]}"
return 0
fi
return 1
}
# Get array of quoted strings for key: "key": ["a", "b"]
get_string_array() {
local block="$1"
local key="$2"
local line
line=$(echo "$block" | grep -oE "\"${key}\"[[:space:]]*:[[:space:]]*\[[^]]*\]" | head -1)
[[ -z "$line" ]] && return 1
local inner="${line#*\[}"
inner="${inner%\]}"
local result=()
while [[ "$inner" =~ \"([^\"]+)\" ]]; do
result+=( "${BASH_REMATCH[1]}" )
inner="${inner#*\"${BASH_REMATCH[1]}\"}"
inner="${inner#,}"
inner="${inner#"${inner%%[![:space:]]*}"}"
done
printf '%s\n' "${result[@]}"
}
# Get hook_events as lines "event\hookname" from block
get_hook_events() {
local block="$1"
local events_block
events_block=$(get_json_block "$block" "hook_events")
[[ -z "$events_block" ]] && return 0
while [[ "$events_block" =~ \"([^\"]+)\"[[:space:]]*:[[:space:]]*\[ ]]; do
local event="${BASH_REMATCH[1]}"
local rest="${events_block#*${BASH_REMATCH[0]}}"
local inner="${rest%%\]*}"
events_block="${rest#*\]}"
while [[ "$inner" =~ \"([^\"]+)\" ]]; do
echo "${event} ${BASH_REMATCH[1]}"
inner="${inner#*\"${BASH_REMATCH[1]}\"}"
inner="${inner#,}"
inner="${inner#"${inner%%[![:space:]]*}"}"
done
done
}
# Reject relative path if it could escape the base (path traversal).
# Call after reading install_dir and config_path from config.
is_unsafe_relative_path() {
local path="$1"
[[ "$path" == *"/../"* ]] && return 0
[[ "$path" == *"/.." ]] && return 0
[[ "$path" == "../"* ]] && return 0
[[ "$path" == ".." ]] && return 0
return 1
}
# Reject adapter or hook name that could be used for path traversal.
# Names must be a single segment (no slashes) and not . or ..
is_unsafe_segment() {
local name="$1"
[[ -z "$name" ]] && return 0
[[ "$name" == "." ]] && return 0
[[ "$name" == *"/"* ]] && return 0
is_unsafe_relative_path "$name" && return 0
return 1
}
# ---- Main ----
AGENT=""
TARGET_DIR=""
require_value() {
local opt="$1"
if [[ $# -lt 2 || -z "$2" || "$2" == -* ]]; then
echo "Error: $opt requires a value (e.g. for --agent: cursor, github-copilot, claude-code, or windsurf)" >&2
exit 1
fi
}
while [[ $# -gt 0 ]]; do
case "$1" in
--agent)
require_value "--agent" "${2:-}"
AGENT="$2"
shift 2
;;
--target-dir)
require_value "--target-dir" "${2:-}"
TARGET_DIR="$2"
shift 2
;;
-h|--help) usage ;;
*) echo "Unknown option: $1" >&2; usage ;;
esac
done
# Ensure an agent is specified
if [[ -z "$AGENT" ]]; then
echo "Error: --agent is required (cursor, github-copilot, claude-code, or windsurf)" >&2
usage
fi
# Ensure the config file exists
if [[ ! -f "$CONFIG_PATH" ]]; then
echo "Error: config not found: $CONFIG_PATH" >&2
exit 1
fi
# Get the agent block
CONFIG_CONTENT=$(cat "$CONFIG_PATH")
AGENT_BLOCK=$(get_json_block "$CONFIG_CONTENT" "$AGENT") || true
if [[ -z "$AGENT_BLOCK" ]]; then
echo "Error: could not find agent block for: $AGENT" >&2
exit 1
fi
# Get the project block used for the install directory and config path
SCOPE_BLOCK=$(get_json_block "$AGENT_BLOCK" "project") || true
if [[ -z "$SCOPE_BLOCK" ]]; then
echo "Error: could not find project block for: $AGENT" >&2
exit 1
fi
# Get the install directory and config path relative to the project block
INSTALL_DIR_REL=$(get_string_key "$SCOPE_BLOCK" "install_dir") || true
CONFIG_PATH_REL=$(get_string_key "$SCOPE_BLOCK" "config_path") || true
if [[ -z "$INSTALL_DIR_REL" || -z "$CONFIG_PATH_REL" ]]; then
echo "Error: missing install_dir or config_path for agent=$AGENT" >&2
exit 1
fi
# Reject unsafe relative paths
if is_unsafe_relative_path "$INSTALL_DIR_REL" || is_unsafe_relative_path "$CONFIG_PATH_REL"; then
echo "Error: install_dir or config_path may not contain '..' (path traversal)." >&2
exit 1
fi
if [[ "$INSTALL_DIR_REL" == *$'\n'* || "$CONFIG_PATH_REL" == *$'\n'* ]]; then
echo "Error: install_dir or config_path may not contain newlines." >&2
exit 1
fi
# Resolve install directory (and config path only when --target-dir is set)
BUNDLE_NAME="${INSTALL_DIR_REL##*/}"
if [[ -n "${TARGET_DIR:-}" ]]; then
if [[ ! -d "$TARGET_DIR" ]]; then
echo "Error: target directory does not exist: $TARGET_DIR" >&2
exit 1
fi
BASE="$(cd "$TARGET_DIR" && pwd)"
INSTALL_DIR="${BASE}/${INSTALL_DIR_REL}"
CONFIG_FILE="${BASE}/${CONFIG_PATH_REL}"
echo "Target directory: $BASE"
else
INSTALL_DIR="$(pwd)/${BUNDLE_NAME}"
CONFIG_FILE=""
fi
echo "Agent: $AGENT"
echo "Install dir: $INSTALL_DIR"
if [[ -n "$CONFIG_FILE" ]]; then
echo "Config path: $CONFIG_FILE (created if missing)"
fi
echo ""
# Overwrite prompt: same for bundle-in-cwd or install-into-target (never overwrite existing hooks.json)
if [[ -d "$INSTALL_DIR" ]] && [[ -t 0 ]]; then
echo "1Password agent hooks already installed at: $INSTALL_DIR"
echo "This will overwrite with a fresh install. Any changes you made may be lost."
read -r -p "Continue? (y/n) " response
case "$response" in
[yY]|[yY][eE][sS]) ;;
*) echo "Aborted."; exit 0 ;;
esac
rm -rf "$INSTALL_DIR"
fi
mkdir -p "${INSTALL_DIR}/bin" "${INSTALL_DIR}/lib" "${INSTALL_DIR}/adapters" "${INSTALL_DIR}/hooks"
# Copy lib and bin
cp "${REPO_ROOT}/bin/run-hook.sh" "${INSTALL_DIR}/bin/run-hook.sh"
for f in "${REPO_ROOT}/lib/"*.sh; do
[[ -f "$f" ]] && cp "$f" "${INSTALL_DIR}/lib/"
done
# Copy adapters for this agent
while IFS= read -r adapter; do
[[ -z "$adapter" ]] && continue
if is_unsafe_segment "$adapter"; then
echo "Error: invalid adapter name (path traversal). skipping." >&2
exit 1
fi
src="${REPO_ROOT}/adapters/${adapter}"
if [[ -f "$src" ]]; then
cp "$src" "${INSTALL_DIR}/adapters/"
else
echo "Warning: adapter not found: $src"
fi
done < <(get_string_array "$AGENT_BLOCK" "adapters")
# Copy only hooks referenced in hook_events
while IFS=$'\t' read -r event hook_name; do
[[ -z "$hook_name" ]] && continue
if is_unsafe_segment "$hook_name"; then
echo "Error: invalid hook name (path traversal). skipping." >&2
exit 1
fi
hook_dir="${REPO_ROOT}/hooks/${hook_name}"
if [[ -d "$hook_dir" && -f "${hook_dir}/hook.sh" ]]; then
mkdir -p "${INSTALL_DIR}/hooks/${hook_name}"
cp -r "${hook_dir}/"* "${INSTALL_DIR}/hooks/${hook_name}/"
else
echo "Warning: hook not found: $hook_dir (or hook.sh missing)"
fi
done < <(get_hook_events "$AGENT_BLOCK")
# Create hooks.json only when --target-dir was set: from template if missing and never overwrite existing
if [[ -n "$CONFIG_FILE" ]]; then
if [[ ! -f "$CONFIG_FILE" ]]; then
mkdir -p "$(dirname "$CONFIG_FILE")"
template="${REPO_ROOT}/${CONFIG_PATH_REL}"
# Template is the repo file at the same path (.cursor/hooks.json).
if [[ -f "$template" ]]; then
cp "$template" "$CONFIG_FILE"
# Replace placeholder "bin/run-hook.sh" in template with install-relative path (cursor-1password-hooks-bundle/bin/run-hook.sh).
SCRIPT_PATH_REL="${INSTALL_DIR_REL}/bin/run-hook.sh"
SCRIPT_PATH_REL_SED="${SCRIPT_PATH_REL//\\/\\\\}"
SCRIPT_PATH_REL_SED="${SCRIPT_PATH_REL_SED//&/\\&}"
# Replace "bin/run-hook.sh" in the copied config with the install path so the agent runs the correct script.
if sed --version 2>/dev/null | grep -q GNU; then
sed -i "s|bin/run-hook\.sh|${SCRIPT_PATH_REL_SED}|g" "$CONFIG_FILE"
else
sed -i.bak "s|bin/run-hook\.sh|${SCRIPT_PATH_REL_SED}|g" "$CONFIG_FILE" && rm -f "${CONFIG_FILE}.bak"
fi
echo "Created $CONFIG_FILE with default hook entries."
else
echo "Warning: no template at $template; skipping config creation."
fi
else
echo "" >&2
echo "WARNING: Config already exists at $CONFIG_FILE; update it to add or change hook entries." >&2
echo "" >&2
fi
fi
if [[ -n "$CONFIG_FILE" ]]; then
echo "Done. Hook(s) installed"
else
echo "Bundle created at: $INSTALL_DIR"
echo "Add hooks.json at your config path and set command to: ${INSTALL_DIR}/bin/run-hook.sh <hook-name>"
fi