The document discusses approaches to writing secure frontend code in React. It outlines common client-side vulnerabilities like XSS and how to prevent them. Specifically, it discusses escaping user input, using safe templating, and only allowing known attributes in React to prevent vulnerabilities.
1 of 45
More Related Content
"Подход к написанию безопасного клиентского кода на примере React", Иван Елкин, MoscowJS 25
1. APPROACHAPPROACH
TO WRITING SECURE FRONTEND INTO WRITING SECURE FRONTEND IN
CONTEXT OF REACTCONTEXT OF REACT
Ivan Elkin
Application Security Expert,
Qiwi
2. PLANPLAN
1. Client Side vulnerabilities
2. Prevention of vulners
3. Prevention of vulners in ReactJS
1
3. CLIENT SIDE VULNERABILITIESCLIENT SIDE VULNERABILITIES
Is a lack of client-side logic or code which
provides an attack vector which affects the
end user
(in browser context )
2
and the main pain is...
12. <div>There is nothing found for query: asdasdasd"<script>alert(1)</script></div>
.html('<div>There is nothing found for query: ' + error.query + '</div>');
REFLECTED XSSREFLECTED XSS
10
?query=asdasdasd"<script>alert(1)</script>
so, if part of HTML has been injected
into your code...
23. <a id="btn-back" href=""<script>alert(1)</script>">Back to Shop</a>
DOM-BASED XSSDOM-BASED XSS
http://bank.com/payment?backUrl="><script>alert(1)</script>
"<script>alert(1)</script>
21
$('#btn-back').attr('href', getURLParameterByName('backUrl'))
but we have one more case....
29. DETECT KEYWORDSDETECT KEYWORDS
var forbidden = [
'onerror',
'onmouseover',
'onclick'
];
if (attribute in forbidden) {
return false;
}
What about onblur, onchange,
onselect, etc. ?
Black Lists
27
30. var allowed = [
'title',
'class',
'hidden',
....
];
if (attribute in allowed) {
add(attribute);
}
BE ON A WHITE SIDEBE ON A WHITE SIDE
28
35. REACT-JSREACT-JS
dangerouslySetInnerHTML
Improper use of the innerHTMLcan open you up to a
attack. Sanitizing user input for display is notoriously error-prone, and failure to properly sanitize is one
of the on the internet.
cross-site scripting
(XSS)
leading causes of web vulnerabilities
Google told me:
33
37. WHAT IS UNDER THEWHAT IS UNDER THE HOOD ?HOOD ?
35
First of all, what innerHTML is ?
w3c
returns a serialization of the node's children
using the HTML syntax
41. REACT-JS ATTRIBUTESREACT-JS ATTRIBUTES
<div data-reactid=".0">
My data
</div>
render: function () {
return (
;
}
<div class="article text-header text-bold"
onmouseenter={this.fadeIn}
onmouseleave={this.fadeOut}>
{this.state.data}
</div>
)
WAT !? 0_o
39
42. REACT-JS ATTRIBUTESREACT-JS ATTRIBUTES
SUPPORTED ATTRIBUTESSUPPORTED ATTRIBUTES
React supports all data-* and aria-* attributes as well as every attribute in the following lists.
“ Note:
All attributes are camel-cased and the attributes class and for are
className and htmlFor, respectively, to match the DOM API specification.
40
44. REACT-JS SEVERAL SIMPLE RULESREACT-JS SEVERAL SIMPLE RULES
Use safe user input by default
Use unsafe input only for special forms
Allow only known attributes
Doesn't allow inline attribute data
<div>
Hello, {{user.name}}
</div>
<img src={{user.imgSrc}} alt={{user.title}} />
<div>
Hello, <script>alert('you`ve been hacked')</script>
</div>
<img src='' alt= newChromeParam=alert('You`ve been Hacked!')/>