This repository has been archived by the owner on Oct 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add documentation for CSP nonce (res.locals.nonce)
- Loading branch information
1 parent
b95e205
commit 6cde0fa
Showing
5 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: Security | ||
--- | ||
|
||
By default, Sapper does not add security headers to your app, but you may add them yourself using middleware such as [Helmet][]. | ||
|
||
### Content Security Policy (CSP) | ||
|
||
Sapper generates inline `<script>`s, which can fail to execute if | ||
[Content Security Policy (CSP)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) headers | ||
disallow arbitrary script execution (`unsafe-inline`). | ||
|
||
To work around this, Sapper can inject a | ||
[nonce](https://www.troyhunt.com/locking-down-your-website-scripts-with-csp-hashes-nonces-and-report-uri/) | ||
which can be configured with middleware to emit the proper CSP headers. | ||
Here is an example using [Express][] and [Helmet][]: | ||
|
||
```js | ||
// server.js | ||
import uuidv4 from 'uuid/v4'; | ||
import helmet from 'helmet'; | ||
|
||
app.use((req, res, next) => { | ||
res.locals.nonce = uuidv4(); | ||
next(); | ||
}); | ||
app.use(helmet({ | ||
contentSecurityPolicy: { | ||
directives: { | ||
scriptSrc: [ | ||
"'self'", | ||
(req, res) => `'nonce-${res.locals.nonce}'` | ||
] | ||
} | ||
} | ||
})); | ||
app.use(sapper({manifest})); | ||
``` | ||
|
||
Using `res.locals.nonce` in this way follows the convention set by | ||
[Helmet's CSP docs](https://helmetjs.github.io/docs/csp/#generating-nonces). | ||
|
||
[Express]: https://expressjs.com/ | ||
[Helmet]: https://helmetjs.github.io/ |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.