Skip to content

Commit 9c36de6

Browse files
authored
Adding inline script hashes to CSP (#24311)
Docusaurus generates only 2 inline scripts during our website build, one for banner insertion and other one for setting dark/light theme. By doing a search on script tags on our build output, these are the only ones. After adding their respective hashes and testing the website on a staging environment, I could confirm that such scripts don't get flagged by the CSP. I also edited some files in our docs folder (react and docs) to see if script generation changed, but remained the same. In addition, adding an extra step to our website deployment pipeline to generate hashes for every script in our index.html and verifies that matches with the ones we have hard coded in our config file. This will give us a failure in case someone changes something in our code that changes the generated inline scripts. No check on other files is needed since the option 'self' in CSP allows us run external scripts (in comparison to inline scripts which we have removed the option to permit (unsafe-inline)). Moving script-src to report only mode again to have a test period.
1 parent a104f51 commit 9c36de6

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

docs/static/staticwebapp.config.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"globalHeaders": {
33
"cache-control": "must-revalidate, max-age=3600",
4-
"Content-Security-Policy": "script-src 'self' 'unsafe-inline' 'unsafe-eval' https:; object-src 'none'; base-uri 'self';",
5-
"Content-Security-Policy-Report-Only": "require-trusted-types-for 'script'; trusted-types default; report-uri https://csp.microsoft.com/report/FluidFramework-WW"
4+
"Content-Security-Policy-Report-Only": "script-src 'self' 'sha256-O8zYuOjyuzUZDv3fub7DKfAs5TEd1dG+fz+hCSCFmQA=' 'sha256-faMHt+UAWeoFU7ZBnPhfAu9zOnnNUwL4RYp09gSUEjU=' ; base-uri 'self'; object-src 'none'; require-trusted-types-for 'script'; trusted-types default; report-uri https://csp.microsoft.com/report/FluidFramework-WW"
65
},
76
"navigationFallback": {
87
"rewrite": "/api/fallback"

docs/validateHashes.sh

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
set -eu -o pipefail
3+
# This script validates the hashes of inline scripts in the index.html file against the configured hashes in the staticwebapp.config.json file.
4+
indexFile="build/index.html"
5+
configFile="static/staticwebapp.config.json"
6+
7+
echo "Extracting and hashing inline scripts from $indexFile"
8+
9+
tmpHashesFile="generated_hashes.txt"
10+
> "$tmpHashesFile"
11+
12+
# Extract inline scripts and compute hashes
13+
awk 'BEGIN { RS="</script>"; FS="<script[^>]*>" }
14+
NF>1 { print $2 }' "$indexFile" | while read -r scriptContent; do
15+
if [[ "$scriptContent" != "" ]]; then
16+
echo "$scriptContent" | tr -d '\n'| openssl dgst -sha256 -binary | openssl base64 | sed 's/^/sha256-/' >> "$tmpHashesFile"
17+
fi
18+
done
19+
20+
echo "Extracted Hashes:"
21+
cat "$tmpHashesFile"
22+
23+
echo "Reading configured hashes from $configFile"
24+
grep -oE "sha256-[A-Za-z0-9+/=]{43,45}" "$configFile" | sort | uniq > expected_hashes.txt
25+
cat expected_hashes.txt
26+
27+
echo "Validating..."
28+
fail=0
29+
while read -r actualHash; do
30+
if ! grep -q "$actualHash" expected_hashes.txt; then
31+
echo "Missing hash in config: $actualHash"
32+
fail=1
33+
else
34+
echo "Hash matched: $actualHash"
35+
fi
36+
done < "$tmpHashesFile"
37+
38+
if [ "$fail" -ne 0 ]; then
39+
echo "Inline script hashes do not match configured values. Override the hashes in $configFile with the extracted hashes."
40+
exit 1
41+
fi
42+
43+
echo "All inline script hashes are valid."

tools/pipelines/deploy-website.yml

+7
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ stages:
137137
env:
138138
INSTRUMENTATION_KEY: $(INSTRUMENTATION_KEY)
139139

140+
- task: Bash@3
141+
displayName: 'Check inline script hashes correctness'
142+
inputs:
143+
targetType: 'filePath'
144+
workingDirectory: $(Build.SourcesDirectory)/docs
145+
filePath: '$(Build.SourcesDirectory)/docs/validateHashes.sh'
146+
140147
# Run the tests
141148
- task: Npm@1
142149
displayName: Run tests

0 commit comments

Comments
 (0)