Add gitleaks pre-receive installer
This commit is contained in:
commit
1c49ad6fb3
3 files changed
+157
No files matched your search
@@ -0,0 +1,9 @@
|
|||||||
|
# gitea-hygiene
|
||||||
|
|
||||||
|
Server-side secret scanning for Gitea at softeryx.xyz.
|
||||||
|
|
||||||
|
Install on the Gitea server:
|
||||||
|
|
||||||
|
curl -fsSL https://softeryx.xyz/joseph/gitea-hygiene/raw/branch/main/install.sh | sudo bash
|
||||||
|
|
||||||
|
Rejects pushes that add secrets (gitleaks, values redacted in the error). Pull-mirror syncs are not affected.
|
||||||
+73
@@ -0,0 +1,73 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Installs a gitleaks pre-receive hook on every Gitea repo (current and future).
|
||||||
|
# Gitea has no global hooks dir, so a systemd timer symlinks the hook into each
|
||||||
|
# repo's hooks/pre-receive.d every 5 minutes. Pull-mirror syncs never run it.
|
||||||
|
set -euo pipefail
|
||||||
|
[ "$(id -u)" = 0 ] || { echo "Run with sudo." >&2; exit 1; }
|
||||||
|
|
||||||
|
BASE="https://softeryx.xyz/joseph/gitea-hygiene/raw/branch/main"
|
||||||
|
VERSION=8.30.1
|
||||||
|
APP_INI="${APP_INI:-/etc/gitea/app.ini}"
|
||||||
|
|
||||||
|
case "$(uname -m)" in
|
||||||
|
x86_64) ARCH=x64 ;;
|
||||||
|
aarch64|arm64) ARCH=arm64 ;;
|
||||||
|
*) echo "Unsupported arch $(uname -m)" >&2; exit 1 ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# 1. gitleaks binary, checksum-verified
|
||||||
|
TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT
|
||||||
|
ASSET="gitleaks_${VERSION}_linux_${ARCH}.tar.gz"
|
||||||
|
curl -fsSL -o "$TMP/$ASSET" "https://github.com/gitleaks/gitleaks/releases/download/v${VERSION}/${ASSET}"
|
||||||
|
curl -fsSL -o "$TMP/sums.txt" "https://github.com/gitleaks/gitleaks/releases/download/v${VERSION}/gitleaks_${VERSION}_checksums.txt"
|
||||||
|
( cd "$TMP" && grep " ${ASSET}\$" sums.txt | sha256sum -c - )
|
||||||
|
tar -xzf "$TMP/$ASSET" -C "$TMP" gitleaks
|
||||||
|
install -o root -g root -m 755 "$TMP/gitleaks" /usr/local/bin/gitleaks
|
||||||
|
echo "Installed $(/usr/local/bin/gitleaks version)"
|
||||||
|
|
||||||
|
# 2. The hook itself, in one shared place
|
||||||
|
install -d -o root -g root -m 755 /usr/local/lib/gitea-hooks/pre-receive.d
|
||||||
|
curl -fsSL -o "$TMP/hook" "$BASE/pre-receive.d/gitleaks"
|
||||||
|
install -o root -g git -m 750 "$TMP/hook" /usr/local/lib/gitea-hooks/pre-receive.d/gitleaks
|
||||||
|
|
||||||
|
# 3. Find the repository root from app.ini
|
||||||
|
ROOT="$(awk -F= '/^\[/{s=$0} s=="[repository]" && $1~/^[ \t]*ROOT[ \t]*$/{gsub(/^[ \t]+|[ \t]+$/,"",$2); print $2}' "$APP_INI" 2>/dev/null || true)"
|
||||||
|
ROOT="${ROOT:-/var/lib/gitea/data/gitea-repositories}"
|
||||||
|
[ -d "$ROOT" ] || { echo "Repo root $ROOT not found; set APP_INI or edit /usr/local/sbin/gitea-hooks-sync" >&2; exit 1; }
|
||||||
|
|
||||||
|
# 4. Sync script + timer
|
||||||
|
cat > /usr/local/sbin/gitea-hooks-sync <<SYNC
|
||||||
|
#!/bin/bash
|
||||||
|
# Symlink shared hooks into every Gitea repo.
|
||||||
|
n=0
|
||||||
|
for hd in "$ROOT"/*/*.git/hooks/pre-receive.d; do
|
||||||
|
[ -d "\$hd" ] || continue
|
||||||
|
for h in /usr/local/lib/gitea-hooks/pre-receive.d/*; do
|
||||||
|
ln -sfn "\$h" "\$hd/\$(basename "\$h")"
|
||||||
|
done
|
||||||
|
n=\$((n+1))
|
||||||
|
done
|
||||||
|
echo "Linked hooks into \$n repos"
|
||||||
|
SYNC
|
||||||
|
chmod 755 /usr/local/sbin/gitea-hooks-sync
|
||||||
|
|
||||||
|
cat > /etc/systemd/system/gitea-hooks-sync.service <<'UNIT'
|
||||||
|
[Unit]
|
||||||
|
Description=Link shared pre-receive hooks into Gitea repos
|
||||||
|
[Service]
|
||||||
|
Type=oneshot
|
||||||
|
ExecStart=/usr/local/sbin/gitea-hooks-sync
|
||||||
|
UNIT
|
||||||
|
cat > /etc/systemd/system/gitea-hooks-sync.timer <<'UNIT'
|
||||||
|
[Unit]
|
||||||
|
Description=Keep Gitea shared hooks linked
|
||||||
|
[Timer]
|
||||||
|
OnBootSec=1min
|
||||||
|
OnUnitActiveSec=5min
|
||||||
|
[Install]
|
||||||
|
WantedBy=timers.target
|
||||||
|
UNIT
|
||||||
|
systemctl daemon-reload
|
||||||
|
systemctl enable --now gitea-hooks-sync.timer >/dev/null
|
||||||
|
/usr/local/sbin/gitea-hooks-sync
|
||||||
|
echo "Done. To turn it off: sudo systemctl disable --now gitea-hooks-sync.timer && sudo rm /usr/local/lib/gitea-hooks/pre-receive.d/gitleaks"
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Gitea server-side pre-receive hook: reject pushes that introduce secrets.
|
||||||
|
#
|
||||||
|
# This runs only on client pushes (HTTP/SSH), not on pull-mirror sync
|
||||||
|
# (those use git fetch and bypass pre-receive). Safe to install globally.
|
||||||
|
#
|
||||||
|
# Install: see /workspace/gitea/hygiene/INSTALL.md
|
||||||
|
set -u
|
||||||
|
|
||||||
|
GITLEAKS="${GITLEAKS_BIN:-/usr/local/bin/gitleaks}"
|
||||||
|
ZERO="0000000000000000000000000000000000000000"
|
||||||
|
|
||||||
|
if [ ! -x "$GITLEAKS" ]; then
|
||||||
|
echo "gitleaks is not installed at $GITLEAKS; refusing push." >&2
|
||||||
|
echo "Install gitleaks or temporarily move this hook aside." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# GIT_DIR is set by Gitea/git to the bare repository being pushed to.
|
||||||
|
SOURCE="${GIT_DIR:-.}"
|
||||||
|
failed=0
|
||||||
|
|
||||||
|
while read -r oldrev newrev refname; do
|
||||||
|
[ -n "${newrev:-}" ] || continue
|
||||||
|
|
||||||
|
# Branch or tag deletion: nothing new to scan.
|
||||||
|
if [ "$newrev" = "$ZERO" ]; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$oldrev" = "$ZERO" ]; then
|
||||||
|
# New ref: scan the tip (and reachable history). --log-opts limits
|
||||||
|
# the walk; for a new branch this may include earlier commits already
|
||||||
|
# on other refs, which is conservative and correct.
|
||||||
|
log_opts="$newrev"
|
||||||
|
else
|
||||||
|
log_opts="$oldrev..$newrev"
|
||||||
|
fi
|
||||||
|
|
||||||
|
report="$(mktemp)"
|
||||||
|
if ! "$GITLEAKS" detect \
|
||||||
|
--source "$SOURCE" \
|
||||||
|
--log-opts="$log_opts" \
|
||||||
|
--redact \
|
||||||
|
--no-banner \
|
||||||
|
--report-format json \
|
||||||
|
--report-path "$report" \
|
||||||
|
--exit-code 1 >/dev/null 2>&1; then
|
||||||
|
echo "Push rejected: gitleaks found secrets in ${refname}." >&2
|
||||||
|
echo "Rebase or filter the commits, remove the secrets, and try again." >&2
|
||||||
|
echo "Rule / file summary (secret values redacted):" >&2
|
||||||
|
# Print only rule + file + line, never the secret itself.
|
||||||
|
python3 - "$report" << 'PY' >&2 || true
|
||||||
|
import json, sys
|
||||||
|
path = sys.argv[1]
|
||||||
|
try:
|
||||||
|
with open(path) as f:
|
||||||
|
data = json.load(f)
|
||||||
|
except Exception:
|
||||||
|
sys.exit(0)
|
||||||
|
if not data:
|
||||||
|
sys.exit(0)
|
||||||
|
for item in data:
|
||||||
|
rule = item.get("RuleID") or item.get("Rule") or "unknown-rule"
|
||||||
|
file = item.get("File") or item.get("FilePath") or "unknown-file"
|
||||||
|
line = item.get("StartLine") or item.get("StartLineNumber") or "?"
|
||||||
|
commit = (item.get("Commit") or "")[:12]
|
||||||
|
print(f" - {rule}: {file}:{line} commit {commit}")
|
||||||
|
PY
|
||||||
|
failed=1
|
||||||
|
fi
|
||||||
|
rm -f "$report"
|
||||||
|
done
|
||||||
|
|
||||||
|
exit "$failed"
|
||||||
Reference in new issue
Block a user