forked from aws-amplify/amplify-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: custom security headers (aws-amplify#963)
* Add custom security headers * Add dev mode headers, move csp to document * Remove Google fonts * Allow codesandbox iframe
- Loading branch information
Showing
4 changed files
with
85 additions
and
24 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,17 @@ | ||
# Custom headers when serving docs through Amplify Hosting | ||
# | ||
# See: https://docs.aws.amazon.com/amplify/latest/userguide/custom-headers.html#setting-custom-headers | ||
applications: | ||
- appRoot: docs | ||
customHeaders: | ||
- pattern: "**/*" | ||
headers: | ||
- key: "Strict-Transport-Security" | ||
value: "max-age=31536000; includeSubDomains" | ||
- key: "X-Frame-Options" | ||
value: "SAMEORIGIN" | ||
- key: "X-XSS-Protection" | ||
value: "1; mode=block" | ||
- key: "X-Content-Type-Options" | ||
value: "nosniff" | ||
# CSP set in _document.page.tsx meta tag |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,53 @@ | ||
import crypto from 'crypto'; | ||
import type { HtmlProps } from 'next/dist/shared/lib/utils'; | ||
import Document, { Html, Head, Main, NextScript } from 'next/document'; | ||
|
||
const favicon = | ||
process.env.NODE_ENV === 'development' | ||
? '/svg/favicon-dev.svg' | ||
: '/svg/favicon.svg'; | ||
|
||
const cspHashOf = (text) => { | ||
const hash = crypto.createHash('sha256'); | ||
hash.update(text); | ||
return `sha256-${hash.digest('base64')}`; | ||
}; | ||
|
||
// See: https://github.com/vercel/next.js/blob/master/examples/with-strict-csp/pages/_document.js | ||
const getCSPContent = (context: Readonly<HtmlProps>) => { | ||
const cspInlineScriptHash = cspHashOf( | ||
NextScript.getInlineScriptSource(context) | ||
); | ||
|
||
// Dev environment | ||
if (process.env.NODE_ENV !== 'production') { | ||
return `default-src 'self'; | ||
style-src 'self' 'unsafe-inline'; | ||
font-src 'self' data:; | ||
frame-src *.codesandbox.io; | ||
script-src 'unsafe-eval' 'self' '${cspInlineScriptHash}' | ||
`; | ||
} | ||
|
||
// Prod environment | ||
return `default-src 'self'; | ||
style-src 'self' 'unsafe-inline'; | ||
font-src 'self'; | ||
frame-src *.codesandbox.io; | ||
script-src 'self' '${cspInlineScriptHash}' | ||
`; | ||
}; | ||
|
||
class MyDocument extends Document { | ||
render() { | ||
return ( | ||
<Html> | ||
<Head> | ||
<link rel="icon" type="image/svg+xml" href={favicon} /> | ||
{/* Adding custom variable fonts from google */} | ||
{/* Including multiple to show theming capabilities */} | ||
<link rel="preconnect" href="https://fonts.googleapis.com" /> | ||
<link | ||
rel="preconnect" | ||
href="https://fonts.gstatic.com" | ||
crossOrigin="true" | ||
/> | ||
<link | ||
href="https://fonts.googleapis.com/css2?family=Open+Sans:[email protected]&family=Work+Sans:wght@100..900&display=swap" | ||
rel="stylesheet" | ||
<meta | ||
httpEquiv="Content-Security-Policy" | ||
content={getCSPContent(this.props)} | ||
/> | ||
<link rel="icon" type="image/svg+xml" href={favicon} /> | ||
</Head> | ||
<body> | ||
<Main /> | ||
|
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