An AngularJS web app finally results in a Single Page Web Application, managing it’s own routes, states (models), incorporating workflows, CRUD operations and much more around some tasks. So, primarily we have to concentrate on the common flaws found in an web app and then figure out what kind of direct or derived vulnerability may be caused in our app.
Eavesdropping: It actually happens in network layer. It has nothing to do with the coding done in the app.
Solution: If there is no direct communication between users via the app, it’s a standard to deliver the app over SSL. But solution for this kind of attack is a combination of configurations starting from DNS setting to but not limited to use of HTTPS. (See also)
Cross Site Scripting(XSS): This is where an attacker can inject scripts into a page sent by the server and browser treats the injected script as any other scripts inside the page.
Cross Site Request Forgery(CSRF): A Cross-site request forgery is when a malicious site can use a visitor’s browser to make a request to your server that causes a change on the server. The server thinks that because the request comes with the user’s cookies, the user wanted to submit that form.
Cross Site Scripting(XSS)
Use Case: Showing menu based on privileges. Users with malicious mind-set can change some of the variables, such that, non privileged members can also see the higher privileged tabs.
Solution: Send or build the template, so it would only generate what is required. Nothing more. Ng-repeat may be a good resort here.
Use Case: Generation of some part of the page, depending on user input. One may input HTML to change the behaviour of the section.
Solution: Treat the user input as strings and do not bind the user input as ng-bind-html
Use Case: User may input some code as the query parameters like:
http://abc.com?<script>alert(5)</script> returns "<p>There were no hits for <script>alert(5)</script>.</p>"
Solution: Use route parameters instead of query parameters. If it’s inevitable to use the query params, do not run eval on the string, use it as a string and of course check for the tags.
- Set correct mime type of the content, exchanged to-and-forth client and server
- If html templates are compiled into templateCache, correctly obfuscate the HTML replacing tags with HTML codes like,
<should be replaced by
- Depending upon the sensitivity of data, passing to the server, it may be required to encrypt the same
- Put each of the piece of the code into a closure, viz. an IIFE and if possible put ‘use strict’ at the top of each of them
- Use dependency injection into every providers. Life would be easy after minification
- If non obfuscated code needs to be used in client side, make the identifier names, as neutral possible. Use ‘re4ty_ft’ instead of ‘carPrice’
- Disable autocomplete in sensitive information fields. User may have to key-in (even can’t paste. Refer to CITI Bank’s login page) or press keys in a virtual keyboard
- It’s not advisable to put any files containing user-restricted-data in the webroot
- Don’t store information in a global variable
Cross Site Request Forgery(CSRF)
This form of attack includes but not limited to:
- Access cookie to change preferences
- Logout the victim
- Post a comment on behalf of the victim
- Use victim as a proxy to get data from the site
- Perform distributed password guessing attack, without even a pass hacker tool
- Send a login hash along with a API key each time client calls the server
- Logout must be done, jointly by client and server, in both ways
- Best not to use a cookie
- For login and sensitive operations, use captcha, or two-factor-authentication apps like DUO
- It’s better to use two different (.aspx or .php whatever) pages for login and app
- No state should be persisted in the server, I.e. to say the REST API should implement a complete REST architecture
- Encourage the users to close the window after logout (Currently Google Chrome does not close the tab/window invoking window.close command)
- Make one landing page and from there on click of a button open a new address bar disabled window to redirect to the app