Test the report receiver and redaction against attacks
shellcheck / shellcheck (push) Failing after 19s
shellcheck / shellcheck (push) Failing after 19s
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>
This commit is contained in:
@@ -11,8 +11,10 @@
|
||||
# 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 are replaced: the home folder, user
|
||||
# name, host name, email addresses, IPv4 addresses and Tailscale names.
|
||||
# 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
|
||||
@@ -46,9 +48,9 @@ newest_log() {
|
||||
[ "${#logs[@]}" -gt 0 ] && echo "${logs[0]}"
|
||||
}
|
||||
|
||||
# Escape $1 for use in a sed regular expression
|
||||
# Escape $1 for use in a sed extended regular expression, between / or #
|
||||
regex_escape() {
|
||||
printf '%s' "$1" | sed 's/[][\.*^$/]/\\&/g'
|
||||
printf '%s' "$1" | sed 's/[][\.*^$/+?(){}|#]/\\&/g'
|
||||
}
|
||||
|
||||
# Print log $1 as it would be sent: without script(1)'s header and footer,
|
||||
@@ -58,15 +60,35 @@ regex_escape() {
|
||||
# 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' \
|
||||
@@ -74,9 +96,26 @@ redact() {
|
||||
-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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user