From 5e16a59a12618a44f2b7c42655269784b445c0c7 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Wed, 16 Sep 2026 10:50:53 +0800 Subject: [PATCH 1/2] chore(ci): guard PR text and commits against unredacted identifiers A recent PR body quoted a raw production log line, exposing a real Telegram group chat id. Add two layers of prevention: - AGENTS.md: hard rule that public artifacts (PR/issue bodies, commit messages, docs, comments) must redact real ids/tokens and never quote raw production logs. - privacy-guard workflow: scans PR title, body and commits for Telegram chat ids, bot tokens, OpenAI/GitHub/AWS key shapes and fails the check on any unredacted match. Re-runs on PR edit so a redacted body clears the check. --- .github/workflows/privacy-guard.yml | 37 +++++++++++++++++++++++++++++ AGENTS.md | 7 ++++++ 2 files changed, 44 insertions(+) create mode 100644 .github/workflows/privacy-guard.yml diff --git a/.github/workflows/privacy-guard.yml b/.github/workflows/privacy-guard.yml new file mode 100644 index 00000000..c413a995 --- /dev/null +++ b/.github/workflows/privacy-guard.yml @@ -0,0 +1,37 @@ +# 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 + 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 + + if grep -nE -- '-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}' /tmp/privacy-scan.txt; then + echo "::error::Unredacted identifier or secret found above. 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..b8b253cc 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -101,6 +101,13 @@ 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. + ## Development Notes - **Platform**: Windows primary, Linux partial (OCR/ASR limited) - **Config**: `%LOCALAPPDATA%/TelegramSearchBot/Config.json` - DO NOT use appsettings.json From 7533d689802ea1faf82d2289fe9d79f7e0fee716 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Wed, 16 Sep 2026 10:58:36 +0800 Subject: [PATCH 2/2] chore(ci): install git hooks on build and share patterns with CI - .githooks/pre-commit + .githooks/commit-msg: block commits whose staged content or message matches sensitive patterns (// shapes) - .githooks/sensitive-patterns.txt: single pattern source for hooks and CI - .githooks/selftest.sh: positive/negative samples assembled from fragments so the test data itself does not trip the hooks - Directory.Build.targets: first build in a fresh clone runs 'git config core.hooksPath .githooks' (skipped when CI=true) - privacy-guard workflow: read the shared pattern file, and report only matched line numbers so a detected value is never echoed into public CI logs - AGENTS.md: document the hooks and their automatic installation --- .githooks/commit-msg | 21 ++++++++++++++ .githooks/pre-commit | 28 ++++++++++++++++++ .githooks/selftest.sh | 45 +++++++++++++++++++++++++++++ .githooks/sensitive-patterns.txt | 5 ++++ .github/workflows/privacy-guard.yml | 12 ++++++-- AGENTS.md | 1 + Directory.Build.targets | 17 +++++++++++ 7 files changed, 127 insertions(+), 2 deletions(-) create mode 100755 .githooks/commit-msg create mode 100755 .githooks/pre-commit create mode 100755 .githooks/selftest.sh create mode 100644 .githooks/sensitive-patterns.txt create mode 100644 Directory.Build.targets 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 index c413a995..b91fab33 100644 --- a/.github/workflows/privacy-guard.yml +++ b/.github/workflows/privacy-guard.yml @@ -25,12 +25,20 @@ jobs: 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 - if grep -nE -- '-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}' /tmp/privacy-scan.txt; then - echo "::error::Unredacted identifier or secret found above. Replace real ids/tokens with /// placeholders and push/edit the PR." + # 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 diff --git a/AGENTS.md b/AGENTS.md index b8b253cc..3385544d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -107,6 +107,7 @@ dotnet publish -r win-x64 --self-contained - 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) 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 @@ + + + + + +