Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .githooks/commit-msg
Original file line number Diff line number Diff line change
@@ -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 (<chat_id>, <user_id>, <task_id>, <token>, <key>)." >&2
echo "See AGENTS.md § PUBLIC OUTPUT REDACTION. Bypass (rare): git commit --no-verify" >&2
exit 1
fi

exit 0
28 changes: 28 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -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 (<chat_id>, <user_id>, <task_id>, <token>, <key>)" >&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
45 changes: 45 additions & 0 deletions .githooks/selftest.sh
Original file line number Diff line number Diff line change
@@ -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' '<pid>'
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 <chat_id> <pid>已启动
bot <token>
key <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"
5 changes: 5 additions & 0 deletions .githooks/sensitive-patterns.txt
Original file line number Diff line number Diff line change
@@ -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}
45 changes: 45 additions & 0 deletions .github/workflows/privacy-guard.yml
Original file line number Diff line number Diff line change
@@ -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 <chat_id>/<user_id>/<task_id>/<token> placeholders and push/edit the PR."
exit 1
fi

echo "No unredacted identifiers detected."
8 changes: 8 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 → `<chat_id>` / `<user_id>` / `<message_id>`; task ids and process ids → `<task_id>` / `<pid>`; bot tokens, API keys, cookies, internal hostnames → `<token>` / `<key>` / `<host>`.
- 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
Expand Down
17 changes: 17 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project>
<!--
Git has no clone-time hook, so a fresh clone gets the sensitive-content hooks on the
first build (dotnet build/test/IDE build) instead: every project imports this file.
Manual fallback: git config core.hooksPath .githooks
See AGENTS.md § PUBLIC OUTPUT REDACTION.
-->
<Target Name="InstallGitHooks"
BeforeTargets="Build"
Condition="'$(CI)' != 'true' and Exists('$(MSBuildThisFileDirectory).git')">
<Exec Command="git config core.hooksPath .githooks"
WorkingDirectory="$(MSBuildThisFileDirectory)"
ContinueOnError="true"
StandardOutputImportance="low"
StandardErrorImportance="low" />
</Target>
</Project>
Loading