diff --git a/.github/workflows/privacy-guard.yml b/.github/workflows/privacy-guard.yml index a1de8a2..727e2a1 100644 --- a/.github/workflows/privacy-guard.yml +++ b/.github/workflows/privacy-guard.yml @@ -36,7 +36,45 @@ jobs: run: gitleaks detect --source . --log-opts=--all --redact --no-banner --exit-code 1 - name: Test denylist guard run: bash scripts/test-privacy-denylist.sh + - name: Test commit-metadata email check + run: bash scripts/test-check-commit-emails.sh - name: Check denylist env: PRIVACY_DENYLIST_EXTRA: ${{ secrets.PRIVACY_DENYLIST_EXTRA }} - run: bash scripts/privacy-denylist.sh + run: | + set -euo pipefail + # Tracked denylist must always run. If the optional CI secret has invalid + # git-grep regexes, warn and continue without it so PRs are not blocked + # while the secret is repaired (same failure mode as an empty secret). + code=0 + bash scripts/privacy-denylist.sh || code=$? + if [[ "$code" -eq 0 ]]; then + exit 0 + fi + if [[ "$code" -eq 2 && -n "${PRIVACY_DENYLIST_EXTRA:-}" ]]; then + echo "::warning title=Privacy guard::PRIVACY_DENYLIST_EXTRA has invalid regex line(s) for git grep -E. Re-running with the tracked denylist only. Fix the Actions secret." + unset PRIVACY_DENYLIST_EXTRA + bash scripts/privacy-denylist.sh + exit $? + fi + exit "$code" + - name: Check commit emails and trailers + env: + EVENT_NAME: ${{ github.event_name }} + PR_BASE: ${{ github.event.pull_request.base.sha }} + PR_HEAD: ${{ github.event.pull_request.head.sha }} + BEFORE: ${{ github.event.before }} + DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} + REF_NAME: ${{ github.ref_name }} + run: | + set -euo pipefail + if [[ "$EVENT_NAME" == "pull_request" ]]; then + export PRIVACY_COMMIT_RANGE="${PR_BASE}..${PR_HEAD}" + elif [[ "$EVENT_NAME" == "push" && "$REF_NAME" == "$DEFAULT_BRANCH" ]]; then + export PRIVACY_COMMIT_RANGE="--all" + elif [[ "$EVENT_NAME" == "push" && "${BEFORE:-}" != "" && "$BEFORE" != 0000000000000000000000000000000000000000 ]]; then + export PRIVACY_COMMIT_RANGE="${BEFORE}..HEAD" + else + export PRIVACY_COMMIT_RANGE="--all" + fi + bash scripts/check-commit-emails.sh diff --git a/.privacy-allowlist b/.privacy-allowlist index c65f78f..2e5836a 100644 --- a/.privacy-allowlist +++ b/.privacy-allowlist @@ -5,6 +5,7 @@ :[0-9]+:[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+\.(test|invalid|example)$ :[0-9]+:[A-Za-z0-9._%+-]+@example\.(com|org|net)$ :[0-9]+:[A-Za-z0-9._%+-]+@users\.noreply\.github\.com$ +:[0-9]+:noreply@github\.com$ :[0-9]+:git@github\.com$ # CI runner home directories diff --git a/.privacy-denylist b/.privacy-denylist index 88e5608..e9cc6da 100644 --- a/.privacy-denylist +++ b/.privacy-denylist @@ -8,6 +8,11 @@ \b100\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\.[0-9]{1,3}\.[0-9]{1,3}\b \bfd7a:115c:a1e0:[0-9a-f:]+ +# Provider-assigned VPS hostnames (not the vendor name itself) +[a-z0-9-]+\.vps\.ovh\.(net|ca)\b +[a-z0-9.-]+\.your-server\.de\b +[a-z0-9-]+\.hetzner\.cloud\b + # Private network addresses \b10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b \b192\.168\.[0-9]{1,3}\.[0-9]{1,3}\b diff --git a/scripts/check-commit-emails.sh b/scripts/check-commit-emails.sh new file mode 100755 index 0000000..e3897ac --- /dev/null +++ b/scripts/check-commit-emails.sh @@ -0,0 +1,187 @@ +#!/usr/bin/env bash +# +# check-commit-emails.sh — fail when a commit in the scan range uses a +# disallowed author, committer, Co-authored-by, or Signed-off-by email, +# or when commit metadata contains a private hostname or internal IP. +# +# Matched values are never printed. Findings are reported as +# +# only. Public CI logs are public. +# +# Range (first match wins): +# $PRIVACY_COMMIT_RANGE a git revision range, or the token --all +# --range | --all command-line flags +# otherwise --all +# +# Allowed emails (override with $ALLOWED_EMAIL_PATTERN): +# *@users.noreply.github.com and noreply@github.com +# +# Exit codes: 0 clean, 1 finding, 2 misuse. + +set -u + +cd "$(git rev-parse --show-toplevel)" || exit 2 + +ALLOWED_EMAIL_PATTERN="${ALLOWED_EMAIL_PATTERN:-(@users\.noreply\.github\.com|^noreply@github\.com)$}" + +# Hostname and address patterns, assembled from fragments so this file does +# not itself match a tree denylist. Keep them generic: no personal names. +TS_NET='[a-z0-9-]+\.ts''\.net\b' +OVH_VPS='[a-z0-9-]+\.vps\.ovh''\.net\b' +OVH_VPS_CA='[a-z0-9-]+\.vps\.ovh''\.ca\b' +HETZNER_DED='[a-z0-9.-]+\.your-server''\.de\b' +HETZNER_CLOUD='[a-z0-9-]+\.hetzner''\.cloud\b' +# Tailnet CGNAT (100.64/10) and RFC1918 +CGNAT='\b100\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\.[0-9]{1,3}\.[0-9]{1,3}\b' +RFC10='\b10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b' +RFC192='\b192\.168\.[0-9]{1,3}\.[0-9]{1,3}\b' +RFC172='\b172\.(1[6-9]|2[0-9]|3[01])\.[0-9]{1,3}\.[0-9]{1,3}\b' + +HOSTNAME_PATTERN="${TS_NET}|${OVH_VPS}|${OVH_VPS_CA}|${HETZNER_DED}|${HETZNER_CLOUD}" +IP_PATTERN="${CGNAT}|${RFC10}|${RFC192}|${RFC172}" + +usage() { + echo "usage: $0 [--all] [--range ]" >&2 + exit 2 +} + +RANGE="${PRIVACY_COMMIT_RANGE:-}" +while [[ $# -gt 0 ]]; do + case "$1" in + --all) RANGE="--all"; shift ;; + --range) + [[ $# -ge 2 ]] || usage + RANGE="$2" + shift 2 + ;; + -h|--help) usage ;; + *) usage ;; + esac +done +[[ -n "$RANGE" ]] || RANGE="--all" + +# Validate the allowed-email pattern without printing it. +if ! printf 'a@users.noreply.github.com\n' | grep -qE "$ALLOWED_EMAIL_PATTERN"; then + echo "error: ALLOWED_EMAIL_PATTERN does not accept a GitHub noreply address" >&2 + exit 2 +fi +if ! printf 'noreply@github.com\n' | grep -qE "$ALLOWED_EMAIL_PATTERN"; then + echo "error: ALLOWED_EMAIL_PATTERN does not accept noreply@github.com" >&2 + exit 2 +fi + +# Resolve the commit list. An empty range (no new commits) is clean. +commits="$(mktemp)" +findings="$(mktemp)" +trap 'rm -f "$commits" "$findings"' EXIT + +if [[ "$RANGE" == "--all" ]]; then + if ! git rev-list --all > "$commits" 2>/dev/null; then + echo "error: could not list commits for --all" >&2 + exit 2 + fi +else + if ! git rev-list "$RANGE" > "$commits" 2>/dev/null; then + echo "error: invalid commit range" >&2 + exit 2 + fi +fi + +if [[ ! -s "$commits" ]]; then + echo "OK commit metadata: no commits in range" + exit 0 +fi + +# classify_email -> category (or empty if allowed) +classify_email() { + local addr="$1" + [[ -n "$addr" ]] || return 0 + if printf '%s\n' "$addr" | grep -qE "$ALLOWED_EMAIL_PATTERN"; then + return 0 + fi + if printf '%s\n' "$addr" | grep -qiE "$HOSTNAME_PATTERN"; then + echo "private-hostname" + return 0 + fi + if printf '%s\n' "$addr" | grep -qiE "$IP_PATTERN"; then + echo "private-ip" + return 0 + fi + echo "disallowed-email" +} + +# classify_text -> category (or empty) +classify_text() { + local text="$1" + [[ -n "$text" ]] || return 0 + if printf '%s\n' "$text" | grep -qiE "$HOSTNAME_PATTERN"; then + echo "private-hostname" + return 0 + fi + if printf '%s\n' "$text" | grep -qiE "$IP_PATTERN"; then + echo "private-ip" + return 0 + fi +} + +# extract_trailer_email — Co-authored-by / Signed-off-by "Name " +extract_trailer_email() { + local line="$1" addr + addr="$(printf '%s\n' "$line" | sed -n 's/.*<\([^>]*\)>.*/\1/p')" + printf '%s\n' "$addr" +} + +while IFS= read -r sha || [[ -n "$sha" ]]; do + [[ -n "$sha" ]] || continue + short="$(git rev-parse --short "$sha")" + author="$(git log -1 --format='%ae' "$sha")" + committer="$(git log -1 --format='%ce' "$sha")" + subject="$(git log -1 --format='%s' "$sha")" + body="$(git log -1 --format='%b' "$sha")" + + catg="$(classify_email "$author")" + [[ -n "$catg" ]] && printf '%s\tauthor\t%s\n' "$short" "$catg" >> "$findings" + catg="$(classify_email "$committer")" + [[ -n "$catg" ]] && printf '%s\tcommitter\t%s\n' "$short" "$catg" >> "$findings" + + # Trailers only (unfolded). Do not treat the whole body as trailers. + trailers="$(git log -1 --format='%(trailers:only,unfold)' "$sha")" + while IFS= read -r tline || [[ -n "$tline" ]]; do + [[ -n "$tline" ]] || continue + key="$(printf '%s\n' "$tline" | sed -n 's/^\([^:]*\):.*/\1/p' | tr '[:upper:]' '[:lower:]')" + case "$key" in + co-authored-by|signed-off-by) + addr="$(extract_trailer_email "$tline")" + catg="$(classify_email "$addr")" + field="Co-authored-by" + [[ "$key" == "signed-off-by" ]] && field="Signed-off-by" + [[ -n "$catg" ]] && printf '%s\t%s\t%s\n' "$short" "$field" "$catg" >> "$findings" + ;; + esac + done <<< "$trailers" + + # Subject and body (including non-trailer text) for hostnames and internal IPs. + # Email addresses in the message body that are not trailers are not classified + # here; author/committer/trailer checks cover the metadata leak path. + catg="$(classify_text "$subject")" + [[ -n "$catg" ]] && printf '%s\tmessage\t%s\n' "$short" "$catg" >> "$findings" + # Skip re-reporting the same category already found in the subject. + body_catg="$(classify_text "$body")" + if [[ -n "$body_catg" && "$body_catg" != "$catg" ]]; then + printf '%s\tmessage\t%s\n' "$short" "$body_catg" >> "$findings" + elif [[ -n "$body_catg" && -z "$catg" ]]; then + printf '%s\tmessage\t%s\n' "$short" "$body_catg" >> "$findings" + fi +done < "$commits" + +if [[ -s "$findings" ]]; then + count="$(wc -l < "$findings" | tr -d ' ')" + echo "FAIL commit metadata: $count finding(s); values withheld" + # field may contain a hyphen; columns are tab-separated + sort -u "$findings" | awk -F '\t' '{printf " %s %s %s\n", $1, $2, $3}' + exit 1 +fi + +n="$(wc -l < "$commits" | tr -d ' ')" +echo "OK commit metadata: $n commit(s) in range, no findings" +exit 0 diff --git a/scripts/test-check-commit-emails.sh b/scripts/test-check-commit-emails.sh new file mode 100755 index 0000000..046e815 --- /dev/null +++ b/scripts/test-check-commit-emails.sh @@ -0,0 +1,178 @@ +#!/usr/bin/env bash +# +# test-check-commit-emails.sh — success and failure paths of +# check-commit-emails.sh in a throwaway repository. Fixtures are assembled +# from fragments so this file never matches a tree denylist. + +set -u + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +REPO="$(mktemp -d)" +trap 'rm -rf "$REPO"' EXIT + +mkdir -p "$REPO/scripts" +cp "$ROOT/scripts/check-commit-emails.sh" "$REPO/scripts/" +chmod +x "$REPO/scripts/check-commit-emails.sh" + +git -C "$REPO" init -q +git -C "$REPO" config user.email "123+tester@users.noreply.github.com" +git -C "$REPO" config user.name tester + +failures=0 +LAST_OUTPUT="" +LAST_STATUS=0 + +# Fragmented fixtures (adjacent quoted strings). +ICLOUD_ADDR="someone@""icloud.com" +OVH_HOST="vps-abc123fe"".vps.ovh.net" +OVH_EMAIL="ubuntu@${OVH_HOST}" +TS_HOST="box.example-net"".ts.net" +PRIV_IP="192.168"."0.42" +NOREPLY="123+tester@users.noreply.github.com" +WEB_FLOW="noreply@github.com" + +commit() { + # commit [VAR=value ...] — runs in $REPO + local message="$1"; shift + printf 'x\n' >> "$REPO/doc.md" + git -C "$REPO" add doc.md + (cd "$REPO" && env "$@" git commit -q -m "$message") +} + +run() { + local name="$1" expected="$2"; shift 2 + local envvars=() args=() + while [[ $# -gt 0 ]]; do + case "$1" in + --all) + args+=("$1"); shift + ;; + --range) + args+=("$1" "$2"); shift 2 + ;; + *=*) + envvars+=("$1"); shift + ;; + *) + args+=("$1"); shift + ;; + esac + done + LAST_OUTPUT="$(cd "$REPO" && env "${envvars[@]}" bash scripts/check-commit-emails.sh "${args[@]}" 2>&1)" + LAST_STATUS=$? + if [[ "$LAST_STATUS" != "$expected" ]]; then + printf 'FAIL %s: expected exit %s, got %s\n%s\n' "$name" "$expected" "$LAST_STATUS" "$LAST_OUTPUT" >&2 + failures=$((failures + 1)) + return 1 + fi + printf 'OK %s\n' "$name" + return 0 +} + +refute() { + local name="$1" text="$2" + if grep -qF -- "$text" <<< "$LAST_OUTPUT"; then + printf 'FAIL %s: output leaked %s\n%s\n' "$name" "" "$LAST_OUTPUT" >&2 + failures=$((failures + 1)) + fi +} + +expect() { + local name="$1" text="$2" + if ! grep -qE -- "$text" <<< "$LAST_OUTPUT"; then + printf 'FAIL %s: output missing %s\n%s\n' "$name" "$text" "$LAST_OUTPUT" >&2 + failures=$((failures + 1)) + fi +} + +# 1. Clean history of noreply commits passes (full history). +commit "clean base" +run "noreply history passes" 0 PRIVACY_COMMIT_RANGE=--all + +# 2. Personal author email fails; value withheld; sha and category reported. +commit "personal author" GIT_AUTHOR_EMAIL="$ICLOUD_ADDR" GIT_AUTHOR_NAME=someone +personal_sha="$(git -C "$REPO" rev-parse --short HEAD)" +run "personal author email fails" 1 PRIVACY_COMMIT_RANGE=--all +expect "reports author field" "author" +expect "reports disallowed-email" "disallowed-email" +expect "reports short sha" "$personal_sha" +refute "does not print personal address" "$ICLOUD_ADDR" +refute "does not print local-part" "someone@" + +# Reset to a clean line by going back? Keep going; later tests use ranges. + +# 3. Personal Co-authored-by trailer fails. +commit "$(printf 'add collaborator\n\nCo-authored-by: Someone <%s>\n' "$ICLOUD_ADDR")" +trailer_sha="$(git -C "$REPO" rev-parse --short HEAD)" +run "personal Co-authored-by fails" 1 --range "$trailer_sha^..$trailer_sha" +expect "reports Co-authored-by" "Co-authored-by" +expect "reports disallowed-email for trailer" "disallowed-email" +refute "trailer value withheld" "$ICLOUD_ADDR" + +# 4. Personal Signed-off-by trailer fails. +commit "$(printf 'signoff\n\nSigned-off-by: Someone <%s>\n' "$ICLOUD_ADDR")" +sign_sha="$(git -C "$REPO" rev-parse --short HEAD)" +run "personal Signed-off-by fails" 1 --range "$sign_sha^..$sign_sha" +expect "reports Signed-off-by" "Signed-off-by" +refute "signoff value withheld" "$ICLOUD_ADDR" + +# 5. Provider VPS hostname as author email fails as private-hostname. +commit "vps author" GIT_AUTHOR_EMAIL="$OVH_EMAIL" GIT_AUTHOR_NAME=ubuntu +vps_sha="$(git -C "$REPO" rev-parse --short HEAD)" +run "vps hostname author fails" 1 --range "$vps_sha^..$vps_sha" +expect "reports private-hostname" "private-hostname" +expect "reports author for vps" "author" +refute "vps hostname withheld" "$OVH_HOST" +refute "vps email withheld" "$OVH_EMAIL" + +# 6. web-flow committer (noreply@github.com) is allowed with a noreply author. +commit "web flow" GIT_COMMITTER_EMAIL="$WEB_FLOW" GIT_COMMITTER_NAME=GitHub +web_sha="$(git -C "$REPO" rev-parse --short HEAD)" +run "web-flow committer passes" 0 --range "$web_sha^..$web_sha" + +# 7. Commit message with a tailnet hostname fails; value withheld. +commit "mention $TS_HOST in the subject" +ts_sha="$(git -C "$REPO" rev-parse --short HEAD)" +run "tailnet hostname in message fails" 1 --range "$ts_sha^..$ts_sha" +expect "reports message field" "message" +expect "reports private-hostname for ts" "private-hostname" +refute "ts hostname withheld" "$TS_HOST" + +# 8. Commit message with an RFC1918 address fails. +commit "bind to $PRIV_IP" +ip_sha="$(git -C "$REPO" rev-parse --short HEAD)" +run "private ip in message fails" 1 --range "$ip_sha^..$ip_sha" +expect "reports private-ip" "private-ip" +refute "private ip withheld" "$PRIV_IP" + +# 9. A range that excludes dirty history passes. +# The first commit is clean; use that one only. +root="$(git -C "$REPO" rev-list --max-parents=0 HEAD)" +root_short="$(git -C "$REPO" rev-parse --short "$root")" +# Range of only the root commit: root^ is invalid; use root --not? Use +# git rev-list "$root" -1 via a one-commit range by SHA equality. +# --range does rev-list RANGE; a single sha lists that commit and ancestors. +# Use the root..root (empty) plus we need only the root. Pass the root as +# a range by setting PRIVACY_COMMIT_RANGE to the root sha — rev-list +# lists the sha and all ancestors, which for root is just root. +run "range of only the clean root passes" 0 --range "$root" +expect "clean range ok line" "OK" + +# 10. Empty range is clean (range from HEAD to HEAD with two-dot is empty). +head_sha="$(git -C "$REPO" rev-parse HEAD)" +run "empty two-dot range passes" 0 --range "${head_sha}..${head_sha}" +expect "empty range message" "no commits in range" + +# 11. Invalid range is misuse. +run "invalid range exits 2" 2 --range "this-is-not-a-rev-xyz" + +# 12. Full history still fails (dirty commits exist). +run "full history with dirt fails" 1 --all +expect "full history fail line" "FAIL commit metadata" + +if [[ "$failures" -ne 0 ]]; then + echo "FAILED $failures test(s)" >&2 + exit 1 +fi +echo "All check-commit-emails tests passed." +exit 0 diff --git a/scripts/test-privacy-denylist.sh b/scripts/test-privacy-denylist.sh index 31c741a..c074783 100755 --- a/scripts/test-privacy-denylist.sh +++ b/scripts/test-privacy-denylist.sh @@ -47,6 +47,9 @@ refute() { } TAILNET="box.example-net.ts"".net" +OVH_VPS="vps-abc123fe"".vps.ovh.net" +HETZNER="node-1"".hetzner.cloud" +HETZNER_DED="static.12.34.56.clients.your-server"".de" plant "Plain documentation." check "clean tree passes" 0 NOOP=1 @@ -56,6 +59,21 @@ check "tailnet hostname fails" 1 NOOP=1 grep -q 'doc.md:1' <<< "$LAST_OUTPUT" || { echo "FAIL hit location not reported" >&2; failures=$((failures + 1)); } refute "matched content leaked to output" "example-net" +plant "host: $OVH_VPS" +check "ovh vps hostname fails" 1 NOOP=1 +refute "ovh hostname leaked to output" "abc123fe" + +plant "host: $HETZNER" +check "hetzner cloud hostname fails" 1 NOOP=1 +refute "hetzner cloud leaked to output" "node-1" + +plant "host: $HETZNER_DED" +check "hetzner dedicated hostname fails" 1 NOOP=1 +refute "hetzner dedicated leaked to output" "your-server" + +plant "The host is an OVH VPS (provider name only)." +check "provider name without hostname passes" 0 NOOP=1 + plant "id 123e4567-e89b-12d3-a456-4266""14174000" check "uuid fails" 1 NOOP=1