-
Notifications
You must be signed in to change notification settings - Fork 122
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 sanitization to dangerouslySetInnerHTML values #530
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import DOMPurify from 'dompurify'; | ||
|
||
import moment from './extendedMoment'; | ||
|
||
/* eslint-disable no-param-reassign */ | ||
|
@@ -200,3 +202,35 @@ export const getScrollToId = (entries, key) => { | |
|
||
return `id_${entries[0].id}`; | ||
}; | ||
|
||
/** | ||
* Sanitize HTML for output. Help prevent XSS attacks. | ||
* | ||
* @link https://www.npmjs.com/package/dompurify | ||
* @param {string} dirty HTML to sanitize | ||
* @return {string} sanitized HTML | ||
*/ | ||
export const sanitizeHTML = (dirty) => { | ||
// Whitelist iframes for the plugins 'embded media' feature. | ||
const iframeWhitelist = [ | ||
'www.hulu.com', | ||
'player.hulu.com', | ||
'open.spotify.com', | ||
'player.vimeo.com', | ||
'www.youtube.com', | ||
// Instagram and Twitter don't use iframes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean we will strip out Twitter and Instagram embeds? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, how was this list of domains generated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. negative. Twitter & Instagram continue to work and can be used. Specifically, for Tweets: the Rest API has the entry content with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This list of domains was generated taking liveblogs advertised media embed features, adding them to an entry, and seeing what WordPress/oembed gave back. In writing that, I realize I need to take a closer look into these domains (ie: subdomain changes for different regions or languages) and each services documentation to ensure the domains are correct and reliable for each service. & as pointed out by @sboisvert, a filter to manage this whitelist would be needed. |
||
]; | ||
const regex = RegExp(`^(https:|)//(${iframeWhitelist.join('|')})/`, 'im'); | ||
DOMPurify.addHook('afterSanitizeAttributes', (node) => { | ||
if (node.tagName === 'IFRAME') { | ||
const iframeSrc = node.getAttribute('src'); | ||
if (iframeSrc && !iframeSrc.match(regex)) { | ||
node.removeAttribute('src'); | ||
} | ||
} | ||
}); | ||
const clean = DOMPurify.sanitize(dirty, { | ||
ADD_TAGS: ['iframe'], | ||
}); | ||
return clean; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in
embed