#!/usr/bin/env bash # # The automatic half of the leak standard, run on what a public release would # publish: the exported tree and the public changelog. Two kinds of hit fail it: # # words that must never ship the names of tools and services this project # has nothing to do with in public # private record ids "#1234", "rule 12", "milestone 7": pointers into # a tracker no reader of the release can open # # The ids are also searched with comment continuation lines joined, because a # citation split at a line break ("see rule\n// 12") passes a per-line search. # # The by-eye half (people, titles, quotes, images) is not here: a search cannot # decide it. # # Known false positives are listed in leak-allow.txt, one pattern per line. A # pattern is cut out of a hit line before the line is tested again, so an # allowed word never hides a real hit beside it. # # Usage: ci/leakgate.sh ... (directories are searched recursively) # Prints each hit and exits 1 if there is one. set -euo pipefail readonly HARD='sluice|storehouse|prospector|bookbag|musicbox|fabledexchange|lidarr|readarr|chaptarr|prowlarr|jackett|sonarr|radarr|qbittorrent|deluge|sabnzbd|nzbget|slskd|soulseek|deemix|torrent|usenet|\bnzb\b|magnet:|grimoire|\bscribe\b|traefik\.internal|vdnt' readonly IDS='(\(|\s)#[0-9]{3,5}\b|\bM[0-9]{1,3}\b|\b(rule|preference|lesson|snippet|process|milestone) #?[0-9]{1,5}\b' # Joined across comment lines, only the worded citations: the bare forms above # are already whole on one line. readonly JOINED='\b(?:rule|preference|lesson|snippet|process|milestone|task|issue)\s+#?\d{1,5}\b|\b(?:reference|measured)\s+library\b' [ "$#" -gt 0 ] || { echo "usage: $0 ..." >&2; exit 2; } here="$(cd "$(dirname "$0")" && pwd)" strip="$(mktemp)" trap 'rm -f "$strip"' EXIT # One sed substitution per allowed pattern, case-insensitive like the search. # \x01 delimits, so a pattern may hold any other character. while IFS= read -r pattern; do case "$pattern" in '' | '#'*) continue ;; esac printf 's\x01%s\x01\x01gI\n' "$pattern" >>"$strip" done <"$here/leak-allow.txt" # Reads grep's path:line:text and prints the hits that still match once the # allowed patterns are cut out of the text. still() { local hit text while IFS= read -r hit; do text="${hit#*:*:}" if printf '%s\n' "$text" | sed -E -f "$strip" | grep -qiE -- "$1"; then printf '%s\n' "$hit" fi done } found="$( { grep -rnHIiE -- "$HARD" "$@" || true grep -rnHIiE -- "$IDS" "$@" || true } | still "$HARD|$IDS" # -I skips binary files, as grep does above. Each joined hit is printed as # path:joined:text, so the same filter applies. find "$@" -type f -exec grep -lIZ '' {} + 2>/dev/null \ | xargs -0 -r perl -0777 -ne ' s/\n[ \t]*(?:\/\/+|--|\*|#+)[ \t]*/ /g; while (/('"$JOINED"')/gi) { (my $m = $1) =~ s/\s+/ /g; print "$ARGV:joined:$m\n" }' \ | still "$(printf '%s' "$JOINED" | sed -E 's/\(\?:/(/g; s/\\s/[[:space:]]/g; s/\\d/[0-9]/g')" )" if [ -n "$found" ]; then printf '%s\n' "$found" | sort -u echo "leakgate: the hits above would be published; reword them, or list a false positive in ci/leak-allow.txt" >&2 exit 1 fi echo "leakgate: clean"