Avoiding XSS in React is Still Hard

Ron Perris
Apr 15, 2018 · 5 min read
Image for post
Image for post

// From https://reactjs.org/docs/react-api.html#createelement
React.createElement(
type,
[props],
[...children]
)
// Classic XSS via anchor tag href attribute.
<a href="javascript: alert(1)">Click me!</a>
// Classic XSS via anchor tag href attribute in a React component.
ReactDOM.render(
<a href="javascript: alert(1)">Click me!</a>,
document.getElementById('root')
)
const URL = require('url-parse')
const url = new URL(attackerControlled)
function isSafe(url) {
if (url.protocol === 'javascript:') return false
return true
}
isSafe(URL('javascript: alert(1)')) // Returns false
isSafe(URL('http://www.reactjs.org')) // Returns true
const URL = require('url-parse')function isSafe(url) {
if (url.protocol === 'javascript:') return false
return true
}
isSafe(URL(' javascript: alert(1)')) // Returns true
isSafe(URL('http://www.reactjs.org')) // Returns true
const URL = require('url-parse')URL(' javascript: alert(1)').protocol // Returns http:
const URL = require('url-parse')URL(' javascript: alert(1)', {}).protocol // Returns ""
const URL = require('url-parse')
const url = new URL(attackerControlled)
function isSafe(url) {
if (url.protocol === 'javascript:') return false
if (url.protocol === '') return false
return true
}
isSafe(URL('javascript: alert(1)', {})) // Returns false
isSafe(URL('http://www.reactjs.org')) // Returns true
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import URL from 'url-parse'
class SafeURL extends Component {
isSafe(dangerousURL, text) {
const url = URL(dangerousURL, {})
if (url.protocol === 'javascript:') return false
if (url.protocol === '') return false
return true
}
render() {
const dangerousURL = this.props.dangerousURL
const safeURL = this.isSafe(dangerousURL) ? dangerousURL : null
return <a href={safeURL}>{this.props.text}</a>
}
}
ReactDOM.render(
<SafeURL dangerousURL=" javascript: alert(1)" text="Click me!" />,
document.getElementById('root')
)
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
const URL = require('url-parse')class SafeURL extends Component {
isSafe(dangerousURL, text) {
const url = URL(dangerousURL, {})
if (url.protocol === 'http:') return true
if (url.protocol === 'https:') return true
return false
}
render() {
const dangerousURL = this.props.dangerousURL
const safeURL = this.isSafe(dangerousURL) ? dangerousURL : null
return <a href={safeURL}>{this.props.text}</a>
}
}
ReactDOM.render(
<SafeURL dangerousURL=" javascript: alert(1)" text="Click me!" />,
document.getElementById('root')
)

javascript-security

JavaScript Security

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store