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 .betterleaks.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Betterleaks configuration (gitleaks-compatible format).
# The default rules apply everywhere except the throwaway fixtures listed here.
# A verified false positive outside these paths goes in .gitleaksignore as
# commit:file:rule:line, with a comment saying why it is not a credential.

[extend]
useDefault = true

[allowlist]
description = "Test fixtures and lock files"
paths = [
'''(^|/)testdata/''',
'''_test\.go$''',
'''(^|/)__tests__/''',
'''\.test\.[jt]sx?$''',
'''(^|/)(go\.sum|package-lock\.json|pnpm-lock\.yaml|yarn\.lock)$''',
]
# Obvious placeholders in docs and examples (matched against the secret only).
regexes = [
'''(?i)^(your[-_ ]|<|\$\{|\$\(|example|changeme|placeholder|x{4,})''',
]
53 changes: 53 additions & 0 deletions .github/scripts/check-private-keys.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
# Fails when a file a change adds or modifies holds private key material.
# check-private-keys.sh <base> <head> files changed between two revisions (CI)
# check-private-keys.sh --staged files staged for commit (pre-commit hook)
# Flagged: a PEM/OpenSSH private key block (header followed by a key body line, so
# a placeholder or a test string that merely names the header passes), key and
# keystore files (.key .p12 .pfx .jks .keystore, id_rsa-style names), and anything
# under data/ (runtime state: the sensor CA key lived there once).
# Allowed: throwaway keys under a testdata/ directory.
# Prints only path and reason, never content.
set -euo pipefail

if [[ "${1:-}" == "--staged" ]]; then
mode=staged
mapfile -d '' files < <(git diff --cached --name-only -z --diff-filter=ACMR)
show() { git show ":$1"; }
elif [[ $# -eq 2 && -n "$1" && -n "$2" && ! "$1" =~ ^0+$ ]]; then
mode=range; head_rev="$2"
mapfile -d '' files < <(git diff --name-only -z --diff-filter=ACMR "$1" "$2")
show() { git show "${head_rev}:$1"; }
else
echo "check-private-keys: no revision range (push to a new branch or schedule), skipped"
exit 0
fi

pem='-----BEGIN [A-Z ]*PRIVATE KEY-----\r?\n[A-Za-z0-9+/=:, ._-]{16,}'
bad=0
flag() { echo "private-key guard: $1: $2" >&2; bad=1; }

for f in "${files[@]}"; do
[[ -z "$f" ]] && continue
case "/$f" in */testdata/*) continue ;; esac
base="${f##*/}"
lower="${base,,}"
case "$lower" in
*.key|*.p12|*.pfx|*.jks|*.keystore|id_rsa|id_dsa|id_ecdsa|id_ed25519)
flag "$f" "key or keystore file"; continue ;;
esac
case "$f" in data/*) flag "$f" "files under data/ are runtime state, never committed"; continue ;; esac
if show "$f" 2>/dev/null | grep -Pzq -- "$pem"; then
flag "$f" "contains a private key block"
fi
done

if [[ "$bad" -ne 0 ]]; then
{
echo "A private key must never be committed. Remove it, rotate the key if it was ever real,"
echo "and load it from a file path, secret store or volume at run time."
echo "Throwaway test keys belong under a testdata/ directory."
} >&2
exit 1
fi
echo "private-key guard: ok ($mode, ${#files[@]} file(s))"
91 changes: 91 additions & 0 deletions .github/workflows/secret-scan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Secret Scan

# Keeps credentials and private keys out of this repository.
#
# - Private keys: fails when a change adds or modifies a private key block, a key
# or keystore file, or anything under data/ (throwaway keys under testdata/ pass).
# - Betterleaks: a pull request scans only the commits it adds and fails on a
# finding. A push to the default branch and the weekly schedule scan the full
# history, so a key that slipped in some other way is reported within a week.
# Verified false positives go in .gitleaksignore (commit:file:rule:line).
#
# Findings are printed redacted; nothing is uploaded anywhere.

on:
push:
branches: [main]
pull_request:
merge_group:
types: [checks_requested]
schedule:
- cron: '17 3 * * 1'
workflow_dispatch:

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
secret-scan:
name: Secret Scan
runs-on: ubuntu-latest
timeout-minutes: 15
env:
BETTERLEAKS_VERSION: 1.9.0
BETTERLEAKS_SHA256: f8b185a39ffcece2a1ca82bf3a4e7435cd81963ffd16b7a9128daf75f35f6de7
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
persist-credentials: false

- name: Private keys
env:
EVENT: ${{ github.event_name }}
PR_BASE: ${{ github.event.pull_request.base.sha }}
PR_HEAD: ${{ github.event.pull_request.head.sha }}
MG_BASE: ${{ github.event.merge_group.base_sha }}
MG_HEAD: ${{ github.event.merge_group.head_sha }}
PUSH_BEFORE: ${{ github.event.before }}
PUSH_AFTER: ${{ github.sha }}
run: |
case "$EVENT" in
pull_request) bash .github/scripts/check-private-keys.sh "$PR_BASE" "$PR_HEAD" ;;
merge_group) bash .github/scripts/check-private-keys.sh "$MG_BASE" "$MG_HEAD" ;;
push) bash .github/scripts/check-private-keys.sh "$PUSH_BEFORE" "$PUSH_AFTER" ;;
*) bash .github/scripts/check-private-keys.sh "" "" ;;
esac

- name: Install Betterleaks (pinned, checksum-verified)
run: |
curl -sSfL --retry 5 --retry-all-errors --retry-delay 5 -o betterleaks.tgz \
"https://github.com/betterleaks/betterleaks/releases/download/v${BETTERLEAKS_VERSION}/betterleaks_${BETTERLEAKS_VERSION}_linux_x64.tar.gz"
echo "${BETTERLEAKS_SHA256} betterleaks.tgz" | sha256sum -c -
tar -xzf betterleaks.tgz betterleaks

- name: Betterleaks
env:
EVENT: ${{ github.event_name }}
PR_BASE: ${{ github.event.pull_request.base.sha }}
MG_BASE: ${{ github.event.merge_group.base_sha }}
MG_HEAD: ${{ github.event.merge_group.head_sha }}
run: |
case "$EVENT" in
pull_request) range="${PR_BASE}..HEAD" ;;
merge_group) range="${MG_BASE}..${MG_HEAD}" ;;
*) range="" ;;
esac
args=()
if [ -n "$range" ]; then
echo "Scanning the $(git rev-list --count "$range") commit(s) in $range"
args=(--log-opts "$range")
else
echo "Scanning the full history ($(git rev-list --count --all) commits)"
args=(--log-opts "--all")
fi
./betterleaks git --config .betterleaks.toml --redact --no-banner \
--exit-code 1 "${args[@]}" .
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@
/prev/
/httpcache/
*.key

# Secrets and runtime state: never committed
.env
.env.*
!.env.example
*.pem
*.p12
*.pfx
/data/
Loading