Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,18 @@ jobs:
exit 1
fi

# style-src may contain 'unsafe-inline' (Leptos requirement), but no other source should
# style-src may contain 'unsafe-inline' (Leptos requirement)
style_val=$(echo "$csp" | sed -n 's/.*style-src \([^;]*\);.*/\1/p')
if echo "$style_val" | grep -q "unsafe-inline"; then
echo " style-src contains 'unsafe-inline' (expected for Leptos)"
fi

# script-src must NOT contain 'unsafe-inline'
# script-src may contain 'unsafe-inline' (Trunk generates an inline
# <script type="module"> to bootstrap WASM; the hash changes per build
# because filenames are content-hashed, so a static hash is not viable).
script_val=$(echo "$csp" | sed -n 's/.*script-src \([^;]*\);.*/\1/p')
if echo "$script_val" | grep -q "unsafe-inline"; then
echo "::error::script-src must not contain 'unsafe-inline'"
exit 1
echo " script-src contains 'unsafe-inline' (Trunk WASM bootstrap)"
fi

echo "✅ CSP header is valid and contains all required directives"
Expand Down
11 changes: 8 additions & 3 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ CSP directives enforced:

```
default-src 'none';
script-src 'self';
script-src 'self' 'unsafe-inline';
style-src 'self' 'unsafe-inline';
img-src 'self' blob:;
font-src 'self';
Expand All @@ -79,8 +79,13 @@ manifest-src 'self';
```

No `unsafe-eval`. The `unsafe-inline` for styles is required by Leptos's
CSR rendering. CI enforces that the CSP header is present and valid via the
`csp-verify` job in `.github/workflows/ci.yml`.
CSR rendering. The `unsafe-inline` for scripts is required because Trunk
generates an inline `<script type="module">` to bootstrap WASM — its
content (including hashed filenames) changes per build, making a static
SHA-256 hash non-viable. This is a known limitation; a post-build
extraction step would be needed to eliminate it. CI enforces that the
CSP header is present and valid via the `csp-verify` job in
`.github/workflows/ci.yml`.

---

Expand Down
23 changes: 16 additions & 7 deletions static/pwa/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,22 @@ self.addEventListener("fetch", (event) => {
// All other requests (CSS, WASM, JS, fonts, images): stale-while-revalidate
event.respondWith(
caches.match(request).then((cached) => {
const networkFetch = fetch(request).then((response) => {
if (response.ok && request.url.startsWith(self.location.origin)) {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone));
}
return response;
});
const networkFetch = fetch(request)
.then((response) => {
if (response.ok && request.url.startsWith(self.location.origin)) {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone));
}
return response;
})
.catch(() => {
// Network failed and no cache — return a proper error response
// instead of letting the promise reject with a TypeError.
return new Response("Offline", {
status: 503,
statusText: "Service Unavailable",
});
});

return cached || networkFetch;
})
Expand Down
2 changes: 1 addition & 1 deletion vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"source": "/(.*)",
"headers": [
{ "key": "Content-Security-Policy", "value": "default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' blob:; font-src 'self'; connect-src 'self'; worker-src 'self'; manifest-src 'self'" },
{ "key": "Content-Security-Policy", "value": "default-src 'none'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' blob:; font-src 'self'; connect-src 'self'; worker-src 'self'; manifest-src 'self'" },
{ "key": "X-Content-Type-Options", "value": "nosniff" },
{ "key": "X-Frame-Options", "value": "DENY" },
{ "key": "Referrer-Policy", "value": "strict-origin-when-cross-origin" },
Expand Down
Loading