Security is one of the most important and most challenging tasks faced by an angularjs developer. It’s not that the developer is actually responsible for implementing the security layer—that is not the case at all—but it is very important for an angularjs developer to understand the role that angularjs plays in the overall security model of an application or website.

You should never attempt to implement an independent client-side security layer in an angularjs application, or any other JavaScript application for that matter. Security should always be implemented on the backend services where the data resides. That is the only safe place to implement a security layer.

Remember the user has full access to the JavaScript running in the browser. As I said before, our angularjs application runs in the user’s browser on the user’s hardware. The user can save the JavaScript locally and easily make modifications circumventing any security layer implemented by an unsuspecting JavaScript developer.

With that in mind, there are several rules that angularjs developers and backend developers need to remember. Although actually implementing the security layer is not usually the job of an angularjs developer, it is often a collaborative effort for all developers involved in a project. The following rules should always be considered:

  1. Always use SSL to communicate with REST services that contain private data (HTTPS).
  2. Always use some type of authentication on each REST service call that contains private data (Basic Authentication, for example).
  3. Never hold REST service authentication status in a session variable on the server. Doing that opens your server-side application up to cross-origin attacks and other serious security concerns.
  4. Never implement a Cross-Origin Resource Sharing (CORS) layer that returns * as the list of allowed domains. For example, (Access-Control-Allow-Origin: *) would allow all domains to make cross-origin calls to the REST services on the site. Doing that circumvents the browser’s CORS security implementation completely.
  5. Always make sure that any JavaScript that may get injected inside a JSON property does not get executed on the server side. This design flaw is at the core of the NoSQL injection attack, where JavaScript functions are injected in the JSON request of a service and unknowingly executed by the server, in order to breach the security of a NoSQL database.

Always remember that any security-related JavaScript code can be viewed and modified by the user. While most modern browsers do offer built-in security, JavaScript developers should never rely on the browser for security. The responsibility for security rests entirely on the shoulders of the backend service developers. With that said, I will show some techniques for developing angularjs applications that work well with a security layer implemented properly in the backend services.

One Last Point on Security

I want to emphasize one last thing about implementing security in a JavaScript application. Any security that you implement in JavaScript can be circumvented by the user,

The login screen is used just as a way to gather and store the user’s credentials in a safe place temporarily and to control the authentication process for each REST service that contains private data. The user’s credentials are removed after each session and have to be entered again at each login, unless the user chooses to save their credentials.

Here is the complete Tutorial for setting up authentication and security in your angularjs App