-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
68 additions
and
25 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {forwardRef} from 'react'; | ||
import {useNonce} from './csp'; | ||
|
||
type ScriptProps = JSX.IntrinsicElements['script']; | ||
|
||
export const Script = forwardRef<HTMLScriptElement, ScriptProps>( | ||
(props, ref) => { | ||
const nonce = useNonce(); | ||
return <script {...props} nonce={nonce} ref={ref} />; | ||
}, | ||
); |
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,30 @@ | ||
import {createContext, useContext} from 'react'; | ||
import cspBuilder from 'content-security-policy-builder'; | ||
|
||
const CSPContext = createContext<string | undefined>(undefined); | ||
|
||
export const CSPProvider = CSPContext.Provider; | ||
|
||
export const useNonce = () => useContext(CSPContext); | ||
|
||
export function createCSPHeader(nonce: string, directives = {}) { | ||
const defaultDirectives = { | ||
baseUri: ["'self'"], | ||
defaultSrc: ["'self'", `'nonce-${nonce}'`, 'https://cdn.shopify.com'], | ||
frameAncestors: ['none'], | ||
styleSrc: ["'self'", "'unsafe-inline'", 'https://cdn.shopify.com'], | ||
}; | ||
return cspBuilder({ | ||
directives: Object.assign({}, defaultDirectives, directives), | ||
}); | ||
} | ||
|
||
export function generateNonce() { | ||
return toHexString(crypto.getRandomValues(new Uint8Array(16))); | ||
} | ||
|
||
function toHexString(byteArray: Uint8Array) { | ||
return Array.from(byteArray, function (byte) { | ||
return ('0' + (byte & 0xff).toString(16)).slice(-2); | ||
}).join(''); | ||
} |
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
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
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