askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
pre-push · 2.1 KB · 44 lines
| 1 | #!/usr/bin/env bash |
| 2 | # Pre-push checks. Enable once per clone: git config core.hooksPath .githooks (bypass in an emergency: git push --no-verify) |
| 3 | # |
| 4 | # 1. no key-shaped secrets in the lines being pushed |
| 5 | # 2. type check, then the unit tests (which include the "no key is ever committed" test) |
| 6 | # 3. an optional extra check kept outside the repository, if this clone has one: .git/hooks/pre-push.local |
| 7 | # |
| 8 | # PREPUSH_ONLY=secrets runs only step 1 (used by the hook's own test). |
| 9 | set -euo pipefail |
| 10 | zero=0000000000000000000000000000000000000000 |
| 11 | root="$(git rev-parse --show-toplevel)" |
| 12 | gitdir="$(git rev-parse --git-dir)" |
| 13 | input="$(cat)" # lines: <local ref> <local sha> <remote ref> <remote sha> |
| 14 | |
| 15 | # High-confidence shapes only, so a failure means something. (Provider keys, cloud keys, tokens, private key blocks.) |
| 16 | pattern='sk-ant-[A-Za-z0-9_-]{20,}|sk-(proj-)?[A-Za-z0-9_-]{32,}|AKIA[0-9A-Z]{16}|gh[pousr]_[A-Za-z0-9]{30,}|xox[baprs]-[A-Za-z0-9-]{10,}|-----BEGIN [A-Z ]*PRIVATE KEY' |
| 17 | |
| 18 | added_lines() { # added lines of every commit being pushed |
| 19 | while read -r _lref lsha _rref rsha; do |
| 20 | [ -z "${lsha:-}" ] && continue |
| 21 | [ "$lsha" = "$zero" ] && continue # deleting a ref |
| 22 | if [ "$rsha" = "$zero" ]; then range="$lsha --not --remotes"; else range="$rsha..$lsha"; fi |
| 23 | # shellcheck disable=SC2086 |
| 24 | git -C "$root" log $range -p --no-color --format= | grep -E '^\+' | grep -vE '^\+\+\+' || true |
| 25 | done <<<"$input" |
| 26 | } |
| 27 | |
| 28 | hits="$(added_lines | grep -E "$pattern" || true)" |
| 29 | if [ -n "$hits" ]; then |
| 30 | echo "pre-push: refusing to push. These added lines look like secrets:" >&2 |
| 31 | echo "$hits" | sed -E 's/(.{6}).{0,}/\1…(hidden)/' | head -5 >&2 |
| 32 | exit 1 |
| 33 | fi |
| 34 | echo "pre-push: no key-shaped secrets in the pushed lines" |
| 35 | [ "${PREPUSH_ONLY:-}" = "secrets" ] && exit 0 |
| 36 | |
| 37 | ( cd "$root" && npm run --silent typecheck && npm test --silent ) || { echo "pre-push: type check or unit tests failed" >&2; exit 1; } |
| 38 | echo "pre-push: type check and unit tests pass" |
| 39 | |
| 40 | local_extra="$gitdir/hooks/pre-push.local" |
| 41 | if [ -x "$local_extra" ]; then |
| 42 | echo "pre-push: running this clone's extra check" |
| 43 | printf '%s\n' "$input" | "$local_extra" |
| 44 | fi |