Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nonce to style element for Content Security Policy #663

Merged
merged 1 commit into from
Apr 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/AutoSizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ High-order component that automatically adjusts the width and height of a single
| children | Function | ✓ | Function responsible for rendering children. This function should implement the following signature: `({ height: number, width: number }) => PropTypes.element` |
| disableHeight | Boolean | | Fixed `height`; if specified, the child's `height` property will not be managed |
| disableWidth | Boolean | | Fixed `width`; if specified, the child's `width` property will not be managed |
| nonce | String | | Nonce of the inlined stylesheets for Content Security Policy |
| onResize | Function | | Callback to be invoked on-resize; it is passed the following named parameters: `({ height: number, width: number })`. |

### Examples
Expand Down
12 changes: 12 additions & 0 deletions docs/usingAutoSizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,15 @@ When using an `AutoSizer` as a direct child of a flex box it usually works out b
```

You can see an example of this [here](https://bvaughn.github.io/react-virtualized/#/components/InfiniteLoader).

### Applying Content Security Policy
[The specification of Content Security Policy](https://www.w3.org/TR/2016/REC-CSP2-20161215/#intro)
describes as the following:

> This document defines Content Security Policy, a mechanism web applications
> can use to mitigate a broad class of content injection vulnerabilities, such
> as cross-site scripting (XSS).

To apply Content Security Policy, you need to pass a nonce to
_react-virtualized_ and accordingly set `nonce-source` to `Content-Security-Policy` field
in HTTP header.
5 changes: 4 additions & 1 deletion source/AutoSizer/AutoSizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default class AutoSizer extends PureComponent {
/** Disable dynamic :width property */
disableWidth: PropTypes.bool,

/** Nonce of the inlined stylesheet for Content Security Policy */
nonce: PropTypes.string,

/** Callback to be invoked on-resize: ({ height, width }) */
onResize: PropTypes.func.isRequired
};
Expand Down Expand Up @@ -51,7 +54,7 @@ export default class AutoSizer extends PureComponent {

// Defer requiring resize handler in order to support server-side rendering.
// See issue #41
this._detectElementResize = createDetectElementResize()
this._detectElementResize = createDetectElementResize(this.props.nonce)
this._detectElementResize.addResizeListener(this._parentNode, this._onResize)

this._onResize()
Expand Down
4 changes: 3 additions & 1 deletion source/vendor/detectElementResize.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
* 1) Guard against unsafe 'window' and 'document' references (to support SSR).
* 2) Defer initialization code via a top-level function wrapper (to support SSR).
* 3) Avoid unnecessary reflows by not measuring size for scroll events bubbling from children.
* 4) Add nonce for style element.
**/

export default function createDetectElementResize () {
export default function createDetectElementResize (nonce) {
// Check `document` and `window` in case of server-side rendering
var _window
if (typeof window !== 'undefined') {
Expand Down Expand Up @@ -118,6 +119,7 @@ export default function createDetectElementResize () {

style.id = 'detectElementResize';
style.type = 'text/css';
style.setAttribute('nonce', nonce);
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
Expand Down