#!/usr/bin/env bash # CWE-407 scan — Varnish Cache (VCL compiler, backend graph) set -euo pipefail echo "# scan=varnish host=$(hostname) date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" cd /tmp && git clone --depth 1 https://github.com/varnishcache/varnish-cache varnish 2>&1 | tail -1 GRAPH_KW="topolog|cycle|reachab|dfs\b|scc\b|dominator|postorder|dependency|vcl.*graph|graph.*vcl" C_MEM='VTAILQ_FOREACH.*==\|VSB_.*find\|for.*next.*next\b\|linear.*search\|vcc_.*find' for dir in lib/libvcc bin/varnishd; do [ -d /tmp/varnish/$dir ] || continue echo "# roots: /tmp/varnish/$dir" find /tmp/varnish/$dir -name "*.c" -o -name "*.h" | sort | while IFS= read -r f; do gl=$(grep -inE "$GRAPH_KW" "$f" 2>/dev/null | cut -d: -f1 | head -5 || true) [ -z "$gl" ] && continue while IFS= read -r ln; do s=$(( ln > 20 ? ln - 20 : 1 )); e=$(( ln + 20 )) hit=$(sed -n "${s},${e}p" "$f" 2>/dev/null | grep -iE "$C_MEM" | head -1 | sed 's/^\s*//' || true) [ -n "$hit" ] && echo "CANDIDATE ${f}:${ln} ${hit}" done <<< "$gl" done done echo "# scan complete"