From ef54373e4c613bce2771e4550793713373cc2d29 Mon Sep 17 00:00:00 2001 From: bntvllnt <32437578+bntvllnt@users.noreply.github.com> Date: Sun, 22 Nov 2026 16:27:15 +0100 Subject: [PATCH 1/2] ci: add privacy guard and package content check Add the Privacy Guard required check, a packed-tarball content allowlist, and a denylist scan of npm pack output. --- .github/workflows/privacy-guard.yml | 110 ++++++++++++++++++++++++++++ .github/workflows/publish.yml | 6 ++ .gitignore | 1 + .gitleaks.toml | 10 +++ .privacy-allowlist | 12 +++ .privacy-denylist | 42 +++++++++++ 6 files changed, 181 insertions(+) create mode 100644 .github/workflows/privacy-guard.yml create mode 100644 .gitleaks.toml create mode 100644 .privacy-allowlist create mode 100644 .privacy-denylist diff --git a/.github/workflows/privacy-guard.yml b/.github/workflows/privacy-guard.yml new file mode 100644 index 0000000..8b69f4e --- /dev/null +++ b/.github/workflows/privacy-guard.yml @@ -0,0 +1,110 @@ +# Copyable Privacy Guard workflow for a public repository (or one about to +# become public). Required status check name: "Privacy Guard". +# +# Copy into the consuming repo: +# .github/workflows/privacy-guard.yml (this file) +# scripts/check-commit-emails.sh +# scripts/privacy-denylist.sh +# scripts/test-privacy-denylist.sh +# scripts/test-check-commit-emails.sh +# .privacy-denylist +# .privacy-allowlist +# And optionally: +# .githooks/pre-push (see pre-push in this directory) +# +# 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 +# personal data and no account-specific addresses. + +name: Privacy Guard + +on: + push: + pull_request: + +permissions: + contents: read + +concurrency: + group: privacy-guard-${{ github.head_ref || github.ref }} + cancel-in-progress: true + +jobs: + privacy-guard: + name: Privacy Guard + 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 + 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 + # Pull requests: the commits this PR adds. Push to the default + # branch: the full history. Other pushes: the pushed range. + 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 + - uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 + - uses: actions/setup-node@v4 + with: + node-version-file: .node-version + cache: pnpm + - name: Install and build package + run: | + set -euo pipefail + pnpm install --frozen-lockfile + if node -p "Boolean(require('./package.json').scripts && require('./package.json').scripts.build)" | grep -q true; then + pnpm build + fi + - name: Check package contents + run: bash scripts/check-package-contents.sh diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 3fa8aaa..6445cfd 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -70,6 +70,9 @@ jobs: - name: Build run: pnpm build + - name: Check package contents + working-directory: ${{ github.workspace }} + run: bash scripts/check-package-contents.sh - name: Publish canary run: | sed -i '/_authToken/d' "$NPM_CONFIG_USERCONFIG" @@ -176,6 +179,9 @@ jobs: git tag -a "v${{ steps.version.outputs.version }}" -m "v${{ steps.version.outputs.version }}" git push origin "v${{ steps.version.outputs.version }}" + - name: Check package contents + working-directory: ${{ github.workspace }} + run: bash scripts/check-package-contents.sh - name: Publish to npm run: | sed -i '/_authToken/d' "$NPM_CONFIG_USERCONFIG" diff --git a/.gitignore b/.gitignore index adb96d9..70ae860 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ coverage/ GEMINI.md CONVENTIONS.md .github/copilot-instructions.md +.privacy-denylist.local diff --git a/.gitleaks.toml b/.gitleaks.toml new file mode 100644 index 0000000..422ccf5 --- /dev/null +++ b/.gitleaks.toml @@ -0,0 +1,10 @@ +title = "gitleaks" + +[extend] +useDefault = true + +[[allowlists]] +description = "synthetic test fixtures" +paths = [ + '''tests/roadmap-2-5-e2e.test.ts''', +] diff --git a/.privacy-allowlist b/.privacy-allowlist new file mode 100644 index 0000000..2bedd55 --- /dev/null +++ b/.privacy-allowlist @@ -0,0 +1,12 @@ +# Regexes matched against "path:line:token" of each denylist hit. Keep entries narrow +# and anchor them: a token is allowed only if the whole token matches. + +# Addresses on reserved or noreply domains, and the SSH remote user +:[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 +:[0-9]+:.?/home/(runner|user)$ diff --git a/.privacy-denylist b/.privacy-denylist new file mode 100644 index 0000000..e9cc6da --- /dev/null +++ b/.privacy-denylist @@ -0,0 +1,42 @@ +# Structural privacy patterns (extended regex, case-insensitive). Generic only: +# never put names, clients, hosts, or credentials here; this file is public. +# Private terms go in the untracked .privacy-denylist.local or the +# PRIVACY_DENYLIST_EXTRA CI secret. See scripts/privacy-denylist.sh. + +# Tailnet hostnames and addresses (CGNAT range, IPv6 ULA) +[a-z0-9-]+\.ts\.net\b +\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 +\b172\.(1[6-9]|2[0-9]|3[01])\.[0-9]{1,3}\.[0-9]{1,3}\b + +# Machine-specific home and editor store paths (a path root, not a URL segment) +(^|[^A-Za-z0-9_./-])/(home|Users)/[A-Za-z0-9._-]+ +[A-Za-z]:\\Users\\[A-Za-z0-9._-]+ +/cursor/stores + +# UUIDs (account, bot, and device ids) +\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b + +# Email addresses; a version suffix such as pkg@1.2.3-canary.abc1234 is not one +[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}\b + +# Phone numbers: international, US-style grouped, French-style grouped, 00-prefixed +(^|[^0-9A-Za-z])\+[0-9]{1,3}[ .-]?[0-9]([ .-]?[0-9]){7,12}\b +\(?[0-9]{3}\)?[ .-][0-9]{3}[ .-][0-9]{4}\b +\b0[1-9]([ .-][0-9]{2}){4}\b +\b00[0-9]{10,13}\b + +# Revenue and pricing figures +\bMRR\b[^.]{0,20}[0-9] +\b(revenue|turnover|pricing)\b[^.]{0,24}[$€£][0-9] +[$€£][0-9][0-9,.]*[kKmM]?[ ]?(MRR|ARR)\b +\b[0-9][0-9,.]*[kKmM]?[ ]?(€|EUR\b|USD\b) From 29b70cf07ebcfab663623aa021dfbffbbe511575 Mon Sep 17 00:00:00 2001 From: bntvllnt <32437578+bntvllnt@users.noreply.github.com> Date: Sun, 22 Nov 2026 16:31:58 +0100 Subject: [PATCH 2/2] ci: fix privacy guard rollout follow-ups Allow structural privacy patterns in leak-gate, ship ignored CI scripts, cover compiled fixture addresses in the email package tarball, and record the eslint-config CI addition in the changelog. --- .gitignore | 6 + scripts/check-commit-emails.sh | 187 ++++++++++++++++++++++++ scripts/check-package-contents.sh | 192 +++++++++++++++++++++++++ scripts/privacy-denylist.sh | 139 ++++++++++++++++++ scripts/test-check-commit-emails.sh | 179 +++++++++++++++++++++++ scripts/test-check-package-contents.sh | 111 ++++++++++++++ scripts/test-privacy-denylist.sh | 176 +++++++++++++++++++++++ 7 files changed, 990 insertions(+) create mode 100755 scripts/check-commit-emails.sh create mode 100755 scripts/check-package-contents.sh create mode 100755 scripts/privacy-denylist.sh create mode 100755 scripts/test-check-commit-emails.sh create mode 100755 scripts/test-check-package-contents.sh create mode 100755 scripts/test-privacy-denylist.sh diff --git a/.gitignore b/.gitignore index 70ae860..efdfe91 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,12 @@ next-env.d.ts .vllnt/ test-results/ scripts/ +!scripts/check-commit-emails.sh +!scripts/privacy-denylist.sh +!scripts/test-privacy-denylist.sh +!scripts/test-check-commit-emails.sh +!scripts/check-package-contents.sh +!scripts/test-check-package-contents.sh # analysis + coverage artifacts .codebase-intelligence/ 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/check-package-contents.sh b/scripts/check-package-contents.sh new file mode 100755 index 0000000..10cffca --- /dev/null +++ b/scripts/check-package-contents.sh @@ -0,0 +1,192 @@ +#!/usr/bin/env bash +# +# check-package-contents.sh — fail if `npm pack` would include a path outside +# the allowlist, or if packed contents match the privacy denylist. +# +# Packed file contents and denylist tokens are never printed. +# +# Allowlist: package.json, README, LICENSE, CHANGELOG, NOTICE (and common +# extensions), plus every positive entry in package.json "files", plus optional +# extra regexes in .package-contents-allowlist (matched against the packed +# path). Test files, .env files, and paths matching the denylist still fail. +# +# Usage: check-package-contents.sh [package-dir] +# $PACKAGE_CONTENTS_DIR default package directory when no argument is given +# +# Exit codes: 0 clean, 1 packing or policy failure, 2 misuse. + +set -euo pipefail + +ROOT="$(git rev-parse --show-toplevel)" || exit 2 +PKG_DIR="${1:-${PACKAGE_CONTENTS_DIR:-$ROOT}}" +PKG_DIR="$(cd "$PKG_DIR" && pwd)" || exit 2 + +DENY_SCRIPT="$ROOT/scripts/privacy-denylist.sh" +if [[ ! -f "$DENY_SCRIPT" ]]; then + echo "error: $DENY_SCRIPT is required" >&2 + exit 2 +fi +if [[ ! -f "$PKG_DIR/package.json" ]]; then + echo "error: $PKG_DIR/package.json is missing" >&2 + exit 2 +fi + +tmp="$(mktemp -d)" +trap 'rm -rf "$tmp"' EXIT + +list_packed() { + PKG_PACK_JSON="$1" node -e ' + const fs = require("fs"); + const raw = fs.readFileSync(process.env.PKG_PACK_JSON, "utf8").trim(); + if (!raw) process.exit(2); + let data = JSON.parse(raw); + if (!Array.isArray(data)) data = [data]; + const files = (data[0] && data[0].files) || []; + for (const f of files) { + let p = typeof f === "string" ? f : f.path; + if (!p) continue; + p = p.replace(/^\.?\/*/, ""); + if (p.startsWith("package/")) p = p.slice("package/".length); + console.log(p); + } + ' +} + +files_field_regexes() { + PKG_JSON="$1" node -e ' + const pkg = require(process.env.PKG_JSON); + const entries = Array.isArray(pkg.files) ? pkg.files : []; + const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + for (const entry of entries) { + if (typeof entry !== "string" || entry.startsWith("!")) continue; + const trimmed = entry.replace(/^\.\//, "").replace(/\/+$/, ""); + if (!trimmed) continue; + if (!trimmed.includes("*")) { + console.log("^" + escapeRe(trimmed) + "(/|$)"); + continue; + } + let out = ""; + for (let i = 0; i < trimmed.length; i++) { + if (trimmed[i] === "*" && trimmed[i + 1] === "*") { + out += ".*"; + i++; + } else if (trimmed[i] === "*") { + out += "[^/]*"; + } else { + out += escapeRe(trimmed[i]); + } + } + console.log("^" + out + "$"); + } + ' +} + +{ + printf '%s\n' \ + '^package\.json$' \ + '^README(\.[A-Za-z0-9]+)?$' \ + '^LICEN[CS]E(\.[A-Za-z0-9]+)?$' \ + '^CHANGELOG(\.[A-Za-z0-9]+)?$' \ + '^NOTICE(\.[A-Za-z0-9]+)?$' \ + '^dist(/|$)' + files_field_regexes "$PKG_DIR/package.json" + for f in "$PKG_DIR/.package-contents-allowlist" "$ROOT/.package-contents-allowlist"; do + if [[ -f "$f" ]]; then + tr -d '\r' < "$f" | sed -E '/^[[:space:]]*(#|$)/d' + fi + done +} > "$tmp/allowed.re" + +if [[ ! -s "$tmp/allowed.re" ]]; then + echo "error: package contents allowlist is empty" >&2 + exit 2 +fi + +is_denied_class() { + local p="$1" base="${1##*/}" + case "$base" in + *.test.ts|*.test.tsx|*.test.js|*.test.mjs|*.test.cjs|*.test.mts|*.test.cts|*.test.jsx) + return 0 ;; + *.spec.ts|*.spec.tsx|*.spec.js|*.spec.mjs|*.spec.cjs|*.spec.jsx) + return 0 ;; + .env|.env.*) + return 0 ;; + esac + case "$p" in + __tests__/*|*/__tests__/*) + return 0 ;; + *.env|*/.env|*/.env.*) + return 0 ;; + esac + return 1 +} + +cd "$PKG_DIR" +if ! npm pack --ignore-scripts --dry-run --json > "$tmp/pack.json" 2>"$tmp/pack.err"; then + echo "error: npm pack --dry-run failed" >&2 + exit 1 +fi + +mapfile -t packed < <(list_packed "$tmp/pack.json") +if [[ "${#packed[@]}" -eq 0 ]]; then + echo "error: npm pack reported no files" >&2 + exit 1 +fi + +denied=0 +violations=0 +for path in "${packed[@]}"; do + [[ -n "$path" ]] || continue + if is_denied_class "$path"; then + printf ' DENY %s\n' "$path" + denied=$((denied + 1)) + continue + fi + if ! printf '%s\n' "$path" | grep -qE -f "$tmp/allowed.re"; then + printf ' EXTRA %s\n' "$path" + violations=$((violations + 1)) + fi +done + +if (( denied > 0 || violations > 0 )); then + echo "FAIL package contents: $denied denied path(s), $violations outside allowlist" + exit 1 +fi + +if ! npm pack --ignore-scripts --pack-destination "$tmp" > "$tmp/pack-name.txt" 2>"$tmp/pack-real.err"; then + echo "error: npm pack failed" >&2 + exit 1 +fi +tarball="$(find "$tmp" -maxdepth 1 -name '*.tgz' -print -quit)" +if [[ -z "$tarball" ]]; then + echo "error: npm pack produced no tarball" >&2 + exit 1 +fi + +extract="$tmp/extract" +mkdir -p "$extract" +tar -xzf "$tarball" -C "$extract" +scan_root="$extract/package" +if [[ ! -d "$scan_root" ]]; then + scan_root="$extract" +fi + +mkdir -p "$scan_root/scripts" +cp "$ROOT/.privacy-denylist" "$scan_root/.privacy-denylist" +cp "$ROOT/.privacy-allowlist" "$scan_root/.privacy-allowlist" +cp "$DENY_SCRIPT" "$scan_root/scripts/privacy-denylist.sh" +git -C "$scan_root" init -q +git -C "$scan_root" config user.email test@example.invalid +git -C "$scan_root" config user.name pack-check +git -C "$scan_root" add -A +git -C "$scan_root" commit -q --allow-empty-message --no-gpg-sign -m "pack" >/dev/null + +code=0 +(cd "$scan_root" && bash scripts/privacy-denylist.sh) || code=$? +if [[ "$code" -ne 0 ]]; then + echo "FAIL package contents: packed tarball failed the privacy denylist (exit $code)" + exit 1 +fi + +echo "OK package contents: ${#packed[@]} file(s) in allowlist, denylist clean" +exit 0 diff --git a/scripts/privacy-denylist.sh b/scripts/privacy-denylist.sh new file mode 100755 index 0000000..fc5f658 --- /dev/null +++ b/scripts/privacy-denylist.sh @@ -0,0 +1,139 @@ +#!/usr/bin/env bash +# +# privacy-denylist.sh — fail when a tracked file matches a denylist pattern +# outside the allowlist. +# +# Patterns (extended regex, one per line, '#' comments; matched case-insensitively): +# .privacy-denylist required, tracked, structural patterns only +# .privacy-denylist.local optional, untracked, for names that must stay private +# $PRIVACY_DENYLIST_EXTRA optional, newline-separated; set it from a CI secret +# Allowlist (.privacy-allowlist): regexes matched against "path:line:token" for each +# matched token, so an allowed token never hides another match on the same line. +# +# Matched content and pattern text are never printed: CI logs of a public +# repository are public. +# +# Limits: only tracked files at the checked-out revision are scanned, not the +# commits of a pull request (gitleaks covers secret-shaped data in all history). +# UTF-16 text is not matched, and the allowlist file itself is not scanned. +# +# Exit codes: 0 clean, 1 match found, 2 misuse or invalid pattern. + +set -u + +cd "$(git rev-parse --show-toplevel)" || exit 2 + +DENY=.privacy-denylist +ALLOW=.privacy-allowlist +SELF=scripts/privacy-denylist.sh + +tracked="$(mktemp)" +extra="$(mktemp)" +local_raw="$(mktemp)" +extra_raw="$(mktemp)" +patterns="$(mktemp)" +allow_patterns="$(mktemp)" +hits="$(mktemp)" +kept="$(mktemp)" +probe="$(mktemp -d)" +trap 'rm -f "$tracked" "$extra" "$local_raw" "$extra_raw" "$patterns" "$allow_patterns" "$hits" "$kept"; rm -rf "$probe"' EXIT +printf '\n' > "$probe/empty" +printf 'a b\n' > "$probe/text" + +strip() { tr -d '\r' | sed -E '/^[[:space:]]*(#|$)/d'; } +# lines : the file with a guaranteed final newline. +lines() { cat "$1"; printf '\n'; } + +# Probes run a pattern with the engine and flags that later use it: git grep -iE for +# the denylist scan, grep -E for the allowlist filter. The two dialects differ. +# deny_probe/allow_probe +deny_probe() { (cd "$probe" && git grep --no-index -h -iE "$1" -e "$2" -- "$3" 2> /dev/null); } +allow_probe() { grep -E "$1" -e "$2" -- "$probe/$3" 2> /dev/null; } + +# A backslash before a letter or 0, other than \b \B \s \S \w \W, is a PCRE-style +# escape (\d, \x41, \Q, \p{L}) that extended regex silently misreads. +UNSUPPORTED_ESCAPE='(^|[^\])(\\\\)*\\[AC-RT-VX-Zac-rt-vx-z0]' + +# valid_regexes : every pattern line must compile with its +# engine and must not match the empty string. Errors name the source and line only. +valid_regexes() { + local file="$1" source="$2" probe_with="$3" n=0 line status + while IFS= read -r line || [[ -n "$line" ]]; do + n=$((n + 1)) + line="${line//$'\r'/}" + [[ "$line" =~ ^[[:space:]]*(#|$) ]] && continue + if [[ "$line" =~ $UNSUPPORTED_ESCAPE ]]; then + echo "error: $source pattern line $n uses an escape extended regex does not support" >&2 + return 1 + fi + "$probe_with" -q "$line" empty + status=$? + if (( status > 1 )); then + echo "error: $source pattern line $n is not a valid regex" >&2 + return 1 + fi + if (( status == 0 )); then + echo "error: $source pattern line $n matches the empty string" >&2 + return 1 + fi + # Zero-width patterns (\b, ^, $) match inside real text but report nothing. + if [[ -z "$("$probe_with" -o "$line" text)" ]] && "$probe_with" -q "$line" text; then + echo "error: $source pattern line $n is zero-width" >&2 + return 1 + fi + done < "$file" +} + +if [[ ! -f "$DENY" ]]; then + echo "error: $DENY is required" >&2 + exit 2 +fi +[[ -f "$DENY.local" ]] && cat "$DENY.local" > "$local_raw" +[[ -n "${PRIVACY_DENYLIST_EXTRA:-}" ]] && printf '%s\n' "$PRIVACY_DENYLIST_EXTRA" > "$extra_raw" +lines "$DENY" | strip > "$tracked" +{ lines "$local_raw"; lines "$extra_raw"; } | strip > "$extra" +cat "$tracked" "$extra" > "$patterns" +[[ -f "$ALLOW" ]] && lines "$ALLOW" | strip > "$allow_patterns" + +if [[ ! -s "$tracked" ]]; then + echo "error: $DENY has no patterns" >&2 + exit 2 +fi +valid_regexes "$DENY" "tracked" deny_probe || exit 2 +valid_regexes "$local_raw" "local" deny_probe || exit 2 +valid_regexes "$extra_raw" "extra" deny_probe || exit 2 +[[ -f "$ALLOW" ]] && { valid_regexes "$ALLOW" "allowlist" allow_probe || exit 2; } +if [[ -s "$allow_patterns" ]] && [[ -z "$(printf '%s\n' 'zz/canary.md:1:canary.ts''.net' | grep -avEf "$allow_patterns")" ]]; then + echo "error: allowlist is too broad; it would hide an obvious match" >&2 + exit 2 +fi + +# -a scans binary files as text; -o reports each matched token on its own line. +git grep -naoiE -f "$patterns" -- . ":(exclude)$DENY" ":(exclude)$ALLOW" ":(exclude)$SELF" > "$hits" 2> /dev/null +status=$? +if (( status > 1 )); then + echo "error: git grep failed" >&2 + exit 2 +fi + +if [[ -s "$allow_patterns" ]]; then + grep -avEf "$allow_patterns" "$hits" > "$kept" + status=$? + if (( status > 1 )); then + echo "error: allowlist filtering failed" >&2 + exit 2 + fi + mv "$kept" "$hits" +fi + +if [[ -s "$hits" ]]; then + echo "FAIL privacy denylist: $(wc -l < "$hits" | tr -d ' ') match(es); content withheld" + cut -d: -f1,2 "$hits" | sort -u | sed 's/^/ /' + exit 1 +fi + +extra_count="$(wc -l < "$extra" | tr -d ' ')" +if [[ "${CI:-}" == "true" && "$extra_count" == "0" ]]; then + echo "::warning title=Privacy guard::No private patterns loaded; only the tracked structural patterns were checked." +fi +echo "OK privacy denylist: no matches ($(wc -l < "$tracked" | tr -d ' ') tracked + $extra_count private patterns)" diff --git a/scripts/test-check-commit-emails.sh b/scripts/test-check-commit-emails.sh new file mode 100755 index 0000000..8f5ec65 --- /dev/null +++ b/scripts/test-check-commit-emails.sh @@ -0,0 +1,179 @@ +#!/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 +git -C "$REPO" config commit.gpgsign false + +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-check-package-contents.sh b/scripts/test-check-package-contents.sh new file mode 100755 index 0000000..c0d47f7 --- /dev/null +++ b/scripts/test-check-package-contents.sh @@ -0,0 +1,111 @@ +#!/usr/bin/env bash +# +# test-check-package-contents.sh — success and failure paths of +# check-package-contents.sh in a throwaway package. 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" "$REPO/dist" +cp "$ROOT/scripts/privacy-denylist.sh" "$ROOT/scripts/check-package-contents.sh" "$REPO/scripts/" +cp "$ROOT/.privacy-denylist" "$ROOT/.privacy-allowlist" "$REPO/" +chmod +x "$REPO/scripts/"*.sh + +git -C "$REPO" init -q +git -C "$REPO" config user.email test@example.invalid +git -C "$REPO" config user.name test +git -C "$REPO" config commit.gpgsign false + +failures=0 +LAST_OUTPUT="" +LAST_STATUS=0 + +write_pkg() { + printf '%s\n' "$1" > "$REPO/package.json" +} + +plant_readme() { + printf 'Hello.\n' > "$REPO/README.md" + printf 'MIT\n' > "$REPO/LICENSE" + printf 'export {}\n' > "$REPO/dist/index.js" +} + +commit_tree() { + git -C "$REPO" add -A + git -C "$REPO" commit -q --allow-empty -m "fixture" +} + +run() { + local name="$1" expected="$2" + LAST_OUTPUT="$(cd "$REPO" && bash scripts/check-package-contents.sh 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 + fi + printf 'OK %s\n' "$name" +} + +refute() { + if grep -qF -- "$2" <<< "$LAST_OUTPUT"; then + printf 'FAIL %s\n' "$1" >&2 + failures=$((failures + 1)) + fi +} + +plant_readme +write_pkg '{"name":"fixture-pack","version":"1.0.0","files":["dist"]}' +commit_tree +run "clean dist package passes" 0 + +write_pkg '{"name":"fixture-pack","version":"1.0.0","files":["dist"]}' +printf 'oops\n' > "$REPO/notes.txt" +# notes.txt is not in files, npm should not pack it; still passes +commit_tree +run "unlisted notes.txt is not packed" 0 + +# No files field: npm packs more, notes.txt becomes EXTRA +rm -f "$REPO/notes.txt" +write_pkg '{"name":"fixture-pack","version":"1.0.0"}' +printf 'oops\n' > "$REPO/notes.txt" +commit_tree +run "unallowlisted path in default pack fails" 1 +grep -q 'EXTRA notes.txt' <<< "$LAST_OUTPUT" || { echo "FAIL extra path not reported" >&2; failures=$((failures + 1)); } + +# Test file packed despite files field +rm -f "$REPO/notes.txt" +mkdir -p "$REPO/src" +printf 'export {}\n' > "$REPO/src/index.ts" +printf 'test()\n' > "$REPO/src/index.test.ts" +write_pkg '{"name":"fixture-pack","version":"1.0.0","files":["dist","src"]}' +commit_tree +run "packed test file is denied" 1 +grep -q 'DENY src/index.test.ts' <<< "$LAST_OUTPUT" || { echo "FAIL test path not denied" >&2; failures=$((failures + 1)); } + +# Denylist hit inside dist +rm -rf "$REPO/src" +write_pkg '{"name":"fixture-pack","version":"1.0.0","files":["dist"]}' +# Fixture assembled from fragments +printf '%s\n' "host: box.example-net.ts"".net" > "$REPO/dist/index.js" +commit_tree +run "denylist hit in packed dist fails" 1 +refute "matched hostname leaked to pack-check output" "example-net" +grep -q 'packed tarball failed the privacy denylist' <<< "$LAST_OUTPUT" || { echo "FAIL denylist failure not reported" >&2; failures=$((failures + 1)); } + +# Restore a clean dist and confirm example.com emails in README are allowed +printf 'export {}\n' > "$REPO/dist/index.js" +printf 'Contact admin@example.com\n' > "$REPO/README.md" +commit_tree +run "reserved example.com email in packed README passes" 0 + +if [[ "$failures" -ne 0 ]]; then + echo "FAILED $failures test-check-package-contents case(s)" >&2 + exit 1 +fi +echo "OK test-check-package-contents.sh" +exit 0 diff --git a/scripts/test-privacy-denylist.sh b/scripts/test-privacy-denylist.sh new file mode 100755 index 0000000..c074783 --- /dev/null +++ b/scripts/test-privacy-denylist.sh @@ -0,0 +1,176 @@ +#!/usr/bin/env bash +# +# test-privacy-denylist.sh — success and failure paths of privacy-denylist.sh +# in a throwaway repository. Fixtures are assembled from fragments (adjacent +# quoted strings) so this file never matches the denylist itself. + +set -u + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +REPO="$(mktemp -d)" +trap 'rm -rf "$REPO"' EXIT + +mkdir -p "$REPO/scripts" +cp "$ROOT/scripts/privacy-denylist.sh" "$REPO/scripts/" +cp "$ROOT/.privacy-denylist" "$ROOT/.privacy-allowlist" "$REPO/" +git -C "$REPO" init -q +git -C "$REPO" config user.email test@example.invalid +git -C "$REPO" config user.name test + +failures=0 +LAST_OUTPUT="" + +# plant : write a one-line fixture. +plant() { printf '%s\n' "$1" > "$REPO/doc.md"; } + +# check [VAR=value ...] +check() { + local name="$1" expected="$2"; shift 2 + git -C "$REPO" add -A + local status + LAST_OUTPUT="$(cd "$REPO" && env "$@" bash scripts/privacy-denylist.sh 2>&1)" + status=$? + if [[ "$status" != "$expected" ]]; then + printf 'FAIL %s: expected exit %s, got %s\n%s\n' "$name" "$expected" "$status" "$LAST_OUTPUT" >&2 + failures=$((failures + 1)) + return + fi + printf 'OK %s\n' "$name" +} + +# refute : the last output must not contain the text. +refute() { + if grep -qF -- "$2" <<< "$LAST_OUTPUT"; then + printf 'FAIL %s\n' "$1" >&2 + failures=$((failures + 1)) + 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" + +plant "Plain documentation." +check "clean tree passes" 0 NOOP=1 + +plant "host: $TAILNET" +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 + +plant "path /ho""me/someone/project/" +check "home path fails" 1 NOOP=1 +plant "HOME=/ho""me/someone" +check "home path without trailing slash fails" 1 NOOP=1 +plant "GET /api/us""ers/123/posts" +check "url route segment passes" 0 NOOP=1 +plant "cwd /ho""me/runner/work/x" +check "allowlisted runner home passes" 0 NOOP=1 + +plant "mail fixture@example.test" +check "reserved-domain address passes" 0 NOOP=1 +plant "mail someone@""company.io" +check "real-looking address fails" 1 NOOP=1 +plant "mail fixture@acme.test"".com" +check "reserved label inside a real domain fails" 1 NOOP=1 +plant "git clone git@github.com:owner/repo.git" +check "ssh remote user passes" 0 NOOP=1 +plant "pnpm add @scope/pkg@0.1.0-canary.bca5ab7" +check "package version pin passes" 0 NOOP=1 + +plant "call 415-555-""0132" +check "grouped phone number fails" 1 NOOP=1 +plant "MR""R is 12k" +check "bare revenue figure fails" 1 NOOP=1 + +plant "ssh admin@example.com $TAILNET" +check "allowed token does not hide another match on the line" 1 NOOP=1 + +printf '\0host %s\n' "$TAILNET" > "$REPO/doc.md" +check "binary file is scanned" 1 NOOP=1 +rm -f "$REPO/doc.md" +plant "Plain documentation." + +printf 'acme-widget appears here\n' >> "$REPO/doc.md" +check "extra pattern from environment fails" 1 'PRIVACY_DENYLIST_EXTRA=acme-widget' +check "extra pattern is not echoed" 1 'PRIVACY_DENYLIST_EXTRA=acme-widget' +refute "extra pattern text leaked to output" "acme-widget" +plant "Plain documentation." +check "extra pattern not present passes" 0 'PRIVACY_DENYLIST_EXTRA=acme-widget' +check "missing private patterns warn under CI" 0 CI=true +grep -q '::warning' <<< "$LAST_OUTPUT" || { echo "FAIL no warning when private patterns are absent in CI" >&2; failures=$((failures + 1)); } + +check "invalid private pattern is an error" 2 'PRIVACY_DENYLIST_EXTRA=Acme[Corp' +refute "invalid pattern text leaked to output" "Acme" +check "private pattern matching empty string is an error" 2 'PRIVACY_DENYLIST_EXTRA=acme|' + +# Constructs that grep -E accepts but git grep -E rejects or silently misreads. +for construct in '(?i)acme' 'acme(?=x)' 'acme(?!x)' '\p{L}acme' 'a{32768}' '\d{3}acme' '\x41acme' '\Qacme\E'; do + check "unsupported construct $construct is an error" 2 "PRIVACY_DENYLIST_EXTRA=$construct" + refute "unsupported construct $construct leaked to output" "acme" + refute "unsupported construct $construct passed validation" "git grep failed" +done +check "invalid private pattern reports its source and line" 2 "PRIVACY_DENYLIST_EXTRA=$(printf 'foo\n\n(?i)acme')" +grep -q 'extra pattern line 3' <<< "$LAST_OUTPUT" || { echo "FAIL invalid pattern source or line not reported" >&2; failures=$((failures + 1)); } +check "escaped backslash before a letter is a literal" 0 'PRIVACY_DENYLIST_EXTRA=acme\\d' +check "supported escapes and alternation pass" 0 'PRIVACY_DENYLIST_EXTRA=\bacme\s+(widget|gadget)\b' + +printf 'acme-widget appears here\n' >> "$REPO/doc.md" +check "crlf and blank private lines are normalized" 1 "PRIVACY_DENYLIST_EXTRA=$(printf 'foo\r\n\r\n\nacme-widget\r\n')" +plant "Plain documentation." + +cp "$REPO/.privacy-allowlist" "$REPO/allow.bak" +plant "host: $TAILNET" +printf '[unclosed\n' > "$REPO/.privacy-allowlist" +check "invalid allowlist regex is an error, not a pass" 2 NOOP=1 +printf 'zzz|\n' > "$REPO/.privacy-allowlist" +check "allowlist matching empty string is an error, not a pass" 2 NOOP=1 +cp "$REPO/allow.bak" "$REPO/.privacy-allowlist" + +plant "host: $TAILNET" +printf '.*' > "$REPO/.privacy-allowlist" +check "unterminated last allowlist line is still validated" 2 NOOP=1 +printf '^.+$\n' > "$REPO/.privacy-allowlist" +check "over-broad allowlist entry is an error, not a pass" 2 NOOP=1 +mv "$REPO/allow.bak" "$REPO/.privacy-allowlist" + +check "zero-width private pattern is an error" 2 'PRIVACY_DENYLIST_EXTRA=\b' +printf '\\b' > "$REPO/.privacy-denylist.local" +check "unterminated zero-width local pattern is an error" 2 NOOP=1 +rm "$REPO/.privacy-denylist.local" + +printf '\0/ho''me/someone\n' > "$REPO/doc.md" +check "nul-delimited token is not dropped by the allowlist filter" 1 NOOP=1 + +printf 'acme' > "$REPO/.privacy-denylist.local" +plant "only foo here" +check "local file and secret patterns stay separate" 1 PRIVACY_DENYLIST_EXTRA=foo +rm "$REPO/.privacy-denylist.local" + +rm "$REPO/.privacy-denylist" +check "missing denylist file is an error" 2 NOOP=1 + +if (( failures > 0 )); then + printf '%s failure(s)\n' "$failures" >&2 + exit 1 +fi +echo "OK privacy denylist tests passed"