diff --git a/.github/workflows/privacy-guard-history-audit.yml b/.github/workflows/privacy-guard-history-audit.yml new file mode 100644 index 0000000..6ca0883 --- /dev/null +++ b/.github/workflows/privacy-guard-history-audit.yml @@ -0,0 +1,43 @@ +# Optional, non-required weekly full-history audit. Use this only in a +# repo that has a committed .privacy-baseline for accepted historical +# exposure. Copy to .github/workflows/privacy-guard-history-audit.yml. +# Do not add "Privacy Guard history audit" as a required status check. +# The required Privacy Guard job still covers new commits; this job +# reports remaining historical findings until a rewrite is authorised. +# +# This file contains no personal data and no account-specific addresses. + +name: Privacy Guard history audit + +on: + schedule: + - cron: "17 4 * * 1" + workflow_dispatch: + +permissions: + contents: read + +jobs: + privacy-guard-history-audit: + name: Privacy Guard history audit + runs-on: ubuntu-latest + env: + GITLEAKS_VERSION: "8.30.1" + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Install gitleaks + run: | + set -euo pipefail + base="https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}" + archive="gitleaks_${GITLEAKS_VERSION}_linux_x64.tar.gz" + curl -sSfL -o "$RUNNER_TEMP/$archive" "$base/$archive" + curl -sSfL -o "$RUNNER_TEMP/checksums.txt" "$base/gitleaks_${GITLEAKS_VERSION}_checksums.txt" + (cd "$RUNNER_TEMP" && grep " ${archive}\$" checksums.txt | sha256sum -c -) + tar -xzf "$RUNNER_TEMP/$archive" -C "$RUNNER_TEMP" gitleaks + echo "$RUNNER_TEMP" >> "$GITHUB_PATH" + - name: Scan history for secrets + run: gitleaks detect --source . --log-opts=--all --redact --no-banner --exit-code 1 + - name: Check commit emails and trailers over full history + run: bash scripts/check-commit-emails.sh --all diff --git a/.github/workflows/privacy-guard.yml b/.github/workflows/privacy-guard.yml index 5d7c78c..c50995a 100644 --- a/.github/workflows/privacy-guard.yml +++ b/.github/workflows/privacy-guard.yml @@ -11,6 +11,9 @@ # .privacy-allowlist # And optionally: # .githooks/pre-push (see pre-push in this directory) +# .privacy-baseline (see privacy-baseline.example) +# privacy-guard-history-audit.yml (weekly full-history audit; +# do not make it a required check) # # Then add "Privacy Guard" as a required status check on the default branch. # Do not print secret or personal values in logs. This file contains no @@ -21,25 +24,17 @@ name: Privacy Guard on: push: pull_request: - # Deviation from the vllnt/skills template: pre-existing history fails - # the full-history email scan, and the template has no SHA baseline. - # The required job scopes push scans to the pushed range; this - # scheduled/manual job reports a full-history audit and is not required. - schedule: - - cron: "17 4 * * 1" - workflow_dispatch: permissions: contents: read concurrency: - group: privacy-guard-${{ github.event_name }}-${{ github.head_ref || github.ref }} + group: privacy-guard-${{ github.head_ref || github.ref }} cancel-in-progress: true jobs: privacy-guard: name: Privacy Guard - if: github.event_name != 'schedule' && github.event_name != 'workflow_dispatch' runs-on: ubuntu-latest env: GITLEAKS_VERSION: "8.30.1" @@ -90,12 +85,18 @@ jobs: REF_NAME: ${{ github.ref_name }} run: | set -euo pipefail - # Deviation from the vllnt/skills template: pre-existing history - # fails the full-history email scan, and the template has no SHA - # baseline. Pull requests: the commits this PR adds. Pushes with - # a known before SHA: the pushed range. Otherwise: full history. + # Validate a committed .privacy-baseline whenever it is present. + # Pull requests: the commits this PR adds. Push: baseline..HEAD + # when a baseline exists, otherwise full history on the default + # branch, or the pushed range on other refs. + baseline="$(bash scripts/check-commit-emails.sh --print-baseline)" if [[ "$EVENT_NAME" == "pull_request" ]]; then export PRIVACY_COMMIT_RANGE="${PR_BASE}..${PR_HEAD}" + elif [[ -n "${baseline}" ]]; then + echo "commit-metadata range: ${baseline:0:12}..HEAD" + export PRIVACY_COMMIT_RANGE="${baseline}..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 @@ -117,51 +118,3 @@ jobs: - name: Check package contents run: bash scripts/check-package-contents.sh - privacy-guard-history-audit: - name: Privacy Guard history audit - if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' - runs-on: ubuntu-latest - env: - GITLEAKS_VERSION: "8.30.1" - steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - name: Install gitleaks - run: | - set -euo pipefail - base="https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}" - archive="gitleaks_${GITLEAKS_VERSION}_linux_x64.tar.gz" - curl -sSfL -o "$RUNNER_TEMP/$archive" "$base/$archive" - curl -sSfL -o "$RUNNER_TEMP/checksums.txt" "$base/gitleaks_${GITLEAKS_VERSION}_checksums.txt" - (cd "$RUNNER_TEMP" && grep " ${archive}\$" checksums.txt | sha256sum -c -) - tar -xzf "$RUNNER_TEMP/$archive" -C "$RUNNER_TEMP" gitleaks - echo "$RUNNER_TEMP" >> "$GITHUB_PATH" - - name: Scan history for secrets - 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: | - set -euo pipefail - 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 (full history) - run: | - set -euo pipefail - export PRIVACY_COMMIT_RANGE="--all" - bash scripts/check-commit-emails.sh diff --git a/.privacy-baseline b/.privacy-baseline new file mode 100644 index 0000000..23be653 --- /dev/null +++ b/.privacy-baseline @@ -0,0 +1,8 @@ +# Placeholder draft. decided is not an acceptance date. +# Do not merge until the exposure is accepted and recorded +# in the security-findings ledger. +# +# Required keys (no others): +sha: 8cd099d035f7e0673377844f5038f3861798dc88 +reason: historical commit-metadata findings; no rewrite +decided: 1970-01-01 diff --git a/scripts/check-commit-emails.sh b/scripts/check-commit-emails.sh index e3897ac..8607b7d 100755 --- a/scripts/check-commit-emails.sh +++ b/scripts/check-commit-emails.sh @@ -11,8 +11,16 @@ # Range (first match wins): # $PRIVACY_COMMIT_RANGE a git revision range, or the token --all # --range | --all command-line flags +# otherwise, if a baseline file is present, ..HEAD # otherwise --all # +# Optional committed baseline file (default .privacy-baseline, override +# with $PRIVACY_BASELINE_FILE or --baseline-file). Allowed only for +# historical exposure the maintainer has accepted, recorded in the +# security-findings ledger. The file is validated whenever it is present. +# --print-baseline prints the resolved SHA and exits, or prints nothing +# and exits 0 when the file is absent. +# # Allowed emails (override with $ALLOWED_EMAIL_PATTERN): # *@users.noreply.github.com and noreply@github.com # @@ -41,11 +49,84 @@ HOSTNAME_PATTERN="${TS_NET}|${OVH_VPS}|${OVH_VPS_CA}|${HETZNER_DED}|${HETZNER_CL IP_PATTERN="${CGNAT}|${RFC10}|${RFC192}|${RFC172}" usage() { - echo "usage: $0 [--all] [--range ]" >&2 + echo "usage: $0 [--all] [--range ] [--print-baseline] [--baseline-file ]" >&2 exit 2 } +# parse_baseline +# Prints the resolved 40-character commit SHA on stdout. +# Exit 2 on a present but invalid file. Does not print reason text. +parse_baseline() { + local file="$1" + local line key value sha="" reason="" decided="" + local -A seen=() + while IFS= read -r line || [[ -n "$line" ]]; do + line="${line%$'\r'}" + [[ -z "${line// /}" ]] && continue + [[ "$line" == \#* ]] && continue + if [[ "$line" != *:* ]]; then + echo "error: .privacy-baseline has a malformed line" >&2 + return 2 + fi + key="${line%%:*}" + value="${line#*:}" + key="${key#"${key%%[![:space:]]*}"}" + key="${key%"${key##*[![:space:]]}"}" + value="${value#"${value%%[![:space:]]*}"}" + value="${value%"${value##*[![:space:]]}"}" + key="${key,,}" + if [[ -z "$key" ]]; then + echo "error: .privacy-baseline has a malformed line" >&2 + return 2 + fi + if [[ -n "${seen[$key]:-}" ]]; then + echo "error: .privacy-baseline has a duplicate key" >&2 + return 2 + fi + seen[$key]=1 + case "$key" in + sha) + if [[ ! "$value" =~ ^[0-9a-fA-F]{7,40}$ ]]; then + echo "error: .privacy-baseline sha is not a git SHA" >&2 + return 2 + fi + sha="$value" + ;; + reason) + if [[ -z "$value" ]]; then + echo "error: .privacy-baseline reason is empty" >&2 + return 2 + fi + reason="$value" + ;; + decided) + if [[ ! "$value" =~ ^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$ ]]; then + echo "error: .privacy-baseline decided must be YYYY-MM-DD" >&2 + return 2 + fi + decided="$value" + ;; + *) + echo "error: .privacy-baseline has an unknown key" >&2 + return 2 + ;; + esac + done < "$file" + if [[ -z "$sha" || -z "$reason" || -z "$decided" ]]; then + echo "error: .privacy-baseline must set sha, reason, and decided" >&2 + return 2 + fi + local resolved + if ! resolved="$(git rev-parse --verify "${sha}^{commit}" 2>/dev/null)"; then + echo "error: .privacy-baseline sha does not resolve to a commit" >&2 + return 2 + fi + printf '%s\n' "$resolved" +} + RANGE="${PRIVACY_COMMIT_RANGE:-}" +BASELINE_FILE="${PRIVACY_BASELINE_FILE:-.privacy-baseline}" +PRINT_BASELINE=0 while [[ $# -gt 0 ]]; do case "$1" in --all) RANGE="--all"; shift ;; @@ -54,11 +135,41 @@ while [[ $# -gt 0 ]]; do RANGE="$2" shift 2 ;; + --print-baseline) PRINT_BASELINE=1; shift ;; + --baseline-file) + [[ $# -ge 2 ]] || usage + BASELINE_FILE="$2" + shift 2 + ;; -h|--help) usage ;; *) usage ;; esac done -[[ -n "$RANGE" ]] || RANGE="--all" + +BASELINE_SHA="" +if [[ -f "$BASELINE_FILE" ]]; then + if ! BASELINE_SHA="$(parse_baseline "$BASELINE_FILE")"; then + exit 2 + fi +elif [[ -e "$BASELINE_FILE" ]]; then + echo "error: .privacy-baseline exists but is not a regular file" >&2 + exit 2 +fi + +if [[ "$PRINT_BASELINE" -eq 1 ]]; then + if [[ -n "$BASELINE_SHA" ]]; then + printf '%s\n' "$BASELINE_SHA" + fi + exit 0 +fi + +if [[ -z "$RANGE" ]]; then + if [[ -n "$BASELINE_SHA" ]]; then + RANGE="${BASELINE_SHA}..HEAD" + else + RANGE="--all" + fi +fi # Validate the allowed-email pattern without printing it. if ! printf 'a@users.noreply.github.com\n' | grep -qE "$ALLOWED_EMAIL_PATTERN"; then diff --git a/scripts/test-check-commit-emails.sh b/scripts/test-check-commit-emails.sh index 8f5ec65..50919ab 100755 --- a/scripts/test-check-commit-emails.sh +++ b/scripts/test-check-commit-emails.sh @@ -23,11 +23,14 @@ failures=0 LAST_OUTPUT="" LAST_STATUS=0 -# Fragmented fixtures (adjacent quoted strings). -ICLOUD_ADDR="someone@""icloud.com" -OVH_HOST="vps-abc123fe"".vps.ovh.net" +# Fixtures are assembled from fragments so this file never contains a +# contiguous real provider suffix, personal mailbox domain, or private +# address — including extra denylist patterns that may be loaded in CI. +# Assembled values stay valid for the checks below. +DISALLOWED_ADDR="someone@""example.invalid" +OVH_HOST="v""ps-abc123fe.v""ps.ov""h.net" OVH_EMAIL="ubuntu@${OVH_HOST}" -TS_HOST="box.example-net"".ts.net" +TS_HOST="box.example-tailnet"".ts.net" PRIV_IP="192.168"."0.42" NOREPLY="123+tester@users.noreply.github.com" WEB_FLOW="noreply@github.com" @@ -48,9 +51,12 @@ run() { --all) args+=("$1"); shift ;; - --range) + --range|--baseline-file) args+=("$1" "$2"); shift 2 ;; + --print-baseline) + args+=("$1"); shift + ;; *=*) envvars+=("$1"); shift ;; @@ -91,31 +97,31 @@ 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 +commit "personal author" GIT_AUTHOR_EMAIL="$DISALLOWED_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 personal address" "$DISALLOWED_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")" +commit "$(printf 'add collaborator\n\nCo-authored-by: Someone <%s>\n' "$DISALLOWED_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" +refute "trailer value withheld" "$DISALLOWED_ADDR" # 4. Personal Signed-off-by trailer fails. -commit "$(printf 'signoff\n\nSigned-off-by: Someone <%s>\n' "$ICLOUD_ADDR")" +commit "$(printf 'signoff\n\nSigned-off-by: Someone <%s>\n' "$DISALLOWED_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" +refute "signoff value withheld" "$DISALLOWED_ADDR" # 5. Provider VPS hostname as author email fails as private-hostname. commit "vps author" GIT_AUTHOR_EMAIL="$OVH_EMAIL" GIT_AUTHOR_NAME=ubuntu @@ -171,6 +177,102 @@ run "invalid range exits 2" 2 --range "this-is-not-a-rev-xyz" run "full history with dirt fails" 1 --all expect "full history fail line" "FAIL commit metadata" +# --- SHA baseline --- + +write_baseline() { + local dest="${4:-$REPO/.privacy-baseline}" + printf '%s\n' \ + "# test fixture — not a production baseline" \ + "" \ + "sha: $1" \ + "reason: $2" \ + "decided: $3" \ + > "$dest" +} + +# 13. No baseline file: --print-baseline is silent and succeeds. +run "print-baseline without file is silent" 0 --print-baseline +if [[ -n "$LAST_OUTPUT" ]]; then + printf 'FAIL print-baseline without file: expected empty output\n%s\n' "$LAST_OUTPUT" >&2 + failures=$((failures + 1)) +fi + +# 14–16. A valid baseline excludes earlier dirt and is printed as a full SHA. +commit "dirt before baseline" GIT_AUTHOR_EMAIL="$DISALLOWED_ADDR" GIT_COMMITTER_EMAIL="$DISALLOWED_ADDR" +dirty_full="$(git -C "$REPO" rev-parse HEAD)" +commit "clean after baseline" +write_baseline "$dirty_full" "accepted historical test fixture" "2026-11-22" +run "print-baseline emits resolved sha" 0 --print-baseline +if [[ "$LAST_OUTPUT" != "$dirty_full" ]]; then + printf 'FAIL print-baseline emits resolved sha: expected the baseline sha only\n' >&2 + failures=$((failures + 1)) +fi +run "baseline excludes earlier dirt" 0 +expect "baseline auto-range ok" "OK" +run "explicit --all overrides baseline" 1 --all +expect "override still reports fail" "FAIL commit metadata" +refute "override withholds disallowed" "$DISALLOWED_ADDR" + +# 17. Dirt after the baseline is still detected. +commit "dirt after baseline" GIT_AUTHOR_EMAIL="$DISALLOWED_ADDR" GIT_COMMITTER_EMAIL="$DISALLOWED_ADDR" +run "dirt after baseline still fails" 1 +expect "post-baseline finding" "disallowed-email" +refute "post-baseline withheld" "$DISALLOWED_ADDR" + +# 18. Explicit --range still wins, and a present valid file is accepted. +root="$(git -C "$REPO" rev-list --max-parents=0 HEAD)" +run "explicit clean range wins over baseline" 0 --range "$root" +expect "explicit range ok" "OK" + +# 19. Invalid files fail even when a range flag is also given. +write_baseline "$dirty_full" "" "2026-11-22" +run "empty reason exits 2" 2 --print-baseline +expect "empty reason message" "reason is empty" + +write_baseline "$dirty_full" "accepted historical test fixture" "22-11-2026" +run "bad decided date exits 2" 2 --print-baseline +expect "bad decided message" "YYYY-MM-DD" + +printf '%s\n' "reason: accepted historical test fixture" "decided: 2026-11-22" > "$REPO/.privacy-baseline" +run "missing sha exits 2" 2 --print-baseline +expect "missing sha message" "must set sha, reason, and decided" + +write_baseline "not-a-sha" "accepted historical test fixture" "2026-11-22" +run "malformed sha exits 2" 2 --print-baseline +expect "malformed sha message" "not a git SHA" + +write_baseline "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "accepted historical test fixture" "2026-11-22" +run "unresolved sha exits 2" 2 --print-baseline +expect "unresolved sha message" "does not resolve to a commit" + +write_baseline "$dirty_full" "accepted historical test fixture" "2026-11-22" +printf '%s\n' "sha: $dirty_full" >> "$REPO/.privacy-baseline" +run "duplicate key exits 2" 2 --print-baseline +expect "duplicate key message" "duplicate key" + +printf '%s\n' "sha: $dirty_full" "reason: accepted historical test fixture" "decided: 2026-11-22" "owner: nobody" > "$REPO/.privacy-baseline" +run "unknown key exits 2" 2 --print-baseline +expect "unknown key message" "unknown key" + +# A present but invalid baseline must fail even with --all, so CI cannot +# skip validation by setting a range. +printf '%s\n' "sha: not-a-sha" "reason: accepted historical test fixture" "decided: 2026-11-22" > "$REPO/.privacy-baseline" +run "invalid baseline fails with --all" 2 --all +expect "invalid with --all message" "not a git SHA" + +# 20. --baseline-file can point at an alternate path. +rm -f "$REPO/.privacy-baseline" +write_baseline "$dirty_full" "accepted historical test fixture" "2026-11-22" "$REPO/.other-baseline" +run "alternate baseline file prints sha" 0 --print-baseline --baseline-file .other-baseline +if [[ "$LAST_OUTPUT" != "$dirty_full" ]]; then + printf 'FAIL alternate baseline file prints sha: expected the baseline sha only\n' >&2 + failures=$((failures + 1)) +fi +# Without --baseline-file, the default path is absent again, so this is --all +# over dirty history and must fail. +run "missing default baseline falls back to all" 1 +expect "fallback fail line" "FAIL commit metadata" + if [[ "$failures" -ne 0 ]]; then echo "FAILED $failures test(s)" >&2 exit 1 diff --git a/scripts/test-privacy-denylist.sh b/scripts/test-privacy-denylist.sh index c074783..a9f13cd 100755 --- a/scripts/test-privacy-denylist.sh +++ b/scripts/test-privacy-denylist.sh @@ -46,10 +46,12 @@ refute() { fi } -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" +# Assembled at runtime; fragments so the file itself does not contain +# contiguous provider suffixes that extra denylist patterns may match. +TAILNET="box.example-tailnet.ts"".net" +OVH_VPS="v""ps-abc123fe.v""ps.ov""h.net" +HETZNER="node-1.hetz""ner.clo""ud" +HETZNER_DED="static.12.34.56.clients.your-ser""ver.de" plant "Plain documentation." check "clean tree passes" 0 NOOP=1