-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Strict CSP with a nonce (#482)
Fixes #482 423
- Loading branch information
Showing
7 changed files
with
490 additions
and
319 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
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,26 +1,29 @@ | ||
import React from 'react' | ||
import { flush } from './style' | ||
|
||
export default function flushToReact() { | ||
export default function flushToReact(options = {}) { | ||
return flush().map(args => { | ||
const id = args[0] | ||
const css = args[1] | ||
return React.createElement('style', { | ||
id: `__${id}`, | ||
// Avoid warnings upon render with a key | ||
key: `__${id}`, | ||
nonce: options.nonce ? options.nonce : undefined, | ||
dangerouslySetInnerHTML: { | ||
__html: css | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
export function flushToHTML() { | ||
export function flushToHTML(options = {}) { | ||
return flush().reduce((html, args) => { | ||
const id = args[0] | ||
const css = args[1] | ||
html += `<style id="__${id}">${css}</style>` | ||
html += `<style id="__${id}"${ | ||
options.nonce ? ` nonce="${options.nonce}"` : '' | ||
}>${css}</style>` | ||
return html | ||
}, '') | ||
} |
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 @@ | ||
export default function withMock(mockFn, testFn) { | ||
return t => { | ||
const cleanUp = mockFn(t) | ||
if (typeof cleanUp !== 'function') { | ||
throw new TypeError('mockFn should return a cleanup function') | ||
} | ||
testFn(t) | ||
cleanUp(t) | ||
} | ||
} | ||
|
||
export function withMockDocument(t) { | ||
const originalDocument = global.document | ||
// We need to stub a document in order to simulate the meta tag | ||
global.document = { | ||
querySelector(query) { | ||
t.is(query, 'meta[property="csp-nonce"]') | ||
return { | ||
getAttribute(attr) { | ||
t.is(attr, 'content') | ||
return 'test-nonce' | ||
} | ||
} | ||
} | ||
} | ||
|
||
return () => { | ||
global.document = originalDocument | ||
} | ||
} |
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
Oops, something went wrong.