Blog | G5 Cyber Security

Secure Client-Side Token Storage

TL;DR

Storing authentication tokens securely on the client-side is tricky. Local Storage is generally okay for simple apps, but has risks. Cookies (with HttpOnly and Secure flags) are better for web apps, especially those handling sensitive data. Consider using a dedicated library like js-cookie or Universal Cookie to simplify cookie management. Always use HTTPS.

Understanding the Risks

Client-side storage means the token lives on the user’s machine. This makes it vulnerable to:

Storage Options Compared

  1. Local Storage:
    • Pros: Simple to use, large storage capacity.
    • Cons: Accessible by JavaScript from any script on the same domain (XSS risk). Not automatically sent with HTTP requests.
    • Example:
      localStorage.setItem('authToken', 'your_token');
      const token = localStorage.getItem('authToken');
  2. Session Storage:
    • Pros: Similar to Local Storage, but data is cleared when the browser tab/window is closed.
    • Cons: Same XSS risks as Local Storage. Limited lifespan.
    • Example:
      sessionStorage.setItem('authToken', 'your_token');
      const token = sessionStorage.getItem('authToken');
  3. Cookies:
    • Pros: Can be marked as HttpOnly (prevents JavaScript access) and Secure (only sent over HTTPS). Automatically sent with HTTP requests. CSRF protection possible with SameSite attribute.
    • Cons: Smaller storage capacity than Local Storage. Can be more complex to manage.
    • Example (using js-cookie library):
      // Install: npm install js-cookie
      import Cookies from 'js-cookie';
      
      Cookies.set('authToken', 'your_token', { secure: true, httpOnly: true, sameSite: 'strict' });
      const token = Cookies.get('authToken');

Step-by-Step Guide to Secure Cookie Storage

  1. Install a cookie library: We recommend js-cookie for its simplicity.
    npm install js-cookie
  2. Set the cookie with security flags: Use the secure and httpOnly flags. The sameSite attribute is also important to prevent CSRF attacks.
    import Cookies from 'js-cookie';
    Cookies.set('authToken', 'your_token', { secure: true, httpOnly: true, sameSite: 'strict' });
  3. Retrieve the cookie:
    const token = Cookies.get('authToken');
  4. Send the token with requests: The browser automatically includes cookies in HTTP requests to the same domain.
  5. Implement CSRF protection: Use a CSRF token generated server-side and included in forms/requests. Verify this token on the server before processing any sensitive actions.

Important Considerations

Exit mobile version