React

React(XSS)

2017-10-22  本文已影响276人  余生筑

方方老师的解释

官方解释

Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications. XSS enables attackers to inject client-side scripts into web pages viewed by other users. A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same-origin policy.

概要

预防

jsx可以有效预防XSS

By default, React DOM escapes any values embedded in JSX before rendering them. Thus it ensures that you can never inject anything that’s not explicitly written in your application. Everything is converted to a string before being rendered.

var content='<strong>content</strong>';

React.render(
    <div>{content}</div>,
    document.body
);

输出内容为

<strong>content</strong>

dangerouslySetInnerHTML

如果你想在jsx中放入一个html标签,有两种方法

render() {
        return (
            <div>
            <strong>ddd</strong>
            </div>
            );
        } 
    }
function createMarkup() {
  return {__html: '<strong>ss</strong>'};
}

function Bpp() {
  return <div dangerouslySetInnerHTML={createMarkup()} />;
}
上一篇下一篇

猜你喜欢

热点阅读