diff --git a/.githooks/commit-msg b/.githooks/commit-msg new file mode 100755 index 00000000..b223a98a --- /dev/null +++ b/.githooks/commit-msg @@ -0,0 +1,21 @@ +#!/bin/sh +# Blocks commits whose message contains unredacted production identifiers or secrets. +# Enable once per clone: git config core.hooksPath .githooks +patterns="$(dirname "$0")/sensitive-patterns.txt" +msg_file="$1" +if [ ! -f "$patterns" ] || [ -z "$msg_file" ] || [ ! -f "$msg_file" ]; then + exit 0 +fi + +matches="$(grep -nE -f "$patterns" "$msg_file" || true)" +if [ -n "$matches" ]; then + echo "" >&2 + echo "✖ commit-msg: message matches sensitive-content patterns:" >&2 + printf '%s\n' "$matches" | sed 's/^/ /' >&2 + echo "" >&2 + echo "Redact real ids/tokens with placeholders (, , , , )." >&2 + echo "See AGENTS.md § PUBLIC OUTPUT REDACTION. Bypass (rare): git commit --no-verify" >&2 + exit 1 +fi + +exit 0 diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 00000000..59e522e2 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,28 @@ +#!/bin/sh +# Blocks commits whose staged content contains unredacted production identifiers or secrets. +# Enable once per clone: git config core.hooksPath .githooks +# Patterns live in .githooks/sensitive-patterns.txt (patterns only: blank lines would match everything). +patterns="$(dirname "$0")/sensitive-patterns.txt" +if [ ! -f "$patterns" ]; then + echo "pre-commit: $patterns not found, skipping sensitive-content scan" >&2 + exit 0 +fi + +staged="$(git diff --cached --unified=0 --no-color --diff-filter=ACMR)" +if [ -z "$staged" ]; then + exit 0 +fi + +matches="$(printf '%s\n' "$staged" | grep -nE -f "$patterns" || true)" +if [ -n "$matches" ]; then + echo "" >&2 + echo "✖ pre-commit: staged content matches sensitive-content patterns:" >&2 + printf '%s\n' "$matches" | sed 's/^/ /' >&2 + echo "" >&2 + echo "Redact real ids/tokens with placeholders (, , , , )" >&2 + echo "or remove the content before committing. See AGENTS.md § PUBLIC OUTPUT REDACTION." >&2 + echo "Deliberate bypass (rare, review the diff first): git commit --no-verify" >&2 + exit 1 +fi + +exit 0 diff --git a/.githooks/selftest.sh b/.githooks/selftest.sh new file mode 100755 index 00000000..7493a4dc --- /dev/null +++ b/.githooks/selftest.sh @@ -0,0 +1,45 @@ +#!/bin/sh +# Self-test for the shared sensitive-content patterns: leaked shapes must match, +# redacted placeholders must not, and every leaked sample line must be caught. +# Samples are assembled from fragments so this script does not trip the hooks itself. +# Run: sh .githooks/selftest.sh +set -e +dir="$(dirname "$0")" +patterns="$dir/sensitive-patterns.txt" + +if [ ! -f "$patterns" ]; then + echo "selftest FAIL: $patterns missing" + exit 1 +fi + +tmp="$(mktemp -d)" +trap 'rm -rf "$tmp"' EXIT + +{ + printf 'INF LLMAgent %s%s %s started\n' '-100' '1234567890' '' + printf 'bot %s:%s\n' '123456789' 'AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsawX' + printf 'key %s%s\n' 'sk-' 'abcdefghijklmnopqrstuvwxyz123456' + printf 'github %s%s\n' 'ghp_' 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij' + printf 'aws %s%s\n' 'AKIA' 'IOSFODNN7EXAMPLE' +} > "$tmp/leaked.txt" + +cat > "$tmp/clean.txt" <<'EOF' +09:59:38 INF 主进程:LLMAgent 已启动 +bot +key +EOF + +leaked_total=5 +leaked_hits="$(grep -cE -f "$patterns" "$tmp/leaked.txt" || true)" +if [ "$leaked_hits" -ne "$leaked_total" ]; then + echo "selftest FAIL: caught $leaked_hits/$leaked_total leaked samples" + exit 1 +fi + +if grep -qE -f "$patterns" "$tmp/clean.txt"; then + echo "selftest FAIL: redacted sample was flagged" + grep -nE -f "$patterns" "$tmp/clean.txt" + exit 1 +fi + +echo "selftest OK: $leaked_hits/$leaked_total leaked samples caught, redacted sample clean" diff --git a/.githooks/sensitive-patterns.txt b/.githooks/sensitive-patterns.txt new file mode 100644 index 00000000..2f3d8cc6 --- /dev/null +++ b/.githooks/sensitive-patterns.txt @@ -0,0 +1,5 @@ +-100[0-9]{7,12}\b +[0-9]{8,10}:[A-Za-z0-9_-]{35} +sk-[A-Za-z0-9_-]{20,} +gh[pousr]_[A-Za-z0-9]{30,} +AKIA[0-9A-Z]{16} diff --git a/.github/workflows/privacy-guard.yml b/.github/workflows/privacy-guard.yml new file mode 100644 index 00000000..b91fab33 --- /dev/null +++ b/.github/workflows/privacy-guard.yml @@ -0,0 +1,45 @@ +# Fails a PR when its title, body, or commit messages contain unredacted +# production identifiers (Telegram chat ids, bot tokens, API keys). +# Incident: PR #391 quoted a raw production log line including a real group chat id. +name: Privacy Guard + +on: + pull_request: + types: [opened, edited, reopened, synchronize] + +permissions: + contents: read + +jobs: + scan: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Scan PR title/body and commit messages + env: + BASE_REF: ${{ github.base_ref }} + PR_TITLE: ${{ github.event.pull_request.title }} + PR_BODY: ${{ github.event.pull_request.body }} + run: | + set -euo pipefail + patterns=.githooks/sensitive-patterns.txt + if [ ! -f "$patterns" ]; then + echo "::error::Pattern file $patterns is missing; refusing to skip the privacy scan." + exit 1 + fi + + printf '%s\n%s\n' "$PR_TITLE" "$PR_BODY" > /tmp/privacy-scan.txt + git fetch --quiet origin "$BASE_REF" + git log --format=%B "origin/$BASE_REF..HEAD" >> /tmp/privacy-scan.txt + + # Report matched line numbers only: echoing matched content would re-leak it into public CI logs. + matched_lines="$(grep -nE -f "$patterns" /tmp/privacy-scan.txt | cut -d: -f1 | paste -sd, - || true)" + if [ -n "$matched_lines" ]; then + echo "::error::Unredacted identifier or secret found in PR text/commits (input lines: $matched_lines). Replace real ids/tokens with /// placeholders and push/edit the PR." + exit 1 + fi + + echo "No unredacted identifiers detected." diff --git a/AGENTS.md b/AGENTS.md index fc00e1c8..3385544d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -101,6 +101,14 @@ dotnet publish -r win-x64 --self-contained - If work is blocked by missing permissions, an external outage, or ambiguous requirements, state the blocker clearly. - After the workflow is complete, use the `ask_user` tool to ask what to do next. +## PUBLIC OUTPUT REDACTION (HARD RULE) +- Never paste raw production logs, configs, or API responses into public artifacts (PR/issue bodies, comments, commit messages, docs, code comments). +- Redact real identifiers: Telegram chat/user/message ids → `` / `` / ``; task ids and process ids → `` / ``; bot tokens, API keys, cookies, internal hostnames → `` / `` / ``. +- Keep timestamps, log levels, error/exit codes and the shape of the log line; that is what matters for diagnosis and it leaks nothing. +- Full logs live only in local/private storage, never in git history or on GitHub. +- CI enforces this for PR titles/bodies and PR commit messages via `.github/workflows/privacy-guard.yml`. If the guard fails, redact and push/edit the PR; do not bypass it. +- Local git hooks block offending commits before they exist: `.githooks/pre-commit` scans staged content, `.githooks/commit-msg` scans the message, both driven by `.githooks/sensitive-patterns.txt` (same patterns as CI). New clones install them automatically on the first `dotnet build`/`test` via `Directory.Build.targets`; manual fallback: `git config core.hooksPath .githooks`. Do not use `--no-verify` to skip them. + ## Development Notes - **Platform**: Windows primary, Linux partial (OCR/ASR limited) - **Config**: `%LOCALAPPDATA%/TelegramSearchBot/Config.json` - DO NOT use appsettings.json diff --git a/Directory.Build.targets b/Directory.Build.targets new file mode 100644 index 00000000..9d169cf6 --- /dev/null +++ b/Directory.Build.targets @@ -0,0 +1,17 @@ + + + + + +