SlideShare a Scribd company logo
APPROACHAPPROACH
TO WRITING SECURE FRONTEND INTO WRITING SECURE FRONTEND IN
CONTEXT OF REACTCONTEXT OF REACT
Ivan Elkin
Application Security Expert, Qiwi
PLANPLAN
1. Client Side vulnerabilities
2. Prevention of vulners
3. Prevention of vulners in ReactJS
1
CLIENT SIDE VULNERABILITIESCLIENT SIDE VULNERABILITIES
Smth what goes wrong in a client-side code
2
XSSXSS
3
XSSXSS
In a common cases is a type of WEB-attack where malicious code
injected in a client-side code
4
XSS TYPESXSS TYPES
Reflected
Stored
DOM-based
5
REFLECTED XSSREFLECTED XSS
Attack injection when malicious code has been injected
after single HTTP request
6
REFLECTED XSSREFLECTED XSS
onResponse: function(response) {
var url = document.location;
var username = getQueryParam(url, 'username');
var tpl = '<span>Hello {username} !</span>';
document.write(tpl.replace(/{username}/, username));
}
http://socialnetwork.com/UserSearch?query=asd%22%3Cscript%3Ealert(1)%3C/script%3E
7
REFLECTED XSSREFLECTED XSS
8
$.get('/search?query=' + query) {
success: function(user) {
$('.search-list')
.append('<div>username: ' + data.user.name + '</div>')
.append('<div>username: ' + data.user.email + '</div>')
}
failure: function(error) {
$('.search-list')
.html('<div>There is nothing found for query: ' + error.query + '</div>');
}
}
REFLECTED XSSREFLECTED XSS
9
asdasdasd"<script>alert(1)</script>"
.html('<div>There is nothing found for query: ' + error.query + '</div>');
REFLECTED XSSREFLECTED XSS
10
REFLECTED XSSREFLECTED XSS
11
STORED XSSSTORED XSS
Attack injection, when malicious code has
been stored on server in advance
12
STORED XSSSTORED XSS
Samy...
13
STORED XSSSTORED XSS
14
<script type="text/javascript">
$.get('user_list', function(data) {
var data = JSON.parse(data);
$.each(data, function(key, user) {
$('.list-group').append(
'<div class="user">' +
'<div>User Name: ' + user.name + '</div>' +
'<div>User e-mail: ' + user.email + '</div>'
'</div>'
);
});
});
</script>
STORED XSSSTORED XSS
15
STORED XSSSTORED XSS
16
STORED XSSSTORED XSS
17
DOM-BASED XSSDOM-BASED XSS
Unlike Reflected and Stored XSS, can work without
interaction with the server
18
http://bank.com/payment?backUrl=http://market.com"
DOM-BASED XSSDOM-BASED XSS
19
$(document).ready(function() {
$('#btn-back').attr('href', getURLParameterByName('backUrl'))
});
DOM-BASED XSSDOM-BASED XSS
20
DOM-BASED XSSDOM-BASED XSS
http://bank.com/payment?backUrl="<script>alert(1)</script>
&quot;&lt;script&gt;alert(1)&lt;/script&gt;
<a id="btn-back" href="&quot;&lt;script&gt;alert(1)&lt;/script&gt;">Back to Shop</a>
21
$('#btn-back').attr('href', getURLParameterByName('backUrl'))
http://bank.com/payment?backUrl=data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==
<a id="btn-back" href="data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==">Back to Shop</a>
atob('PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg== ')
"<script>alert(1)</script>"
DOM-BASED XSSDOM-BASED XSS
Base64
22
HOW TO PROTECT MY SITE?HOW TO PROTECT MY SITE?
Escaping HTML
Escaping Attributes
Escaping JS data
Escaping JSON data
Escaping CSS data
23
ESCAPINGESCAPING
& --> &amp;
< --> &lt;
> --> &gt;
" --> &quot;
' --> &#x27;
/ --> &#x2F;
24
SAFE TEMPLATINGSAFE TEMPLATING
<div class="json-data">
<%= data.to_json %>;
</div>
btw, don't make such mistakes
25
DETECT KEYWORDSDETECT KEYWORDS
<div class="avatar">
<img alt={{avatar.name}} src={{avatar.image}}>
</div>
<div class="avatar">
<img alt= onerror=alert(1) src=>
</div>
avatar: {
name: ' onerror=alert(1)'
image: ''
}
avatar: {
name: 'Me in Italy'
image: '/images/aajkdshf2347jk/avatar.jpg'
}
Handlebars, XTemplate and some others...
26
DETECT KEYWORDSDETECT KEYWORDS
var forbidden = [
'onerror',
'onmouseover',
'onclick'
];
if (attribute in forbidden) {
return false;
}
What about onblur, onchange, onselect, etc. ?
Black Lists
27
var allowed = [
'title',
'class',
'hidden',
....
];
if (attribute in allowed) {
add(attribute);
}
BE ON A WHITE SIDEBE ON A WHITE SIDE
28
REACT-JSREACT-JS
one more JS framework...
...but it uses .jsx
29
REACT-JSREACT-JS
componentDidMount: function() {
this.setState({
data: 'A long time ago in a galaxy far,<br/> far away...'
})
},
render: function () {
return (
<Article className="article">
{this.state.data}
</Article>
);
}
30
REACT-JSREACT-JS
31
REACT-JSREACT-JS
<div class="article" data-reactid=".0">
A long time ago in a galaxy far,&lt;br&gt; far away...
</div>
32
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
FACEBOOK TELLSFACEBOOK TELLS
function createMarkup() {
return {
__html: 'First · Second'
};
};
<div dangerouslySetInnerHTML={createMarkup()} />
34
REACT-JSREACT-JS
/**
* @param {DOMElement} node
* @param {string} html
* @internal
*/
var setInnerHTML = function(node, html) {
node.innerHTML = html;
};
if you use dangerouslySetInnerHTML ...
w3c
returns a serialization of the node's children using
the HTML syntax
35
REACT-JSREACT-JS
/**
* @param {DOMElement} node
* @param {string} text
* @internal
*/
var setTextContent = function(node, text) {
node.textContent = text;
};
otherwise...
w3c
returns the text content of this node and its
descendants...
< == &lt; > == &gt;
36
REACT-JS ATTRIBUTESREACT-JS ATTRIBUTES
37
REACT-JS ATTRIBUTESREACT-JS ATTRIBUTES
<div data-reactid=".0">A long time ago &middot; in a galaxy far, &lt;br&#x2F;&gt; far away...</div>
render: function () {
return (
;
}
<div class="article text-header text-bold" onmouseenter={this.fadeIn} onmouseleave={this.fadeOut}>
{this.state.data}
</div>
)
WAT !? 0_o
38
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.
39
REACT-JS ATTRIBUTESREACT-JS ATTRIBUTES
var HTMLDOMPropertyConfig = {
isCustomAttribute: RegExp.prototype.test.bind(
/^(data|aria)-[a-z_][a-zd_.-]*$/
),
Properties: {
/**
* Standard Properties
*/
accept: null,
acceptCharset: null,
accessKey: null,
action: null,
allowFullScreen: MUST_USE_ATTRIBUTE | HAS_BOOLEAN_VALUE,
allowTransparency: MUST_USE_ATTRIBUTE,
alt: null,
async: HAS_BOOLEAN_VALUE,
autoComplete: null,
// autoFocus is polyfilled/normalized by AutoFocusMixin
// autoFocus: HAS_BOOLEAN_VALUE,
autoPlay: HAS_BOOLEAN_VALUE,
cellPadding: null,
cellSpacing: null,
charSet: MUST_USE_ATTRIBUTE,
checked: MUST_USE_PROPERTY | HAS_BOOLEAN_VALUE,
classID: MUST_USE_ATTRIBUTE,
40
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
41
<EndFrame onQuestions={this.doAnswer}>
<Title>
Thanx for your attention!
</Title>
</EndFrame>

More Related Content

PDF
"Подход к написанию безопасного клиентского кода на примере React", Иван Елки...
PDF
WebCamp: Developer Day: Web Security: Cookies, Domains and CORS - Юрий Чайков...
PDF
Web Security Horror Stories
PPTX
PDF
Google chrome presentation
PPT
"Подход к написанию безопасного клиентского кода на примере React", Иван Елки...
WebCamp: Developer Day: Web Security: Cookies, Domains and CORS - Юрий Чайков...
Web Security Horror Stories
Google chrome presentation

Similar to mjs (20)

PDF
ng-owasp: OWASP Top 10 for AngularJS Applications
PDF
Complete xss walkthrough
PPT
XSS - Attacks & Defense
PPT
Jim Manico: Developer Top 10 Core Controls, web application security @ OWASP ...
PPTX
Security in NodeJS applications
PPT
Introduction to java script Lecture 4.ppt
PDF
Null Mumbai 14th May Lesser Known Webapp attacks by Ninad Sarang
PDF
Null 14 may_lesser_known_attacks_by_ninadsarang
PPT
Javascript for beggineers Lecture 4 (1).ppt
PPT
Introduction to Scripting programming Language.
ODP
Top 10 Web Security Vulnerabilities
PDF
ClubHack Magazine issue April 2012
PPT
PHPUG Presentation
PDF
Application Security around OWASP Top 10
PPTX
Managing State in React Apps with RxJS by James Wright at FrontCon 2019
PPT
Securing Java EE Web Apps
PPT
JS-Lecture Lecture PPT contains Control Structures DOM
PPT
Lecture 4.javascriptpractcieandtheoryppt
PPTX
Solving anything in VCL
PDF
10 Excellent Ways to Secure Your Spring Boot Application - Devoxx Belgium 2019
ng-owasp: OWASP Top 10 for AngularJS Applications
Complete xss walkthrough
XSS - Attacks & Defense
Jim Manico: Developer Top 10 Core Controls, web application security @ OWASP ...
Security in NodeJS applications
Introduction to java script Lecture 4.ppt
Null Mumbai 14th May Lesser Known Webapp attacks by Ninad Sarang
Null 14 may_lesser_known_attacks_by_ninadsarang
Javascript for beggineers Lecture 4 (1).ppt
Introduction to Scripting programming Language.
Top 10 Web Security Vulnerabilities
ClubHack Magazine issue April 2012
PHPUG Presentation
Application Security around OWASP Top 10
Managing State in React Apps with RxJS by James Wright at FrontCon 2019
Securing Java EE Web Apps
JS-Lecture Lecture PPT contains Control Structures DOM
Lecture 4.javascriptpractcieandtheoryppt
Solving anything in VCL
10 Excellent Ways to Secure Your Spring Boot Application - Devoxx Belgium 2019
Ad

mjs