-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathsecurity-headers-test.sh
More file actions
executable file
·322 lines (267 loc) · 11.1 KB
/
security-headers-test.sh
File metadata and controls
executable file
·322 lines (267 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/bin/bash
# Security headers validation — checks that the Netlify deployment serves
# proper security headers (CSP, X-Frame-Options, HSTS, etc.).
#
# Usage:
# ./scripts/security-headers-test.sh # Check production site
# ./scripts/security-headers-test.sh --url <url> # Check a specific URL
# ./scripts/security-headers-test.sh --config-only # Only validate netlify.toml config
#
# Prerequisites:
# - curl (for live checks)
# - python3 (for config parsing)
#
# Output:
# /tmp/security-headers-report.json — full JSON data
# /tmp/security-headers-summary.md — human-readable summary
#
# Exit code:
# 0 — all required headers present
# 1 — missing required security headers
set -euo pipefail
cd "$(dirname "$0")/.."
# ============================================================================
# Colors & argument parsing
# ============================================================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BOLD='\033[1m'
DIM='\033[2m'
NC='\033[0m'
CHECK_URL="https://console.kubestellar.io"
CONFIG_ONLY=""
for arg in "$@"; do
case "$arg" in
--url) shift_next="url" ;;
--config-only) CONFIG_ONLY="1" ;;
*) [ "${shift_next:-}" = "url" ] && CHECK_URL="$arg" && shift_next="" ;;
esac
done
REPORT_JSON="/tmp/security-headers-report.json"
REPORT_MD="/tmp/security-headers-summary.md"
echo -e "${BOLD}═══════════════════════════════════════════════════${NC}"
echo -e "${BOLD} Security Headers Validation${NC}"
echo -e "${BOLD}═══════════════════════════════════════════════════${NC}"
echo ""
# ============================================================================
# Required headers and their expected values
# ============================================================================
# Headers we require (name, expected pattern, severity)
declare -a REQUIRED_HEADERS=(
"X-Frame-Options|DENY|required"
"X-Content-Type-Options|nosniff|required"
"Referrer-Policy|strict-origin|required"
)
# Headers we recommend (nice to have)
declare -a RECOMMENDED_HEADERS=(
"Content-Security-Policy|.|recommended"
"Strict-Transport-Security|max-age=|recommended"
"Permissions-Policy|.|recommended"
"X-XSS-Protection|.|recommended"
)
PASS_COUNT=0
FAIL_COUNT=0
WARN_COUNT=0
TOTAL_CHECKS=0
RESULTS_LINES=""
# ============================================================================
# Phase 1: Validate netlify.toml configuration
# ============================================================================
echo -e "${BOLD}Phase 1: netlify.toml header configuration${NC}"
echo ""
NETLIFY_TOML="netlify.toml"
# Returns exit code 0 if $1 is configured as a real header key inside a
# [[headers]] -> [headers.values] block in the given TOML file.
# Comment lines and unrelated sections are not matched.
netlify_header_configured() {
local header_name="$1"
local toml_file="$2"
python3 - "$header_name" "$toml_file" <<'PYEOF'
import sys, re
header_name = sys.argv[1].lower()
toml_file = sys.argv[2]
in_headers_block = False # inside [[headers]]
in_values_section = False # inside [headers.values]
with open(toml_file, encoding="utf-8") as fh:
for raw_line in fh:
line = raw_line.split("#")[0].strip() # strip inline comments
if not line:
continue
# Detect [[headers]] array-of-tables header
if re.fullmatch(r'\[\[headers\]\]', line, re.IGNORECASE):
in_headers_block = True
in_values_section = False
continue
# Any other [section] resets context
if re.match(r'^\[', line):
if re.fullmatch(r'\[headers\.values\]', line, re.IGNORECASE) and in_headers_block:
in_values_section = True
else:
in_headers_block = False
in_values_section = False
continue
# key = value assignment inside [headers.values]
if in_values_section:
m = re.match(r'^([A-Za-z0-9_-]+)\s*=', line)
if m and m.group(1).lower() == header_name:
sys.exit(0)
sys.exit(1)
PYEOF
}
if [ -f "$NETLIFY_TOML" ]; then
for entry in "${REQUIRED_HEADERS[@]}"; do
IFS='|' read -r header_name expected_pattern severity <<< "$entry"
TOTAL_CHECKS=$((TOTAL_CHECKS + 1))
if netlify_header_configured "$header_name" "$NETLIFY_TOML"; then
echo -e " ${GREEN}✓${NC} ${header_name} — configured in netlify.toml"
PASS_COUNT=$((PASS_COUNT + 1))
RESULTS_LINES="${RESULTS_LINES}{\"header\":\"${header_name}\",\"source\":\"config\",\"status\":\"pass\",\"severity\":\"${severity}\"},"
else
echo -e " ${RED}❌${NC} ${header_name} — ${RED}MISSING from netlify.toml${NC}"
FAIL_COUNT=$((FAIL_COUNT + 1))
RESULTS_LINES="${RESULTS_LINES}{\"header\":\"${header_name}\",\"source\":\"config\",\"status\":\"fail\",\"severity\":\"${severity}\"},"
fi
done
echo ""
for entry in "${RECOMMENDED_HEADERS[@]}"; do
IFS='|' read -r header_name expected_pattern severity <<< "$entry"
TOTAL_CHECKS=$((TOTAL_CHECKS + 1))
if netlify_header_configured "$header_name" "$NETLIFY_TOML"; then
echo -e " ${GREEN}✓${NC} ${header_name} — configured"
PASS_COUNT=$((PASS_COUNT + 1))
RESULTS_LINES="${RESULTS_LINES}{\"header\":\"${header_name}\",\"source\":\"config\",\"status\":\"pass\",\"severity\":\"${severity}\"},"
else
echo -e " ${YELLOW}⚠️ ${NC} ${header_name} — ${YELLOW}not configured (recommended)${NC}"
WARN_COUNT=$((WARN_COUNT + 1))
RESULTS_LINES="${RESULTS_LINES}{\"header\":\"${header_name}\",\"source\":\"config\",\"status\":\"warn\",\"severity\":\"${severity}\"},"
fi
done
else
echo -e " ${YELLOW}⚠️ netlify.toml not found — skipping config check${NC}"
fi
echo ""
# ============================================================================
# Phase 2: Live header check (skip if --config-only)
# ============================================================================
if [ -z "$CONFIG_ONLY" ]; then
echo -e "${BOLD}Phase 2: Live header check (${CHECK_URL})${NC}"
echo ""
# Fetch headers with timeout
FETCH_TIMEOUT_SECS=10
HEADERS_FILE=$(mktemp)
trap 'rm -f "$HEADERS_FILE"' EXIT
HTTP_CODE=$(curl -sS -o /dev/null -w "%{http_code}" \
--max-time "$FETCH_TIMEOUT_SECS" \
-D "$HEADERS_FILE" \
"$CHECK_URL" 2>/dev/null || echo "000")
if [ "$HTTP_CODE" = "000" ]; then
echo -e " ${YELLOW}⚠️ Could not reach ${CHECK_URL} — skipping live check${NC}"
elif [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 400 ]; then
echo -e " ${DIM}HTTP ${HTTP_CODE}${NC}"
echo ""
ALL_HEADERS=("${REQUIRED_HEADERS[@]}" "${RECOMMENDED_HEADERS[@]}")
for entry in "${ALL_HEADERS[@]}"; do
IFS='|' read -r header_name expected_pattern severity <<< "$entry"
TOTAL_CHECKS=$((TOTAL_CHECKS + 1))
header_value=""
header_value=$(grep -i "^${header_name}:" "$HEADERS_FILE" 2>/dev/null | head -1 | cut -d: -f2- | sed 's/^[[:space:]]*//' || true)
if [ -n "$header_value" ]; then
pattern_match=""
echo "$header_value" | grep -qi "$expected_pattern" 2>/dev/null && pattern_match="1"
if [ -n "$pattern_match" ]; then
echo -e " ${GREEN}✓${NC} ${header_name}: ${DIM}${header_value}${NC}"
PASS_COUNT=$((PASS_COUNT + 1))
RESULTS_LINES="${RESULTS_LINES}{\"header\":\"${header_name}\",\"source\":\"live\",\"status\":\"pass\",\"value\":\"$(echo "$header_value" | sed 's/"/\\"/g')\",\"severity\":\"${severity}\"},"
else
echo -e " ${YELLOW}⚠️ ${NC} ${header_name}: ${header_value} ${YELLOW}(unexpected value)${NC}"
WARN_COUNT=$((WARN_COUNT + 1))
RESULTS_LINES="${RESULTS_LINES}{\"header\":\"${header_name}\",\"source\":\"live\",\"status\":\"warn\",\"value\":\"$(echo "$header_value" | sed 's/"/\\"/g')\",\"severity\":\"${severity}\"},"
fi
else
if [ "$severity" = "required" ]; then
# Live check is informational — config validation is the authority
echo -e " ${YELLOW}⚠️ ${NC} ${header_name}: ${YELLOW}not served live (deploy pending?)${NC}"
WARN_COUNT=$((WARN_COUNT + 1))
RESULTS_LINES="${RESULTS_LINES}{\"header\":\"${header_name}\",\"source\":\"live\",\"status\":\"warn\",\"severity\":\"${severity}\"},"
else
echo -e " ${YELLOW}⚠️ ${NC} ${header_name}: ${YELLOW}not present (recommended)${NC}"
WARN_COUNT=$((WARN_COUNT + 1))
RESULTS_LINES="${RESULTS_LINES}{\"header\":\"${header_name}\",\"source\":\"live\",\"status\":\"warn\",\"severity\":\"${severity}\"},"
fi
fi
done
else
echo -e " ${YELLOW}⚠️ HTTP ${HTTP_CODE} — unexpected status${NC}"
fi
echo ""
fi
# ============================================================================
# Generate reports
# ============================================================================
RESULTS_LINES="${RESULTS_LINES%,}"
cat > "$REPORT_JSON" << EOF
{
"timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
"url": "${CHECK_URL}",
"configOnly": $([ -n "$CONFIG_ONLY" ] && echo "true" || echo "false"),
"summary": {
"total": ${TOTAL_CHECKS},
"pass": ${PASS_COUNT},
"fail": ${FAIL_COUNT},
"warn": ${WARN_COUNT}
},
"checks": [${RESULTS_LINES}]
}
EOF
cat > "$REPORT_MD" << EOF
# Security Headers Report
**Date:** $(date -u +%Y-%m-%dT%H:%M:%SZ)
**URL:** ${CHECK_URL}
## Summary
| Metric | Count |
|--------|-------|
| Passed | ${PASS_COUNT} |
| Failed | ${FAIL_COUNT} |
| Warnings | ${WARN_COUNT} |
| **Total** | **${TOTAL_CHECKS}** |
**Status:** $([ "$FAIL_COUNT" -eq 0 ] && echo "PASS" || echo "FAIL")
## Required Headers
| Header | Status |
|--------|--------|
EOF
for entry in "${REQUIRED_HEADERS[@]}"; do
IFS='|' read -r header_name expected_pattern severity <<< "$entry"
if netlify_header_configured "$header_name" "$NETLIFY_TOML" 2>/dev/null; then
echo "| ${header_name} | PASS |" >> "$REPORT_MD"
else
echo "| ${header_name} | **FAIL** |" >> "$REPORT_MD"
fi
done
echo "" >> "$REPORT_MD"
echo "## Recommended Headers" >> "$REPORT_MD"
echo "" >> "$REPORT_MD"
echo "| Header | Status |" >> "$REPORT_MD"
echo "|--------|--------|" >> "$REPORT_MD"
for entry in "${RECOMMENDED_HEADERS[@]}"; do
IFS='|' read -r header_name expected_pattern severity <<< "$entry"
if netlify_header_configured "$header_name" "$NETLIFY_TOML" 2>/dev/null; then
echo "| ${header_name} | PASS |" >> "$REPORT_MD"
else
echo "| ${header_name} | WARN |" >> "$REPORT_MD"
fi
done
# ============================================================================
# Summary & exit
# ============================================================================
if [ "$FAIL_COUNT" -eq 0 ]; then
echo -e "${GREEN}${BOLD}Security headers check passed${NC}"
else
echo -e "${RED}${BOLD}Security headers check failed — ${FAIL_COUNT} required header(s) missing${NC}"
fi
echo ""
echo "Reports:"
echo " JSON: $REPORT_JSON"
echo " Summary: $REPORT_MD"
[ "$FAIL_COUNT" -gt 0 ] && exit 1
exit 0