32
@PhilippeDeRyck ARE Y OU BOTCHING THE SECURITY OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium https://www.websec.be

A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

ARE YOU BOTCHING THE SECURITYOF YOUR ANGULARJSAPPLICATIONS?

PhilippeDeRyckDevFest 2016,Brussels,Belgium

https://www.websec.be

Page 2: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

WHAT IS THE BIGGEST THREAT TO AN ANGULARJSAPPLICATION?

DEVELOPERS (THAT ARE NOT SECURITY-AWARE)

Page 3: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

KNOWLEDGE IS KEY TO BUILDING SECURE APPLICATIONS

§Mygoalistohelpyoubuildsecurewebapplications− In-housetrainingprogramsatvariouscompanies−HostedwebsecuritytrainingcoursesatDistriNet (KULeuven)− Talksatvariousdeveloperconferences− Slides,videosandblogpostsonhttps://www.websec.be

§ Ihaveabroadsecurityexpertise,withafocusonWebSecurity−PhDinclient-sidewebsecurity−MainauthorofthePrimeronclient-sidewebsecurity−PartoftheorganizingcommitteeoftheSecAppDev course

§ I’malsoachef,sodemo’swillbefood-related!

Page 4: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

CROSS-SITE SCRIPTING (XSS)

§ InanXSSattack,maliciouscontentisinjectedintoyourapplication’spages− Inthe“original”XSSattacks,anattackerinjectedJavaScriptcode− Today,injectedcontentcanbeJavaScript,CSS,HTML,SVG,…

Page 5: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

CROSS-SITE SCRIPTING (XSS)

§ InanXSSattack,maliciouscontentisinjectedintoyourapplication’spages− Inthe“original”XSSattacks,anattackerinjectedJavaScriptcode− Today,injectedcontentcanbeJavaScript,CSS,HTML,SVG,…

https://www.youtube.com/watch?v=K0noqLisW_c

Page 6: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

CROSS-SITE SCRIPTING (XSS)

§ InanXSSattack,maliciouscontentisinjectedintoyourapplication’spages− Inthe“original”XSSattacks,anattackerinjectedJavaScriptcode− Today,injectedcontentcanbeJavaScript,CSS,HTML,SVG,…

§ Therealproblemisthatinjectedcontentrunsinyourcontext−Completeaccesstoyourclient-sidedataandcode−Abilitytouseanypermissionstheuserhasgrantedtoyourapplication− ThefullpowerofXHRtocontactyourbackend,inthenameoftheuser

§ XSSattacksareverypowerful,andunfortunatelyverycommon−XSSisranked3rd intheOWASPtop10and4th intheSANStop25

Page 7: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

http://colesec.inventedtheinternet.com/beef-the-browser-exploitation-framework-project/

Page 8: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

HOW DO YOU PROTECT AGAINST XSS?

§ TherootcausebehindXSSisconfusionbetweendataandcode−Untrusteddataismixedwithtrustedcode,andsenttothebrowser− Thebrowserwillneverknowwhichpartisdataandwhichiscode

<div><h3>Your search for “<i>Crazy Cats<script>alert(“Miauw!”)</script></i>”returned 5 results

</h3></div>

<div><h3>Your search for “$query” returned $count results

</h3></div>

Page 9: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

HOW DO YOU PROTECT AGAINST XSS?

§ TherootcausebehindXSSisconfusionbetweendataandcode−Untrusteddataismixedwithtrustedcode,andsenttothebrowser− Thebrowserwillneverknowwhichpartisdataandwhichiscode

§ Theserverneedstorenderthedataharmless−Byescaping“dangerous”partsinthedata

<div><h3>Your search for “encode($query)” returned $count results

</h3></div>

Page 10: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

HOW DO YOU PROTECT AGAINST XSS?

§ TherootcausebehindXSSisconfusionbetweendataandcode−Untrusteddataismixedwithtrustedcode,andsenttothebrowser− Thebrowserwillneverknowwhichpartisdataandwhichiscode

§ Theserverneedstorenderthedataharmless−Byescaping“dangerous”partsinthedata

<div><h3>Your search for “<i>Crazy Cats&lt;script&gt;alert(“Miauw!”)&lt;/script&gt;</i>returned 5 results

</h3></div>

Page 11: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

HOW DO YOU PROTECT AGAINST XSS?

§ TherootcausebehindXSSisconfusionbetweendataandcode−Untrusteddataismixedwithtrustedcode,andsenttothebrowser− Thebrowserwillneverknowwhichpartisdataandwhichiscode

§ Theserverneedstorenderthedataharmless−Byescaping“dangerous”partsinthedata

§ Theescapingprocessiscontext-sensitive−HTMLbody <h1>DATA</h1>−HTMLattributes <div id=‘DATA’>− Stylesheetcontext body { background-color: DATA;}− Scriptcontext alert(“DATA”);

Page 12: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

SO,WHAT’S THE DEAL WITH ANGULARJS?

§ AngularJSisoftenusedasalibrarywithintraditionalapplications− TheserverbuildsanHTMLpage,includingAngularJStemplates− Theserverneedstorenderuser-supplieddataharmlesstoprotectagainstXSS

<script src=“…/angular.js”></script>…<div><h3>

Your search for “<i>encode($query)</i>” returned $count results

</h3></div>

Page 13: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

SO,WHAT’S THE DEAL WITH ANGULARJS?

§ AngularJSisoftenusedasalibrarywithintraditionalapplications− TheserverbuildsanHTMLpage,includingAngularJStemplates− Theserverneedstorenderuser-supplieddataharmlesstoprotectagainstXSS

§ ButisthatevenpossibleinanAngularJSenvironment?

<div class=”ng-app”>{{constructor.constructor(‘alert(1)’)}}

</div>

<div class="ng-app"><b class="ng-style: {x:constructor.constructor('alert(1)')()};" />

</div>

Page 14: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

SO,WHAT’S THE DEAL WITH ANGULARJS?

§ AngularJSisoftenusedasalibrarywithintraditionalapplications− TheserverbuildsanHTMLpage,includingAngularJStemplates− Theserverneedstorenderuser-supplieddataharmlesstoprotectagainstXSS

§ ButisthatevenpossibleinanAngularJSenvironment?−No

§ AngularJSattemptedtopreventthiswiththeexpressionsandbox−PreventsdirectaccesstoglobalJavaScriptfunctionality− Impossibletolockdowncompletely,soonlyavailableinAngularJS1.2- 1.6−Angular2offersofflinetemplatecompilation

Page 15: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

RULE#1

DO NOT COMBINE TEMPLATES WITH USER-SUPPLIED DATA ON THE SERVER

Providethedataseparatelytotheclient-sideAngularJSapplication

Page 16: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

AND WHAT IF WE DO IT THE ANGULAR WAY?

§ Remembertheconfusionbetweendataandcode?− TemplatesandJavaScriptcodeareconsideredtheapplication’scode−DatafetchedfromAPIsisconsidereddata

§ AngularJSknowswhichpartsareuntrusted−AndautomaticallyappliesStrictContextualEscaping(SCE)− SCEappliestoalldatabindingswithng-bind or{{ }}− SCEison-by-defaultsinceversion1.2

§ ButwhatifweactuallywanttoallowsomeHTMLintheuser’sdata?

Page 17: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

Page 18: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

http://stackoverflow.com/questions/9381926/angularjs-insert-html-into-view/25513186#25513186

Page 19: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

ALL IS GREAT …UNTIL YOU GET A CALL ONE EVENING

What,noway!Whathappened?Didtheystealourdata?

No,it’sworse!Muchworse!

TheyloadedtheEmberJSlibrary!

We’vebeenhacked!

Thenwhat?!

Page 20: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

LET’S INVESTIGATE THE STACKOVERFLOW ADVICE …

https://docs.angularjs.org/api/ng/service/$scehttps://docs.angularjs.org/error/$sce/unsafe

Page 21: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

LETTING ANGULARJS1.X DO THE WORK FOR YOU

§ SimpledatawillbeencodedfortherightcontextwithSCE

§ AngularJSwillnotallowyoutodirectlyuseuntrusteddata

§ Sanitizinguntrusteddatamakesitsafetouse

§ StaticHTMLsnippetscanbemarkedassafeifabsolutelynecessary

<p>{{var}}</p>var = “test<script>alert(1)</script>”

<p ng-bind-html=“var”></p><input ng-model=“var” />

<input ng-model=“var” /> angular.module(“…”, [‘ngSanitize’]<p ng-bind-html=“var”></p>

<p ng-bind-html=“var”></p>var = $sce.trustAsHtml(“<b>test</b>)”

Page 22: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

AND IT’S EVEN BETTER IN ANGULARJS2.X

§ Alldataissanitizedbydefault

§ StaticHTMLsnippetscanbemarkedassafeifabsolutelynecessary

<input ng-model=“var” /> <p>{{var}}</p>

<p>{{var}}</p>var = domSanitizer.bypassSecurityTrustHtml(“<b>test</b>)”

Page 23: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

http://stackoverflow.com/a/25513186

Page 24: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

RULE#2

DO NOT MARK UNTRUSTED DATA AS SAFE

Usethebuilt-insanitizertoremovedangerousfeaturesfromtheuntrusteddata

Page 25: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

XSSVULNERABILITIES WILL POP UP EVENTUALLY

§ YoucandeployasecondlineofdefensewithContentSecurityPolicy− Server-drivenbrowser-enforcedsecuritypolicy− IncasethereisanXSSattack,thescriptwillbeseverelyconstrainedorevenblocked−CSPevolvedtoanextensiveandpowerfulbrowsersecuritypolicy

§ CSPlocksdownwhatcanhappeninawebpage−Refusestoexecuteinlinescriptandstyle−Onlyloadsexternalresourcesiftheyareexplicitlywhitelisted

§ CSPhassevereincompatibilityproblemswithtraditionalwebapplications−ButiseasytodeployonanAngularJSapplication

Page 26: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

AQUICK OVERVIEW OF CSP’S DIRECTIVES

§ CSPhasdirectivesforallkindsofresources− default-src appliestoanyresource,ifthere’snomorespecific directive− img-src,script-src,style-src,…

§ Adirectivecanhavenumerousvalidvalues−Keywords:‘none’,‘self’,*− Expressions:https://websec.be,https:,https://websec.be/jquery.js,*.websec.be

§ Ifabsolutelynecessary,youcanre-enableinlinescripts,stylesandeval−Byaddingthe‘unsafe-inline’or‘unsafe-eval’keywordstothedirectives

Page 27: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

BROWSER SUPPORT FOR CSPLEVEL 1IS AWESOME

http://caniuse.com/#search=csp

Page 28: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

WRITING SANE CSPPOLICIES

§ DeployCSPusingtheContent-Security-Policy responseheader− <meta> tagsareagoodalternativeifheadersaretoodifficulttouse

§Makeyourpolicyassecureaspossible−Avoid‘unsafe-inline’and‘unsafe-eval’,especiallyforscripts−Bespecificaboutwhichfilesyouwanttoincludetoavoidbypassattacks−Defineallimportantdirectivestoavoidoverrideattackswith<meta> tags

§Useavailabletoolsandfeaturestomakeyourlifeeasier−Google’sCSPEvaluator−Report-uri.io forpolicygeneration&reportcollection

https://csp-evaluator.withgoogle.com/https://report-uri.io/

Page 29: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

RULE#3

DO NOT IGNORE THE TREMENDOUS POWER OF CSP

Makesureyourappsarecompatible,andlockdownyourCSPpolicy

Page 30: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

THE FOCUS HERE TODAY WAS PURELY ON ANGULARJS

§ Butthere’salotmoretobuildingasecureapplication− Thewebhasevolvedalotinthelastfewyears−Plentyofnewthreats,butalsoplentyofnewsecuritytechnologies

§ EssentialsecurityprinciplestoapplytoyourAngularJSapplications−DeployyourapplicationsoverHTTPS−Usestrongauthenticationmechanisms−Performaccesscontrolintherightplaces,withtherightdata−Protectagainstcommonthreatsagainstsessionmanagement

Page 31: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

ADDITIONAL INFORMATION

§ I’mrunninga2-daywebsecuritycourseonDecember6and7− Informationandregistrationonhttps://essentials.websec.be

§ https://www.websec.be containsalotofinformationaboutwebsecurity− Theslidesforthistalkwillbeavailablethere− Slidedecks,videosandblogpostsaboutvarioussecuritytopics− Subscribetothemailinglisttostayuptodate

§ Feelfreetocontactmewithfeedback,questions,orspeakinginvitations

[email protected] /in/philippederyck

Page 32: A YOU BOTCHING THE ECURITY ANGULARJS APPLICATIONS · OF YOUR ANGULARJS APPLICATIONS? Philippe De Ryck DevFest 2016, Brussels, Belgium ... −XSS is ranked 3rd in the OWASP top 10

@PhilippeDeRyck

THE RULES OF ANGULARJSSECURITY

#3 DO NOT IGNORE THE TREMENDOUS POWER OF CSP

#2 DO NOT MARK UNTRUSTED DATA AS SAFE

#1 DO NOT COMBINE TEMPLATES WITH USER-SUPPLIED DATA

#0 YOU TELL ALL YOUR FRIENDS ABOUT THESE RULES!