SPB Git

Repository Public

install-hooks.sh 4.7 KB 122 lines shellscript

1#!/usr/bin/env bash2# =============================================================================3#                  SP BOUCHER | SOFTWARE & RESEARCH4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project          : QYVORA8# File             : security/audit/install-hooks.sh9# Module           : Security10# Repository       : https://git.spboucher.ai/qyvora11# Project Website  : Not specified12# Created          : 2026-12-0413# Last Modified    : 2026-12-0414#15# Author           : Simon-Pierre Boucher16# Position         : Professor of Finance17# Institution      : Université du Québec en Outaouais (UQO)18# Organization     : SP Boucher19# Website          : https://spboucher.ai20# Git              : https://git.spboucher.ai21#22# Preferred Contact: contact@spboucher.ai23# Development      : dev@spboucher.ai24# Support          : support@spboucher.ai25# Security/Admin   : admin@spboucher.ai26#27# Copyright (c) 202628# Simon-Pierre Boucher29# All rights reserved where applicable. This file is governed by the30# applicable project license, copyright notices and relevant law.31# Existing third-party rights and notices remain unaffected.32#33# License          : None specified (no license file in this repository)34#35# Do not embed credentials, private keys, access tokens,36# certificates or confidential personal information.37#38# =============================================================================39# END OF SP BOUCHER SOURCE HEADER40# =============================================================================4142# Installs the Git pre-push hook that runs the quick security audit before a push.43#44#   security/audit/install-hooks.sh            install (keeps an existing hook as pre-push.local and chains to it)45#   security/audit/install-hooks.sh --remove   remove the hook installed by this script46#47# The hook blocks the push when the release gate of the quick audit fails. To push anyway in an emergency:48#   QY_SKIP_AUDIT=1 git push        (the skip is printed; use `git push --no-verify` to skip every hook)49# The hook runs only when the push contains changes under Sources/, Tests/, App/, security/ or Package.swift.50# QY_HOOK_AUDIT_ARGS replaces the arguments of the audit (default: --quick --no-report).5152set -euo pipefail53cd "$(dirname "$0")/../.."5455HOOKS_DIR="$(git rev-parse --git-path hooks)"56HOOK="$HOOKS_DIR/pre-push"57MARK="# qyvora-security-audit-hook"5859if [ "${1:-}" = "--remove" ]; then60    if [ -f "$HOOK" ] && grep -q "$MARK" "$HOOK"; then61        rm -f "$HOOK"62        [ -f "$HOOK.local" ] && mv "$HOOK.local" "$HOOK"63        echo "pre-push hook removed"64    else65        echo "no pre-push hook installed by this script"66    fi67    exit 068fi6970mkdir -p "$HOOKS_DIR"71if [ -f "$HOOK" ] && ! grep -q "$MARK" "$HOOK"; then72    mv "$HOOK" "$HOOK.local"73    echo "existing pre-push hook kept as $HOOK.local (it runs first)"74fi7576cat > "$HOOK" <<'HOOK_BODY'77#!/usr/bin/env bash78# qyvora-security-audit-hook — installed by security/audit/install-hooks.sh79set -u80ROOT="$(git rev-parse --show-toplevel)"81SELF_DIR="$(cd "$(dirname "$0")" && pwd)"82if [ -x "$SELF_DIR/pre-push.local" ]; then83    "$SELF_DIR/pre-push.local" "$@" || exit $?84fi85if [ "${QY_SKIP_AUDIT:-0}" = "1" ]; then86    echo "pre-push: QY_SKIP_AUDIT=1 — security audit SKIPPED for this push" >&287    exit 088fi89# Run only when the pushed commits touch the wallet or the audit engine.90relevant=091while read -r local_ref local_sha remote_ref remote_sha; do92    [ -z "${local_sha:-}" ] && continue93    case "$local_sha" in 0000000000000000000000000000000000000000) continue ;; esac94    if [ "$remote_sha" = "0000000000000000000000000000000000000000" ]; then95        range="$local_sha"96        files="$(git -C "$ROOT" show --pretty=format: --name-only "$local_sha" 2>/dev/null)"97    else98        files="$(git -C "$ROOT" diff --name-only "$remote_sha" "$local_sha" 2>/dev/null)"99    fi100    if printf '%s\n' "$files" | grep -Eq '^(Sources/|Tests/|App/|security/|Package\.swift|Scripts/security-audit\.sh)'; then101        relevant=1102    fi103done104if [ "$relevant" = "0" ]; then105    exit 0106fi107echo "pre-push: running the quick security audit (QY_SKIP_AUDIT=1 to skip)…" >&2108# Output goes to a scratch directory: the hook never rewrites the committed reports.109OUT="${TMPDIR:-/tmp}/qyvora-prepush-audit"110# shellcheck disable=SC2086111QY_AUDIT_REPORTS="$OUT" "$ROOT/Scripts/security-audit.sh" ${QY_HOOK_AUDIT_ARGS:---quick --no-report}112status=$?113if [ "$status" -ne 0 ]; then114    echo "pre-push: the release gate of the quick audit FAILED (exit $status) — push refused." >&2115    echo "pre-push: see $OUT/SECURITY_AUDIT_RESULTS.json (field gate.blockers)." >&2116    exit 1117fi118exit 0119HOOK_BODY120chmod +x "$HOOK"121echo "pre-push hook installed: $HOOK"122
Files main
Menu

Theme