Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Validate `Privacy Guard` denylist patterns with `git grep -E`, the engine that scans, so PCRE-only syntax such as `(?i)` or `(?:...)` fails by pattern index instead of an opaque `git grep failed`; every invalid pattern is reported in one run.

- Add a `Privacy Guard` workflow: gitleaks over full history plus a structural denylist check with an allowlist, optional private patterns, and withheld match content.

## 0.1.0-canary.6
Expand Down
42 changes: 31 additions & 11 deletions scripts/privacy-denylist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,54 @@ patterns="$(mktemp)"
allow_patterns="$(mktemp)"
hits="$(mktemp)"
kept="$(mktemp)"
trap 'rm -f "$tracked" "$extra" "$patterns" "$allow_patterns" "$hits" "$kept"' EXIT
probe_dir="$(mktemp -d)"
trap 'rm -rf "$tracked" "$extra" "$patterns" "$allow_patterns" "$hits" "$kept" "$probe_dir"' EXIT
printf '\n' > "$probe_dir/empty.txt"
printf 'a b\n' > "$probe_dir/text.txt"

strip() { sed -E '/^[[:space:]]*(#|$)/d'; }
# lines <file>: the file with a guaranteed final newline.
lines() { cat "$1"; printf '\n'; }

# valid_regexes <file> <label>: every line must compile and must not match the empty string.
# rx <engine> <flags> <pattern> <probe file>: run one pattern through the engine
# that will use it. git grep -E (POSIX regcomp) rejects PCRE syntax such as
# (?i), (?:...) and lookarounds that GNU grep -E accepts, so denylist patterns
# are validated with git grep itself; allowlist patterns run under GNU grep.
rx() {
local engine="$1" flags="$2" pattern="$3" probe="$4"
if [[ "$engine" == git ]]; then
git -C "$probe_dir" grep --no-index "-${flags}E" -e "$pattern" -- "$probe" 2> /dev/null
else
grep "-${flags}E" -e "$pattern" "$probe_dir/$probe" 2> /dev/null
fi
}

# valid_regexes <file> <label> <engine>: every line must compile and must not match
# the empty string. Reports every bad pattern by index so one run finds them all.
valid_regexes() {
local file="$1" label="$2" n=0 line status probe
local file="$1" label="$2" engine="$3" n=0 bad=0 line status probe
while IFS= read -r line || [[ -n "$line" ]]; do
n=$((n + 1))
printf '\n' | grep -qE -e "$line" 2> /dev/null
rx "$engine" q "$line" empty.txt > /dev/null
status=$?
if (( status > 1 )); then
echo "error: $label pattern #$n is not a valid regex" >&2
return 1
bad=1
continue
fi
if (( status == 0 )); then
echo "error: $label pattern #$n matches the empty string" >&2
return 1
bad=1
continue
fi
# Zero-width patterns (\b, ^, $) match inside real text but report nothing.
probe="$(printf 'a b\n' | grep -oE -e "$line" 2> /dev/null)"
if [[ -z "$probe" ]] && printf 'a b\n' | grep -qE -e "$line" 2> /dev/null; then
probe="$(rx "$engine" o "$line" text.txt)"
if [[ -z "$probe" ]] && rx "$engine" q "$line" text.txt > /dev/null; then
echo "error: $label pattern #$n is zero-width" >&2
return 1
bad=1
fi
done < "$file"
return "$bad"
}

if [[ ! -f "$DENY" ]]; then
Expand All @@ -79,8 +99,8 @@ if [[ ! -s "$tracked" ]]; then
echo "error: $DENY has no patterns" >&2
exit 2
fi
valid_regexes "$patterns" "denylist" || exit 2
valid_regexes "$allow_patterns" "allowlist" || exit 2
valid_regexes "$patterns" "denylist" git || exit 2
valid_regexes "$allow_patterns" "allowlist" grep || exit 2
if [[ -s "$allow_patterns" ]] && [[ -z "$(printf '%s\n' 'zz/canary.md:1:canary.ts''.net' | grep -avEf "$allow_patterns")" ]]; then
echo "error: allowlist is too broad; it would hide an obvious match" >&2
exit 2
Expand Down
11 changes: 11 additions & 0 deletions scripts/test-privacy-denylist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ grep -q '::warning' <<< "$LAST_OUTPUT" || { echo "FAIL no warning when private p
check "invalid private pattern is an error" 2 'PRIVACY_DENYLIST_EXTRA=Acme[Corp'
refute "invalid pattern text leaked to output" "Acme"
check "private pattern matching empty string is an error" 2 'PRIVACY_DENYLIST_EXTRA=acme|'
check "every bad private pattern is reported, not only the first" 2 "PRIVACY_DENYLIST_EXTRA=$(printf '(?i)acme\nfine-term\nacme(?:x)')"
[[ "$(grep -c 'is not a valid regex' <<< "$LAST_OUTPUT")" == 2 ]] || { echo "FAIL expected two reported patterns" >&2; failures=$((failures + 1)); }

# PCRE-only syntax compiles under GNU grep -E but not under git grep -E, the
# engine that scans. It must be rejected by index, before the scan runs.
plant "Plain documentation."
for pcre in '(?i)acme' '(?:acme)' 'acme(?=x)' 'acme\p{L}'; do
check "git-grep-invalid pattern $pcre is rejected up front" 2 "PRIVACY_DENYLIST_EXTRA=$pcre"
grep -q 'denylist pattern #[0-9]* is not a valid regex' <<< "$LAST_OUTPUT" || { echo "FAIL $pcre not reported by pattern index" >&2; failures=$((failures + 1)); }
refute "pcre pattern text leaked to output" "acme"
done

cp "$REPO/.privacy-allowlist" "$REPO/allow.bak"
plant "host: $TAILNET"
Expand Down
Loading