Skip to content

Commit 9222965

Browse files
committed
refactor(security): extracts CSP hash verification into shared script
1 parent a005ceb commit 9222965

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@ jobs:
1515
cache: pnpm
1616
- run: pnpm install --frozen-lockfile
1717
- name: Verify CSP inline script hash
18-
run: |
19-
SCRIPT=$(sed -n 's/.*<script>\(.*\)<\/script>.*/\1/p' index.html)
20-
HASH=$(printf '%s' "$SCRIPT" | openssl dgst -sha256 -binary | base64)
21-
if ! grep -q "sha256-$HASH" public/_headers; then
22-
echo "::error::CSP hash mismatch! Inline script hash 'sha256-$HASH' not found in public/_headers"
23-
echo "Update the sha256 hash in public/_headers to match the inline script in index.html"
24-
exit 1
25-
fi
26-
echo "CSP hash verified: sha256-$HASH"
18+
run: bash scripts/verify-csp-hash.sh
2719
- run: pnpm run typecheck
2820
- run: pnpm test
2921
- name: Install Playwright browsers

.github/workflows/deploy.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,7 @@ jobs:
1919
- run: pnpm run typecheck
2020
- run: pnpm test
2121
- name: Verify CSP inline script hash
22-
run: |
23-
SCRIPT=$(sed -n 's/.*<script>\(.*\)<\/script>.*/\1/p' index.html)
24-
HASH=$(printf '%s' "$SCRIPT" | openssl dgst -sha256 -binary | base64)
25-
if ! grep -q "sha256-$HASH" public/_headers; then
26-
echo "::error::CSP hash mismatch! Inline script hash 'sha256-$HASH' not found in public/_headers"
27-
echo "Update the sha256 hash in public/_headers to match the inline script in index.html"
28-
exit 1
29-
fi
30-
echo "CSP hash verified: sha256-$HASH"
22+
run: bash scripts/verify-csp-hash.sh
3123
- name: WAF smoke tests
3224
run: pnpm test:waf
3325
- run: pnpm run build

prek.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ priority = 0
4444
id = "csp-hash"
4545
name = "CSP hash verification"
4646
language = "system"
47-
entry = "bash -c 'SCRIPT=$(sed -n \"s/.*<script>\\(.*\\)<\\/script>.*/\\1/p\" index.html) && HASH=$(printf \"%s\" \"$SCRIPT\" | openssl dgst -sha256 -binary | base64) && grep -q \"sha256-$HASH\" public/_headers && echo \"CSP hash OK: sha256-$HASH\" || { echo \"CSP hash mismatch! sha256-$HASH not in public/_headers\"; exit 1; }'"
47+
entry = "bash scripts/verify-csp-hash.sh"
4848
pass_filenames = false
4949
always_run = true
5050
priority = 0

scripts/verify-csp-hash.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
SCRIPT=$(sed -n 's/.*<script>\([^<]*\)<\/script>.*/\1/p' index.html)
5+
if [[ -z "$SCRIPT" ]]; then
6+
echo "No inline <script>...</script> found in index.html"
7+
[[ -n "${GITHUB_ACTIONS:-}" ]] && echo "::error::No inline <script>...</script> found in index.html"
8+
exit 1
9+
fi
10+
HASH=$(printf '%s' "$SCRIPT" | openssl dgst -sha256 -binary | base64)
11+
12+
if ! grep -qF "sha256-$HASH" public/_headers; then
13+
echo "CSP hash mismatch! Inline script hash 'sha256-$HASH' not found in public/_headers"
14+
echo "Update the sha256 hash in public/_headers to match the inline script in index.html"
15+
[[ -n "${GITHUB_ACTIONS:-}" ]] && echo "::error::CSP hash mismatch! Inline script hash 'sha256-$HASH' not found in public/_headers"
16+
exit 1
17+
fi
18+
19+
echo "CSP hash verified: sha256-$HASH"

0 commit comments

Comments
 (0)