Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·394 lines (358 loc) · 16.9 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·394 lines (358 loc) · 16.9 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env sh
#
# Installs the newest bravebot release for this platform, or the one named by the version given.
#
# curl -fsSL https://raw.githubusercontent.com/brave/bravebot/main/install.sh | sh
# curl -fsSL https://raw.githubusercontent.com/brave/bravebot/main/install.sh | sh -s 1.2.3
# curl -fsSL https://raw.githubusercontent.com/brave/bravebot/main/install.sh | INSTALL_DIR="$HOME/.local/bin" sh
#
# The checksum check is not optional: without it a network-fetched executable would run on the
# strength of TLS alone, and a substituted release asset would be indistinguishable from a good
# one. Running this again is also how an install made this way is updated, so it writes down where
# the binary went: that is what lets bravebot name the command that updates this copy, and what
# puts a later run in the same place rather than beside it.
#
# Nor is the signature check. The checksum is published beside the binary, so whoever can replace
# one can replace both. On Linux the checksum carries a signature that has to verify against the key
# embedded below, and a machine without gpg is refused before anything is downloaded, unless
# BRAVEBOT_ALLOW_MISSING_GPG=1 is set, which installs on the checksum alone and says so. On macOS
# the binary has to be signed by Brave's Developer ID team and pass Gatekeeper's assessment.
set -eu
REPO="brave/bravebot"
API_URL="https://api.github.com/repos/${REPO}/releases/latest"
# The key a Linux checksum has to be signed by. This is the public half, embedded so it has no
# external dependency. The fingerprint ensures only this specific key is trusted.
SIGNING_KEY_FINGERPRINT="13F28F0405C49B0B232DBA1BC1E827646A2DE416"
# The Developer ID team that signs each macOS binary. codesign alone accepts a valid signature from
# any team Apple issued a certificate to, so a signature from another team is a refusal.
APPLE_TEAM_ID="KL8N8XSYF4"
BIN_NAME="bravebot"
DEFAULT_INSTALL_DIR="/usr/local/bin"
RELEASE_PUBKEY="$(cat <<'PUBKEY'
-----BEGIN PGP PUBLIC KEY BLOCK-----
mQINBGqsIu8BEACplre+QZwgCtdzPGBFhzPQFpxifwb5qWOnVcZ4yWmlCUpBYx8D
45miyE6T6jKl7pzqHKtvQHfOAsb0NoRmKSELSCfyGHCXkU9Bpd0KSae9HAJKLlD6
HhnFnG1gyQK78kac3vWhqIjKYg5RKP0fWMtPhkDG7+YishzbbJ7E37gyvQW3JhnB
oXGx1C1XlKh1K1sr2N5XDCIDsH0/jP8nzsX5d9+1FkrgSHoXmmZyUPJ6mKADl+D1
JgqZe0X135qva0+dBYa/bbB9nqWBR+aOYYnuVs1UPFBb9fFAwK3o7t83S4gobf3d
joGjoFk9exVKwnUtXW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fUquzPc9F0MmjM5+Y8jEwJpDidPdmGGRVdn
EVOksqVLyJDmuFIbChfUyMblgDbnLjqP6hOiOvWqyfsirr1MOXaZ6igAeokE0c3y
s9HGBsRJA682nL1rZQ+OcvQigiVm6Eo/L1t5KM/Luz4+65lRJVy2i9YliDHimP6U
SEWQlP6FAOZEwtoP0KVOy5cJcfUTY1hiOvtiqGyq+hi3PRk9JTunFjue/Q+17epr
HNzPcU6aUBlTlXbHvSS1MSD2AQ1aKX3mYpkde0A0Aaw3/J82Zpz3
=FyJl
-----END PGP PUBLIC KEY BLOCK-----
PUBKEY
)"
# BRAVEBOT_HOME names the state directory itself, as it does for the program, and answers before
# HOME. A value that is empty or not an absolute path names nothing and is passed over.
# An unset or empty HOME names no profile directory, and then there is no state directory at all:
# nothing is read from one and nothing written to one (docs/specs/state-directory.md#STATE-2).
# Joining the name onto nothing makes `/.bravebot`, a location nobody stated, and a record under it
# would decide where this release lands.
STATE_DIR=""
INSTALLED_BY=""
case "${BRAVEBOT_HOME:-}" in
/*) STATE_DIR="${BRAVEBOT_HOME}" ;;
*)
if [ -n "${HOME:-}" ]; then
STATE_DIR="${HOME}/.bravebot"
fi
;;
esac
if [ -n "$STATE_DIR" ]; then
INSTALLED_BY="${STATE_DIR}/installed-by"
fi
fail() {
echo "error: $1" >&2
exit 1
}
need_cmd() {
command -v "$1" >/dev/null 2>&1 || fail "required command not found: $1"
}
# Three decimal numbers joined by dots, and nothing else. The version becomes part of a URL, so it is
# held to this shape rather than to "does not look dangerous": a dot-dot, a slash or a host in it
# is refused here, before any request, and what is left can only name a tag under REPO.
is_version() {
case "$1" in
"" | *[!0-9.]* | .* | *. | *..*) return 1 ;;
esac
# With no empty field possible, two dots is exactly three numbers.
case "$1" in
*.*.*) ;;
*) return 1 ;;
esac
rest="${1#*.}"
case "${rest#*.}" in
*.*) return 1 ;;
esac
return 0
}
# The digest and nothing else: sixty-four hex digits, no filename beside them. A checksum file of
# any other shape is a release published wrong, and installing anyway would make the one signal
# that distinguishes a bad download from a good one meaningless.
is_sha256() {
case "${#1}" in
64) ;;
*) return 1 ;;
esac
case "$1" in
*[!0-9a-fA-F]*) return 1 ;;
esac
return 0
}
# Succeeds only when the signature verifies and was made by the key with the given fingerprint.
# A good signature from any other key in the imported file is a refusal, which is what the
# fingerprint is for. The keyring is made for this call and removed after it, so the person's own
# is neither read nor added to.
verify_checksum_signature() {
asc_path="$1"
sha_path="$2"
pubkey_path="$3"
fingerprint="$4"
gpg_home="$(mktemp -d)" || return 1
# The last field of VALIDSIG is the primary key's fingerprint, whichever subkey signed.
if (
GNUPGHOME="$gpg_home"
export GNUPGHOME
gpg --batch --quiet --import "$pubkey_path" 2>/dev/null || exit 1
status="$(gpg --batch --status-fd 1 --verify "$asc_path" "$sha_path" 2>/dev/null)" || exit 1
printf '%s\n' "$status" | grep -q "^\[GNUPG:\] VALIDSIG .* ${fingerprint}\$"
); then
rc=0
else
rc=1
fi
rm -rf "$gpg_home"
return "$rc"
}
# The requirement holds the signature to a certificate Apple issued to APPLE_TEAM_ID, and spctl
# is the Gatekeeper assessment a double-click would get, which is where notarization is checked.
# A shell started from a terminal is never assessed by Gatekeeper, so this asks for it. `-t install`
# because the asset is a bare executable: `-t execute` rejects anything that is not an app bundle,
# however it is signed.
verify_code_signature() {
codesign --verify --deep --strict \
-R "=anchor apple generic and certificate leaf[subject.OU] = \"${APPLE_TEAM_ID}\"" "$1" &&
spctl -a -t install "$1"
}
# Registers the handler for bravebot:// links on a Linux desktop (LINK-6): a launcher entry that runs
# the installed binary in a terminal with the link as its one argument, and the scheme pointed at it.
# Nothing is registered where BRAVEBOT_NO_DEEP_LINKS is set, where there is no place to put the entry,
# or where the binary's path holds a character a launcher's Exec line would read as something else.
# A failure to register is not a failure to install, so every step here is allowed to fail quietly.
register_deep_link() {
dest="$1"
[ -z "${BRAVEBOT_NO_DEEP_LINKS:-}" ] || return 0
case "$dest" in
/* ) ;;
*) return 0 ;;
esac
case "$dest" in
*'"'* | *'%'* | *'`'* | *'$'* | *\\* | *'
'*) return 0 ;;
esac
applications="${XDG_DATA_HOME:-${HOME:+$HOME/.local/share}}"
[ -n "$applications" ] || return 0
case "$applications" in
/*) ;;
*) return 0 ;;
esac
applications="${applications}/applications"
mkdir -p "$applications" 2>/dev/null || return 0
entry="${applications}/bravebot-handler.desktop"
rm -f "$entry" 2>/dev/null || true
printf '%s\n' \
'[Desktop Entry]' \
'Type=Application' \
'Name=bravebot link handler' \
"Exec=\"${dest}\" --handle-uri %u" \
'Terminal=true' \
'NoDisplay=true' \
'MimeType=x-scheme-handler/bravebot;' > "$entry" 2>/dev/null || return 0
if command -v xdg-mime >/dev/null 2>&1; then
xdg-mime default bravebot-handler.desktop x-scheme-handler/bravebot >/dev/null 2>&1 || true
fi
return 0
}
main() {
[ "$#" -le 1 ] || fail "usage: install.sh [version]"
# Checked first, so an argument that is refused costs no request and writes nothing.
TAG=""
if [ "$#" -eq 1 ]; then
requested="${1#v}"
is_version "$requested" || fail "not a version: expected three numbers such as 1.2.3 (a leading v is allowed)"
TAG="v${requested}"
fi
need_cmd curl
need_cmd mktemp
need_cmd chmod
need_cmd mv
need_cmd rm
need_cmd uname
case "$(uname -s)" in
Darwin) OS_KEY="darwin" ;;
Linux) OS_KEY="linux" ;;
*) fail "unsupported OS: $(uname -s). Windows installs with install.ps1 in PowerShell, or with npm: npm install -g @brave/bravebot" ;;
esac
case "$(uname -m)" in
arm64 | aarch64) ARCH_KEY="arm64" ;;
x86_64 | amd64) ARCH_KEY="amd64" ;;
*) fail "unsupported architecture: $(uname -m)" ;;
esac
# Under Rosetta a translated shell reports x86_64 on an arm64 machine. The x86_64 binary would
# work and would run translated, so take the native one.
if [ "$OS_KEY" = "darwin" ] && [ "$ARCH_KEY" = "amd64" ]; then
translated="$(sysctl -in sysctl.proc_translated 2>/dev/null || true)"
native_arm="$(sysctl -in hw.optional.arm64 2>/dev/null || true)"
if [ "$translated" = "1" ] && [ "$native_arm" = "1" ]; then
echo "Detected Rosetta translation; installing the native arm64 binary."
ARCH_KEY="arm64"
fi
fi
ASSET_NAME="${BIN_NAME}-${OS_KEY}-${ARCH_KEY}"
# Asked before anything is downloaded, so a Mac that cannot check a signature is told so rather
# than handed a binary nobody vouched for.
if [ "$OS_KEY" = "darwin" ]; then
need_cmd codesign
need_cmd spctl
fi
# A Linux machine without gpg is refused here for the same reason. BRAVEBOT_ALLOW_MISSING_GPG=1 is
# how a person who cannot install gpg chooses to install on the checksum alone; only exactly 1
# counts, since it turns a check off. It is read only when gpg is missing, so with gpg here a
# signature that is missing or wrong is still refused. It names no URL, and the only request it
# changes is the one for the signature, which is then not made.
SKIP_SIGNATURE=""
if [ "$OS_KEY" = "linux" ] && ! command -v gpg >/dev/null 2>&1; then
[ "${BRAVEBOT_ALLOW_MISSING_GPG:-}" = "1" ] ||
fail "gpg not found, so the signature on ${ASSET_NAME}.sha256 cannot be checked; refusing to install. Install GnuPG (on Debian or Ubuntu: apt install gpg) and run this again. As a last resort, run this to install on the checksum alone, which does not show who published the binary: curl -fsSL https://raw.githubusercontent.com/brave/bravebot/main/install.sh | BRAVEBOT_ALLOW_MISSING_GPG=1 sh"
SKIP_SIGNATURE="1"
fi
# Where the last install put it, so running this again updates that copy instead of leaving a
# second one somewhere else on the PATH. INSTALL_DIR wins, for a person who is moving it.
if [ -z "${INSTALL_DIR:-}" ] && [ -n "$INSTALLED_BY" ] && [ -r "$INSTALLED_BY" ]; then
recorded="$(head -n 1 "$INSTALLED_BY" 2>/dev/null || true)"
if [ -n "$recorded" ]; then
INSTALL_DIR="$(dirname "$recorded")"
fi
fi
INSTALL_DIR="${INSTALL_DIR:-$DEFAULT_INSTALL_DIR}"
if [ -z "$TAG" ]; then
TAG="$(curl -fsSL "$API_URL" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)"
[ -n "$TAG" ] || fail "unable to resolve the latest release tag from $API_URL"
fi
BASE_URL="https://github.com/${REPO}/releases/download/${TAG}"
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT
BIN_PATH="${TMP_DIR}/${ASSET_NAME}"
SHA_PATH="${TMP_DIR}/${ASSET_NAME}.sha256"
echo "Downloading ${ASSET_NAME} ${TAG}..."
curl -fsSL "${BASE_URL}/${ASSET_NAME}" -o "$BIN_PATH" ||
fail "unable to download ${ASSET_NAME} for ${TAG}; check that this release exists for your platform"
curl -fsSL "${BASE_URL}/${ASSET_NAME}.sha256" -o "$SHA_PATH"
EXPECTED="$(tr -d '[:space:]' < "$SHA_PATH")"
is_sha256 "$EXPECTED" || fail "malformed checksum for ${ASSET_NAME}"
if command -v shasum >/dev/null 2>&1; then
ACTUAL="$(shasum -a 256 "$BIN_PATH" | cut -d ' ' -f 1)"
elif command -v sha256sum >/dev/null 2>&1; then
ACTUAL="$(sha256sum "$BIN_PATH" | cut -d ' ' -f 1)"
else
fail "need shasum or sha256sum to verify the download"
fi
# Lowercased both sides, since the comparison is of two digests and not of two spellings.
EXPECTED="$(printf '%s' "$EXPECTED" | tr 'A-F' 'a-f')"
ACTUAL="$(printf '%s' "$ACTUAL" | tr 'A-F' 'a-f')"
if [ "$ACTUAL" != "$EXPECTED" ]; then
echo "error: checksum mismatch for ${ASSET_NAME}" >&2
echo "expected: $EXPECTED" >&2
echo "actual: $ACTUAL" >&2
exit 1
fi
# Linux ships no code signature, unlike Darwin (notarized) and Windows (Authenticode), so its
# checksum carries a detached GPG signature instead. With gpg here, a signature that is missing
# is refused like one that is wrong, since deleting it is what somebody replacing the release
# would do. The check is skipped only where the override was accepted above, and the warning says
# so. gpg is not looked for a second time, so a gpg removed since then makes the check fail.
if [ "$OS_KEY" = "linux" ]; then
if [ -z "$SKIP_SIGNATURE" ]; then
ASC_PATH="${TMP_DIR}/${ASSET_NAME}.sha256.asc"
PUBKEY_PATH="${TMP_DIR}/bravebot-release.asc"
curl -fsSL "${BASE_URL}/${ASSET_NAME}.sha256.asc" -o "$ASC_PATH" ||
fail "unable to download the checksum signature for ${ASSET_NAME}"
printf '%s\n' "$RELEASE_PUBKEY" > "$PUBKEY_PATH"
verify_checksum_signature "$ASC_PATH" "$SHA_PATH" "$PUBKEY_PATH" "$SIGNING_KEY_FINGERPRINT" ||
fail "signature verification failed for ${ASSET_NAME}.sha256; refusing to install"
else
echo "warning: gpg not found and BRAVEBOT_ALLOW_MISSING_GPG=1 is set, so the signature on ${ASSET_NAME}.sha256 was not checked; the install relies on the checksum alone, which does not show who published the binary." >&2
fi
fi
if [ "$OS_KEY" = "darwin" ]; then
verify_code_signature "$BIN_PATH" ||
fail "${ASSET_NAME} is not signed by Brave Software (team ${APPLE_TEAM_ID}), or Gatekeeper rejected it (checking notarization may need the network); refusing to install"
fi
chmod +x "$BIN_PATH"
DEST_PATH="${INSTALL_DIR}/${BIN_NAME}"
if [ ! -d "$INSTALL_DIR" ]; then
mkdir -p "$INSTALL_DIR" 2>/dev/null || {
need_cmd sudo
sudo mkdir -p "$INSTALL_DIR"
}
fi
if [ -w "$INSTALL_DIR" ]; then
mv "$BIN_PATH" "$DEST_PATH"
else
need_cmd sudo
sudo mv "$BIN_PATH" "$DEST_PATH"
fi
# What bravebot reads to know it can offer the command that updates this copy. A machine with no
# HOME gets the binary and no notice about later releases, which is the same as no record at all.
#
# Created reachable only by this user, and narrowed where it is already there, for the reason the
# program narrows it: this directory holds the prompt history, and at the umask that is readable
# by every local account. Both the directory and the record are created with the mode they keep
# rather than chmod'ed once they exist, since the other order leaves them open for the moment in
# between; the chmod is what narrows a directory an earlier install left, and the record is
# removed and written again rather than written over. A link is stepped over rather than
# followed, as the program steps over one: chmod without -h resolves it on both platforms this
# supports, so a linked directory would have an install setting the mode of wherever the link
# leads, which is outside anything this was given.
if [ -n "$STATE_DIR" ] && (umask 077 && mkdir -p "$STATE_DIR") 2>/dev/null; then
if [ ! -L "$STATE_DIR" ]; then
chmod 700 "$STATE_DIR" 2>/dev/null || true
fi
rm -f "$INSTALLED_BY" 2>/dev/null || true
(umask 077 && printf '%s\n' "$DEST_PATH" > "$INSTALLED_BY") 2>/dev/null || true
fi
if [ "$OS_KEY" = "linux" ]; then
register_deep_link "$DEST_PATH"
fi
echo "Installed ${BIN_NAME} ${TAG} to ${DEST_PATH}"
ON_PATH="$(command -v "$BIN_NAME" 2>/dev/null || true)"
if [ -z "$ON_PATH" ]; then
echo "note: ${INSTALL_DIR} is not on your PATH; add it to run ${BIN_NAME} by name."
elif [ "$ON_PATH" != "$DEST_PATH" ]; then
echo "note: ${ON_PATH} comes first on your PATH, so that is what \`${BIN_NAME}\` still runs."
else
echo "Run: ${BIN_NAME} --help"
fi
}
# Sourced with BRAVEBOT_INSTALL_SH_TEST=1, this file installs nothing, so a test can call main itself.
if [ "${BRAVEBOT_INSTALL_SH_TEST:-0}" != "1" ]; then
main "$@"
fi