Files
coredesktop/report
T
davidandClaude Opus 5.5 143473e798
shellcheck / shellcheck (push) Failing after 19s
Test the report receiver and redaction against attacks
server/report-receiver/test_security.py sends the receiver what anyone
on the internet could, and the Redaction tests in tests/report.bats feed
report logs full of secrets. Each test was checked to fail before its fix.

The receiver now refuses a negative Content-Length (read without limit),
parts nested thousands deep (a RecursionError dropped the connection)
and more than 20 parts (thousands took seconds of CPU to parse). It
counts an IPv6 address's /64 for the rate limit, since a home has a
whole /64 to send from. Report fields go into the Gitea issue as inline
code, and titles keep only the characters of step names and versions,
so a report can't @mention the team, link issues, or show a tracking
image. It refuses reports while its disk has less than 2 GB free.

report now also takes out passwords in URLs, Bearer and Basic tokens,
password= and token: values, API keys by their prefixes, Nextcloud app
passwords, private keys, IPv6 and MAC addresses, the Cores' domains and
the user's name. It reads the Core settings rather than sourcing them,
and escapes extended regex characters in names, which it didn't before.

The plan for the checks still to do, in the smoketest VM (the agent
sandbox, network exposure, what root runs, the install path), is a
Claude Code skill: /coredesktop-security-checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:27:17 -07:00

248 lines
10 KiB
Bash
Executable File

#!/bin/bash
#
# Purpose : Send an install or update log to the CoreDesktop team, so they can
# work out what went wrong and fix it
#
# report Send the newest log, after showing what will be sent
# report FILE Send FILE
# report --view [FILE] Show what would be sent, and send nothing
# report --offer FILE [STEP...]
# What update and install.sh run when they fail: ask on
# the terminal whether to send FILE. STEP names what failed.
#
# Nothing is sent unless the user says yes. Before sending, the log is cleaned
# of terminal codes, and personal details and secrets are replaced: the home
# folder, user name, host name, email, IP and MAC addresses, Tailscale names,
# the Cores' domains and the user's name, passwords, tokens, API keys and
# private keys (see redact).
#
# What's sent (the receiver on federated.sh implements the other side): an
# HTTPS POST of multipart/form-data to $COREDESKTOP_REPORT_URL, with fields
# kind install or update, from the log's name
# version CoreDesktop's version, e.g. 2026.11.08 (a82bbfc)
# failed what failed, e.g. "71-siblings.sh, 46-neovim.sh" (may be empty)
# kernel the running kernel, from uname -r
# siblings the installed sibling projects, one "name version" per line
# log the cleaned, redacted log, gzipped (log.gz, application/gzip)
# A 2xx response's body is the report's ID, on one line: up to 40 letters,
# digits and dashes. Any other response's
# body is a message for the user.
COREDESKTOP="$HOME/.local/share/coredesktop"
LOGS="$HOME/.local/state/coredesktop/logs"
REPORT_URL="${COREDESKTOP_REPORT_URL:-https://federated.sh/report}"
TTY="${COREDESKTOP_TTY:-/dev/tty}"
# The receiver refuses bigger reports
MAX_BYTES=$(( 10 * 1024 * 1024 ))
usage() {
cat << EOF
Usage: report [FILE] Send a log (the newest, if none is named)
report --view [FILE] Show what would be sent, and send nothing
EOF
}
newest_log() {
local logs
mapfile -t logs < <(ls -t "$LOGS"/*.log 2> /dev/null)
[ "${#logs[@]}" -gt 0 ] && echo "${logs[0]}"
}
# Escape $1 for use in a sed extended regular expression, between / or #
regex_escape() {
printf '%s' "$1" | sed 's/[][\.*^$/+?(){}|#]/\\&/g'
}
# Print log $1 as it would be sent: without script(1)'s header and footer,
# terminal codes or progress redraws, and with personal details replaced.
# LC_ALL=C so the ranges mean bytes, whatever the locale. IPv4 addresses are
# replaced twice because a match takes the character after an address, which
# can be the one before the next; dotted versions such as 1.0.4.1.3 are kept,
# and so are four-part ones after v or == (v26.11.10.1, not1mm==26.11.10.1).
# A single = doesn't count, so addr=192.168.1.20 is still replaced.
#
# Secrets go first, before the rules below can take part of one: private keys,
# passwords in URLs (https://user:password@host), Bearer and Basic tokens,
# password=..., token: ... and the like, API keys by their prefixes (Anthropic,
# OpenAI, GitHub, GitLab, Slack, Tailscale, AWS, Hugging Face) and Nextcloud
# app passwords. Then MAC and IPv6 addresses (not 12:34:56, 1:2.3-4 or pacman's
# ":: "), and your Cores' domains and your name, as configure core saved them.
redact() {
local home user host
home="$(regex_escape "$HOME")"
user="$(regex_escape "${USER:-$(id -un)}")"
host="$(regex_escape "$(uname -n)")"
local hex='[0-9A-Fa-f]{1,4}' personal=() value
while IFS= read -r value; do
[ -n "$value" ] && personal+=(-e "s/(^|[^A-Za-z0-9-])$(regex_escape "$value")([^A-Za-z0-9-]|\$)/\\1<core-domain>\\2/gI")
done < <(core_values CORE_DOMAIN)
while IFS= read -r value; do
[ -n "$value" ] && personal+=(-e "s/(^|[^A-Za-z0-9])$(regex_escape "$value")([^A-Za-z0-9]|\$)/\\1<name>\\2/gI")
done < <(core_values CORE_NAME)
LC_ALL=C sed -E \
-e '1{/^Script started on /d}' -e '/^Script done on .*COMMAND_EXIT_CODE/d' \
-e 's/\x1b\][^\x07\x1b]*(\x07|\x1b\\)//g' -e 's#\x1b\[[0-9;?]*[ -/]*[@-~]##g' \
-e 's/\x1b[()][0-9A-Za-z]//g' -e 's/\x1b[=>]//g' -e 's/\r+$//' -e 's/.*\r//' \
-e '/-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----/,/-----END [A-Z0-9 ]*PRIVATE KEY-----/c <private key>' \
-e 's#([A-Za-z][A-Za-z0-9+.-]*://)[^/@[:space:]]+@#\1<credentials>@#g' \
-e 's#(Bearer|Basic)[[:space:]]+[A-Za-z0-9._~+/=-]{8,}#\1 <secret>#gI' \
-e "s/((password|passwd|passphrase|secret|token|api[_-]?key|access[_-]?key|private[_-]?key)[\"']?[[:space:]]*[:=][[:space:]]*[\"']?)[^[:space:]\"',;&]+/\\1<secret>/gI" \
-e 's/(sk-ant-[A-Za-z0-9_-]{8,}|sk-[A-Za-z0-9_-]{20,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|glpat-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|tskey-[A-Za-z0-9-]{8,}|AKIA[0-9A-Z]{16}|hf_[A-Za-z0-9]{20,})/<secret>/g' \
-e 's/(^|[^A-Za-z0-9-])([A-Za-z0-9]{5}-){4}[A-Za-z0-9]{5}([^A-Za-z0-9-]|$)/\1<secret>\3/g' \
-e "s#${home}([/[:space:]'\":]|\$)#~\\1#g" \
-e 's/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/<email>/g' \
-e 's/[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.ts\.net/<tailscale-name>/g' \
-e 's/(^=?|[^0-9.vV=]|[^=]=)([0-9]{1,3}\.){3}[0-9]{1,3}([^0-9.]|$)/\1<ip>\3/g' \
-e 's/(^=?|[^0-9.vV=]|[^=]=)([0-9]{1,3}\.){3}[0-9]{1,3}([^0-9.]|$)/\1<ip>\3/g' \
-e "s/(^|[^A-Za-z0-9_-])${user}([^A-Za-z0-9_-]|\$)/\\1<user>\\2/g" \
-e "s/(^|[^A-Za-z0-9_-])${host}([^A-Za-z0-9_-]|\$)/\\1<host>\\2/g" \
-e 's/(^|[^0-9A-Fa-f:])([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}([^0-9A-Fa-f:]|$)/\1<mac>\3/g' \
-e "s#(^|[^0-9A-Za-z:])($hex:){4,7}$hex(/[0-9]{1,3})?([^0-9A-Za-z:]|\$)#\\1<ip>\\4#g" \
-e "s#(^|[^0-9A-Za-z:])$hex(:$hex){0,6}::($hex(:$hex){0,6})?(/[0-9]{1,3})?([^0-9A-Za-z:]|\$)#\\1<ip>\\6#g" \
-e "s#(^|[^0-9A-Za-z:])::$hex(:$hex){0,6}(/[0-9]{1,3})?([^0-9A-Za-z:]|\$)#\\1<ip>\\4#g" \
"${personal[@]}" \
"$1"
}
# Each value of setting $1 (CORE_DOMAIN, CORE_NAME) in your Cores' settings,
# as configure core saved them
core_values() {
local file
for file in "$HOME/.config/coredesktop/core.conf" "$HOME/.config/coredesktop/cores/"*.conf; do
[ -f "$file" ] || continue
# Read, not sourced, so nothing in the file runs. configure core writes
# each value with printf %q: take out its backslashes and quotes.
sed -nE "s/^$1=//p" "$file" | sed -E "s/^\\\$?'(.*)'\$/\\1/; s/\\\\(.)/\\1/g"
done | sort -u
}
coredesktop_version() {
git -C "$COREDESKTOP" log -1 --format='%cd (%h)' --date=format:%Y.%m.%d 2> /dev/null || echo unknown
}
# "name version" for each installed sibling, without its URL, which can name a person
sibling_versions() {
[ -f "$COREDESKTOP/sibling" ] || return 0
bash "$COREDESKTOP/sibling" list 2> /dev/null | grep -v '^No sibling' \
| awk '{ $NF = ""; sub(/ +$/, ""); print }'
}
# show_redacted FILE: page through what would be sent
show_redacted() {
if command -v less > /dev/null && [ -t 1 ]; then
redact "$1" | less
else
redact "$1"
fi
}
# send FILE [STEP...]: send FILE's report and print its ID
send() {
local file="$1" kind tmp response code body
shift
kind="$(basename "$file")"
kind="${kind%%-*}"
[[ "$kind" == install || "$kind" == update ]] || kind=other
tmp="$(mktemp -d)" || return 1
redact "$file" | gzip -9 > "$tmp/log.gz"
if (( $(stat -c %s "$tmp/log.gz") > MAX_BYTES )); then
echo "This log is too big to send ($(( $(stat -c %s "$tmp/log.gz") / 1024 / 1024 )) MB compressed)."
rm -rf "$tmp"
return 1
fi
local failed
failed="$(printf '%s, ' "$@")"
failed="${failed%, }"
echo "Sending..."
# Values go in with -F name=<file, which sends them as they are: with
# name=value, curl would treat a value starting with @ or < as a file
printf '%s' "$kind" > "$tmp/kind"
coredesktop_version | tr -d '\n' > "$tmp/version"
printf '%s' "$failed" > "$tmp/failed"
uname -r | tr -d '\n' > "$tmp/kernel"
sibling_versions > "$tmp/siblings"
response="$(curl -sS --max-time 120 -A CoreDesktop-report \
-F "kind=<$tmp/kind" -F "version=<$tmp/version" -F "failed=<$tmp/failed" \
-F "kernel=<$tmp/kernel" -F "siblings=<$tmp/siblings" \
-F "log=@$tmp/log.gz;type=application/gzip;filename=log.gz" \
-w '\n%{http_code}' "$REPORT_URL" 2>&1)"
local status=$?
rm -rf "$tmp"
code="${response##*$'\n'}"
body="${response%$'\n'*}"
[[ "$response" == *$'\n'* ]] || body="$response"
# Without the newline that usually ends a reply
body="${body%"${body##*[![:space:]]}"}"
# A 2xx that isn't a report ID is some other page, not the receiver
if (( status == 0 )) && [[ "$code" == 2?? && "$body" =~ ^[A-Za-z0-9-]{1,40}$ ]]; then
echo "Sent. Thank you! Your report's ID is $body; mention it if you contact us about this."
return 0
fi
echo "Couldn't send the report: ${body:-no response}"
echo "To try again later, run: report $file"
return 1
}
# Open the terminal once, on file descriptor 3, for ask to read answers from.
# Fails if there's no terminal to ask on.
open_tty() {
{ exec 3< "$TTY"; } 2> /dev/null
}
# ask QUESTION: read an answer from the terminal into $answer
ask() {
read -rp "$1" answer <&3
}
# offer FILE [STEP...]: ask whether to send FILE, show it first if asked
offer() {
local file="$1"
[ -f "$file" ] || return 0
open_tty || return 0
echo
echo "Send this log to the CoreDesktop team, so we can look into what went wrong?"
echo "Personal details such as your user name, host name and email addresses are"
echo "taken out first."
while ask "[v] view what would be sent, [y] send, [N] don't send: "; do
case "$answer" in
[vV]*) show_redacted "$file" ;;
[yY]*) send "$@"; return 0 ;;
*) break ;;
esac
done
echo "Not sent. To send it later, run: report $file"
}
case "$1" in
-h|--help) usage ;;
--offer) shift; offer "$@" ;;
--view)
file="${2:-$(newest_log)}"
[ -f "$file" ] || { echo "No log to show."; exit 1; }
show_redacted "$file"
;;
-*) usage; exit 1 ;;
*)
file="${1:-$(newest_log)}"
[ -f "$file" ] || { echo "No log found in $LOGS."; exit 1; }
echo "Log: $file"
if ! open_tty; then
# No terminal to ask on: running report is itself the request to send
send "$file"
exit
fi
while true; do
ask "[v] view what would be sent, [y] send, [N] don't send: " || answer=n
case "$answer" in
[vV]*) show_redacted "$file" ;;
[yY]*) send "$file"; exit ;;
*) echo "Not sent."; exit 0 ;;
esac
done
;;
esac