Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React and XSS

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

React and XSS

Avatar for Yunosuke Yamada

Yunosuke Yamada

October 16, 2022
Tweet

More Decks by Yunosuke Yamada

Other Decks in Programming

Transcript

  1. XSS (Cross-site scripting) 悪意のあるスクリプトを閲覧者のブラウザで実行すること 反射型 XSS、格納型 XSS、DOM Based XSS などの種類があるが

    いずれも XSS するためには文字列として入力したスクリプトを 標的となるサイトで実行させなければならない 2
  2. 生成される HTML export default function Home() { const script =

    ` <script> while (1) { alert('!'); } </script>`; return <main>{script}</main>; } ↓ <main>&lt;script&gt; while (1) { alert(&#x27;!&#x27;); } &lt;/script&gt;</main> 4
  3. dangerouslySetInnerHTML https://ja.reactjs.org/docs/dom- elements.html#dangerouslysetinnerhtml export default function Home() { const script

    = ... const html = { __html: script }; return ( <main> <div dangerouslySetInnerHTML={html} />; </main> ); } 6
  4. href, src export default function Home() { const script =

    ` javascript: while (1) { alert('!'); }`; return ( <main> <a href={script}>link</a> </main> ); } 7
  5. import DOMPurify from "isomorphic-dompurify"; export default function Home() { const

    script = "<script>...</script>Hello"; const html = { __html: DOMPurify.sanitize(script) }; return ( <main> <div dangerouslySetInnerHTML={html} /> </main> ); } ↓ <main><div>Hello</div></main> 11