From 1c49ad6fb330d395b0b9830d3c053d068d0c24cb Mon Sep 17 00:00:00 2001 From: authentik Default Admin <1+joseph@noreply.softeryx.xyz> Date: Sun, 29 Nov 2026 16:28:21 -0500 Subject: [PATCH] Add gitleaks pre-receive installer --- README.md | 9 +++++ install.sh | 73 ++++++++++++++++++++++++++++++++++++++++ pre-receive.d/gitleaks | 75 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 README.md create mode 100644 install.sh create mode 100644 pre-receive.d/gitleaks diff --git a/README.md b/README.md new file mode 100644 index 0000000..2a1bb02 --- /dev/null +++ b/README.md @@ -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. diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..01047f9 --- /dev/null +++ b/install.sh @@ -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 < /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" diff --git a/pre-receive.d/gitleaks b/pre-receive.d/gitleaks new file mode 100644 index 0000000..eafa270 --- /dev/null +++ b/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"