Get a Pentest and security assessment of your IT network.

Cyber Security

One-Time IDs: Website Authentication Security

TL;DR

Using a one-time ID in the querystring to authenticate users on a second website is not secure. It’s vulnerable to several attacks, including session hijacking and replay attacks. You should use proper authentication methods like OAuth 2.0 or OpenID Connect.

Why One-Time IDs in the Querystring are Bad

Sending an ID through the URL (the querystring) has significant security risks:

  • Visible to Users & Logs: The ID is part of the browser history and server logs, making it easy for attackers to find.
  • Easy to Share/Re-use: Anyone with access to the URL can use the ID.
  • Session Hijacking: If an attacker gets hold of a user’s session ID (even indirectly), they can impersonate that user.
  • Replay Attacks: An attacker could simply reuse the same ID multiple times if it isn’t properly invalidated after use.

Step-by-Step Guide to Secure Authentication

Here’s how to improve your authentication process:

1. Understand the Risks

Before implementing any solution, be clear about what you’re trying to protect and who you’re protecting it from.

2. Choose a Proper Authentication Protocol

Avoid custom solutions like one-time IDs in the querystring. Use established standards:

  • OAuth 2.0: Allows users to grant limited access to their resources on one site (e.g., Google, Facebook) to another site without sharing passwords.
  • OpenID Connect (OIDC): An identity layer built on top of OAuth 2.0 that provides user authentication information.
  • Session Cookies with HTTPOnly and Secure Flags: If you need a simpler solution for your own websites, use session cookies but configure them correctly (see step 4).

3. Implement the Chosen Protocol

The implementation details depend on the protocol you choose. Here’s a general outline using OAuth 2.0:

  1. Register your application: Register your second website as an application with the identity provider (e.g., Google, Facebook). This will give you a Client ID and Client Secret.
  2. Redirect to the Identity Provider: When a user needs to log in, redirect them to the identity provider’s authorization endpoint.
  3. Handle the Callback: After authentication, the identity provider redirects the user back to your website with an Authorization Code.
  4. Exchange the Code for Tokens: Exchange the Authorization Code for Access and Refresh Tokens using your Client ID and Secret.
  5. Use the Access Token: Use the Access Token to access protected resources on behalf of the user.

4. Secure Session Management (If Using Cookies)

If you’re not using OAuth 2.0/OIDC and must use cookies, follow these best practices:

  • HTTPOnly Flag: Prevents JavaScript from accessing the cookie, mitigating XSS attacks.
    Set-Cookie: sessionid=abcdefg; HttpOnly
  • Secure Flag: Ensures the cookie is only transmitted over HTTPS, protecting against eavesdropping.
    Set-Cookie: sessionid=abcdefg; Secure
  • SameSite Attribute: Controls whether cookies are sent with cross-site requests. Use Strict or Lax to prevent CSRF attacks.
    Set-Cookie: sessionid=abcdefg; SameSite=Strict
  • Session Expiration: Set a reasonable expiration time for sessions.
  • Regenerate Session IDs: Regenerate the session ID after login to prevent session fixation attacks.
    // Example in PHP
    session_regenerate_id(true);

5. Validate Tokens/Sessions

Always validate tokens or sessions before granting access to protected resources.

  • Token Validation: Verify the signature, expiration time, and audience of OAuth 2.0 tokens.
  • Session Validation: Check if the session ID exists in your database and hasn’t expired.

6. Use HTTPS

Always use HTTPS to encrypt all communication between your website and users.

Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation