This repository has been archived by the owner on Feb 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempts to fix all the security issues reported by the Mozilla Observatory: https://observatory.mozilla.org/analyze/viz.scrapd.org Fixes #194
- Loading branch information
Showing
4 changed files
with
197 additions
and
3 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,54 @@ | ||
import helmet from 'helmet'; | ||
import uuidv4 from 'uuid/v4'; | ||
|
||
// Configuration values mostly come from this talk: | ||
// https://pyvideo.org/pybay-2019/browser-security-with-http-headers.html | ||
export default function csp(app) { | ||
// Create a nonce on every request and make it available to other middleware | ||
app.use((req, res, next) => { | ||
res.locals.nonce = Buffer.from(uuidv4()).toString('base64'); | ||
next(); | ||
}); | ||
|
||
const nonce = (req, res) => `'nonce-${res.locals.nonce}'`; | ||
|
||
const scriptSrc = [nonce, "'strict-dynamic'", "'unsafe-inline'", 'https:']; | ||
|
||
// In dev we allow 'unsafe-eval', so HMR doesn't trigger the CSP | ||
if (process.env.NODE_ENV !== 'production') { | ||
scriptSrc.push("'unsafe-eval'"); | ||
} | ||
|
||
app.use( | ||
helmet({ | ||
contentSecurityPolicy: { | ||
directives: { | ||
baseUri: ["'none'"], | ||
objectSrc: ["'none'"], | ||
scriptSrc | ||
} | ||
}, | ||
policy: ['strict-origin-when-cross-origin', 'unsafe-url'] | ||
}) | ||
); | ||
|
||
// Sets "Strict-Transport-Security: max-age=31536000"; includeSubDomains; preload. | ||
app.use( | ||
helmet.hsts({ | ||
maxAge: 31536000, | ||
includeSubDomains: true, | ||
preload: true | ||
}) | ||
); | ||
|
||
// Sets "Referrer-Policy: strict-origin-when-cross-origin". | ||
app.use(helmet.referrerPolicy({ policy: 'strict-origin-when-cross-origin' })); | ||
|
||
// The following headers are superseeded by the CSP policy, but still usefull for older browsers. | ||
// Sets "X-Frame-Options: DENY". | ||
app.use(helmet.frameguard({ action: 'deny' })); | ||
// Sets "X-XSS-Protection: 1; mode=block". | ||
app.use(helmet.xssFilter()); | ||
// Sets "X-Content-Type-Options: nosniff". | ||
app.use(helmet.noSniff()); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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