#!/usr/bin/env bash # CWE-407 scan — nginx (resolver CNAME chains, upstream graph) set -euo pipefail echo "# scan=nginx host=$(hostname) date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" cd /tmp && git clone --depth 1 https://github.com/nginx/nginx nginx 2>&1 | tail -1 GRAPH_KW="cycle\b|reachab\|topolog\|cname.*chain\|chain.*cname\|upstream.*graph\|dependency\|dfs\b\|visited" C_MEM='ngx_array_push.*==\|for.*elts\|ngx_str_set.*find\|linear\|ngx_list_push.*==\b' echo "# roots: /tmp/nginx/src" find /tmp/nginx/src -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 echo "# scan complete"