<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hxa</title>
    <description>The latest articles on DEV Community by Hxa (@hxa).</description>
    <link>https://dev.to/hxa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4168777%2F2b98dc40-ce80-4040-818f-331fc7aa709e.png</url>
      <title>DEV Community: Hxa</title>
      <link>https://dev.to/hxa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hxa"/>
    <language>en</language>
    <item>
      <title>What developers need to know</title>
      <dc:creator>Hxa</dc:creator>
      <pubDate>Wed, 07 Oct 2026 12:13:09 +0000</pubDate>
      <link>https://dev.to/hxa/-5b9b</link>
      <guid>https://dev.to/hxa/-5b9b</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/hxa/infoleak-prevention-for-authentication-system-118g" class="crayons-story__hidden-navigation-link"&gt;Infoleak Prevention for Authentication System&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/hxa" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4168777%2F2b98dc40-ce80-4040-818f-331fc7aa709e.png" alt="hxa profile" class="crayons-avatar__image" width="800" height="800"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/hxa" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Hxa
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Hxa
                
                
              
              &lt;div id="story-author-preview-content-4812125" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/hxa" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4168777%2F2b98dc40-ce80-4040-818f-331fc7aa709e.png" class="crayons-avatar__image" alt="" width="800" height="800"&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Hxa&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/hxa/infoleak-prevention-for-authentication-system-118g" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Oct 7&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/hxa/infoleak-prevention-for-authentication-system-118g" id="article-link-4812125"&gt;
          Infoleak Prevention for Authentication System
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/architecture"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;architecture&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/cybersecurity"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;cybersecurity&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/security"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;security&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
            &lt;a href="https://dev.to/hxa/infoleak-prevention-for-authentication-system-118g#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              

              &lt;span class="hidden s:inline"&gt;Add&amp;nbsp;Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            8 min read
          &lt;/small&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>authentication</category>
      <category>privacy</category>
      <category>security</category>
    </item>
    <item>
      <title>Infoleak Prevention for Authentication System</title>
      <dc:creator>Hxa</dc:creator>
      <pubDate>Wed, 07 Oct 2026 12:03:56 +0000</pubDate>
      <link>https://dev.to/hxa/infoleak-prevention-for-authentication-system-118g</link>
      <guid>https://dev.to/hxa/infoleak-prevention-for-authentication-system-118g</guid>
      <description>&lt;p&gt;Authentication systems protect the most valuable part of an application: user identity and access. A leaked password, session cookie, refresh token, API key, or authentication credential can allow an attacker to impersonate a legitimate user.&lt;/p&gt;

&lt;p&gt;A secure authentication system therefore needs to assume that credentials can eventually be exposed. The design goal is to reduce the usefulness of leaked credentials, detect abnormal usage, limit the attacker’s capabilities, and provide mechanisms for rapid revocation.&lt;/p&gt;

&lt;p&gt;This article describes a practical defense architecture for preventing and containing authentication-related information leaks.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Use Secure, HttpOnly Cookies
&lt;/h2&gt;

&lt;p&gt;For browser-based applications, authentication sessions should use secure cookies.&lt;/p&gt;

&lt;p&gt;A recommended cookie configuration is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Set-Cookie: __Host-session=&amp;lt;random-session-id&amp;gt;;
  Path=/;
  Secure;
  HttpOnly;
  SameSite=Lax
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;HttpOnly&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;HttpOnly&lt;/code&gt; prevents JavaScript from directly reading the cookie.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookie&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The authentication session remains inaccessible to JavaScript, which significantly reduces the impact of many XSS-based credential theft scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;Secure&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Secure&lt;/code&gt; ensures the cookie is transmitted only over HTTPS.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;SameSite&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;SameSite=Lax&lt;/code&gt; or &lt;code&gt;SameSite=Strict&lt;/code&gt; provides protection against many cross-site request forgery scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;__Host-&lt;/code&gt; prefix
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;__Host-&lt;/code&gt; cookie prefix provides additional restrictions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Secure&lt;/code&gt; is required.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Path=/&lt;/code&gt; is required.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;Domain&lt;/code&gt; attribute cannot be specified.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;__Host-session=&amp;lt;random-value&amp;gt;; Secure; HttpOnly; SameSite=Lax; Path=/
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a strong browser-side boundary around the authentication session.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Avoid Storing Authentication Tokens in &lt;code&gt;localStorage&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Browser applications should avoid storing long-lived authentication credentials in JavaScript-accessible storage.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;access_token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any successful XSS payload running in the application's origin may be able to read the token:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;access_token&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A safer browser architecture uses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Browser
   │
   ▼
HttpOnly Cookie
   │
   ▼
Session ID
   │
   ▼
Server-side Session
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser stores only the session identifier, while the actual authentication state remains server-side.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Generate High-Entropy Session IDs
&lt;/h2&gt;

&lt;p&gt;Session identifiers must be generated using a cryptographically secure random number generator.&lt;/p&gt;

&lt;p&gt;A practical baseline is a 256-bit random value.&lt;/p&gt;

&lt;p&gt;Node.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node:crypto&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sessionId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;base64url&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Web Crypto:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getRandomValues&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Avoid predictable identifiers based on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User ID
Timestamp
Incrementing counters
Email address
IP address
Weak hashes
Deterministic UUIDs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A session ID should be computationally infeasible to guess.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Rotate Sessions After Authentication
&lt;/h2&gt;

&lt;p&gt;Session rotation protects against session fixation and reduces the lifetime of previously issued credentials.&lt;/p&gt;

&lt;p&gt;Before authentication:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Session A
anonymous
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After successful login:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Session A → revoked
Session B → authenticated
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser receives a new session cookie.&lt;/p&gt;

&lt;p&gt;Session rotation should also happen after important authentication state changes, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Login&lt;/li&gt;
&lt;li&gt;Privilege escalation&lt;/li&gt;
&lt;li&gt;MFA enrollment&lt;/li&gt;
&lt;li&gt;Password changes&lt;/li&gt;
&lt;li&gt;Passkey enrollment&lt;/li&gt;
&lt;li&gt;Recovery flows&lt;/li&gt;
&lt;li&gt;Suspicious authentication events&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Anonymous Session
       │
       ▼
Authentication
       │
       ▼
Revoke old session
       │
       ▼
Create authenticated session
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  5. Use Short-Lived and Bounded Sessions
&lt;/h2&gt;

&lt;p&gt;Sessions should have explicit lifetime limits.&lt;/p&gt;

&lt;p&gt;A practical session policy can include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Absolute Lifetime: 30 days
Idle Timeout:       7 days
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The absolute lifetime defines the maximum age of the session.&lt;/p&gt;

&lt;p&gt;The idle timeout defines how long a session can remain unused.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Login
  │
  ├── active usage → session remains valid
  │
  └── 7 days idle → session expires

30 days after creation → session expires regardless of activity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Shorter lifetimes can be used for highly sensitive applications.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. Support Server-Side Session Revocation
&lt;/h2&gt;

&lt;p&gt;Authentication systems should retain the ability to revoke individual sessions.&lt;/p&gt;

&lt;p&gt;Example session record:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sessions
--------------------------------
id
user_id
created_at
expires_at
last_seen_at
revoked_at
ip
user_agent
device_id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a user logs out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;sessions&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;revoked_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NOW&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Deleting the browser cookie provides the client-side logout behavior.&lt;/p&gt;

&lt;p&gt;Revoking the session on the server provides the security control.&lt;/p&gt;

&lt;p&gt;This is critical when dealing with stolen credentials.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Stolen Cookie
     │
     ▼
Attacker Request
     │
     ▼
Session Lookup
     │
     ▼
revoked_at != NULL
     │
     ▼
401 Unauthorized
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  7. Validate Session State Before Granting Access
&lt;/h2&gt;

&lt;p&gt;Every authenticated request should pass basic session validation.&lt;/p&gt;

&lt;p&gt;At minimum:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Cookie exists
      ↓
Session exists
      ↓
Session belongs to user
      ↓
Session is active
      ↓
Session is not expired
      ↓
Session is not revoked
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A session can maintain explicit lifecycle states:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;active
reauth_required
suspended
revoked
expired
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Session
   │
   ├── active
   │
   ├── reauth_required
   │
   ├── suspended
   │
   ├── revoked
   │
   └── expired
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives the authentication layer more control than a simple valid/invalid flag.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. Store Session Context
&lt;/h2&gt;

&lt;p&gt;A session should contain enough context to identify unusual authentication behavior.&lt;/p&gt;

&lt;p&gt;Useful metadata includes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;session_id
user_id
created_at
expires_at
last_seen_at
ip_address
asn
country
user_agent
device_id
authentication_method
mfa_verified
revoked_at
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These fields support security analysis while keeping the actual credential separate from the browser.&lt;/p&gt;

&lt;p&gt;The IP address and similar metadata should primarily function as risk signals.&lt;/p&gt;




&lt;h2&gt;
  
  
  9. Use Risk-Based Session Detection
&lt;/h2&gt;

&lt;p&gt;A stolen cookie may initially look identical to a legitimate session.&lt;/p&gt;

&lt;p&gt;The server therefore needs behavioral and contextual analysis.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Known Session
   │
   ├── Same device
   ├── Same browser
   ├── Same ASN
   └── Same region
          │
          ▼
       Low Risk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another request may look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Known Session
   │
   ├── New device
   ├── New ASN
   ├── New country
   └── Unusual behavior
          │
          ▼
       High Risk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The session can remain valid while its risk level changes.&lt;/p&gt;




&lt;h2&gt;
  
  
  10. Monitor IP Changes
&lt;/h2&gt;

&lt;p&gt;IP changes are common in real-world environments because users move between Wi-Fi, cellular networks, VPNs, and corporate networks.&lt;/p&gt;

&lt;p&gt;For this reason, IP changes should usually increase risk rather than immediately invalidate the session.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Previous IP:
203.x.x.x

Current IP:
103.x.x.x

Risk:
+10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A major geographic change can increase the score further.&lt;/p&gt;




&lt;h2&gt;
  
  
  11. Monitor Autonomous System Changes
&lt;/h2&gt;

&lt;p&gt;ASN information provides another useful signal.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Original:
Residential ISP

Current:
Cloud VPS Provider
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A transition from a normal residential network to a hosting provider can indicate automation, proxying, or stolen-session usage.&lt;/p&gt;

&lt;p&gt;Possible signals:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Residential ISP → Residential ISP
Low risk

Residential ISP → Corporate ISP
Medium risk

Residential ISP → Cloud Provider
Higher risk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cloud providers and VPN networks can also be legitimate, so ASN should remain a risk signal rather than a standalone blocking rule.&lt;/p&gt;




&lt;h2&gt;
  
  
  12. Monitor Geographic Changes
&lt;/h2&gt;

&lt;p&gt;Country changes can provide useful context.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;17:00 — Jakarta, Indonesia
17:05 — Singapore
17:10 — New York, United States
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Large location changes over very short intervals can indicate session theft.&lt;/p&gt;

&lt;p&gt;This becomes especially useful when combined with other signals:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Country change
+
New device
+
New ASN
+
Sensitive action
=
High risk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  13. Detect Impossible Travel
&lt;/h2&gt;

&lt;p&gt;Authentication systems can estimate whether a user could realistically travel between observed locations.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;08:00
Jakarta

08:10
London
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A transition requiring an impossible travel speed is a strong anomaly signal.&lt;/p&gt;

&lt;p&gt;Possible response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Impossible Travel
      │
      ▼
Increase Risk Score
      │
      ▼
Require Re-authentication
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  14. Track Device Identity
&lt;/h2&gt;

&lt;p&gt;Sessions can be associated with a logical device identifier.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Device A
   ├── Session 1
   └── Session 2

Device B
   └── Session 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If Session 1 suddenly appears from an unfamiliar device:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Session 1
   │
   ▼
Device mismatch
   │
   ▼
Risk increases
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Device binding can be useful for sensitive applications.&lt;/p&gt;

&lt;p&gt;For general SaaS products, device identity works best as a risk signal because users legitimately switch devices and browsers.&lt;/p&gt;




&lt;h2&gt;
  
  
  15. Monitor User-Agent Changes
&lt;/h2&gt;

&lt;p&gt;A large browser change can be suspicious.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Chrome / Windows
       ↓
curl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Chrome / Windows
       ↓
Firefox / Linux
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The signal becomes stronger when combined with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;New device
New ASN
New country
High request frequency
Sensitive endpoint access
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;User-Agent alone should rarely determine session validity.&lt;/p&gt;




&lt;h2&gt;
  
  
  16. Separate Authentication from Authorization
&lt;/h2&gt;

&lt;p&gt;A valid session establishes identity.&lt;/p&gt;

&lt;p&gt;Authorization determines whether that identity may perform a particular operation.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Authentication
     │
     ▼
Who is this user?
     │
     ▼
Authorization
     │
     ▼
What may this user do?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A user session may allow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Profile: Read
Profile: Write
Billing: Read
Admin: Deny
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A stolen cookie therefore remains subject to the application's authorization rules.&lt;/p&gt;




&lt;h2&gt;
  
  
  17. Require Step-Up Authentication for Sensitive Actions
&lt;/h2&gt;

&lt;p&gt;A valid session should provide convenient access to ordinary operations.&lt;/p&gt;

&lt;p&gt;Sensitive operations should require stronger authentication assurance.&lt;/p&gt;

&lt;p&gt;Examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Change password
Change email
Disable MFA
Create API key
Delete account
Modify billing
Add administrator
Withdraw funds
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The system can request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Password
MFA
Passkey
Security key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Valid Session
      │
      ▼
Sensitive Operation
      │
      ▼
Step-Up Authentication
      │
      ├── Success → Continue
      │
      └── Failure → Reject
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This dramatically limits the usefulness of a stolen session cookie.&lt;/p&gt;




&lt;h2&gt;
  
  
  18. Use Refresh Token Rotation
&lt;/h2&gt;

&lt;p&gt;For applications using access tokens and refresh tokens, refresh tokens should rotate after successful use.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Refresh Token A
      │
      ▼
Refresh
      │
      ▼
Refresh Token B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Token A becomes invalid after the rotation.&lt;/p&gt;

&lt;p&gt;If Token A is later reused:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Refresh Token A
      │
      ▼
Reuse detected
      │
      ▼
Revoke token family
      │
      ▼
Require authentication
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This provides an important stolen-token detection mechanism.&lt;/p&gt;




&lt;h2&gt;
  
  
  19. Protect Against CSRF
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;HttpOnly&lt;/code&gt; protects cookies from direct JavaScript access.&lt;/p&gt;

&lt;p&gt;CSRF requires a separate defense strategy.&lt;/p&gt;

&lt;p&gt;Useful controls include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SameSite cookies
+
CSRF tokens
+
Origin validation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For sensitive state-changing requests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;POST /api/password/change
X-CSRF-Token: &amp;lt;token&amp;gt;
Origin: https://app.example.com
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server validates both the request origin and the CSRF token according to the application's architecture.&lt;/p&gt;




&lt;h2&gt;
  
  
  20. Validate the &lt;code&gt;Origin&lt;/code&gt; Header
&lt;/h2&gt;

&lt;p&gt;Sensitive endpoints can validate that the request originated from the expected application.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;Origin: https://app.example.com
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://app.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unexpected origins can be rejected or escalated to additional security checks.&lt;/p&gt;

&lt;p&gt;This is particularly useful for:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Account changes
Payment actions
Administrative actions
Credential management
Security settings
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  21. Add Rate Limiting
&lt;/h2&gt;

&lt;p&gt;A stolen session can be abused at high speed.&lt;/p&gt;

&lt;p&gt;Rate limiting should consider several dimensions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user_id
session_id
IP
ASN
device_id
endpoint
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Normal traffic
10 requests/sec
        │
        ▼
     Allow

Extreme traffic
10,000 requests/sec
        │
        ▼
    Rate limit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sensitive endpoints should have stricter limits.&lt;/p&gt;




&lt;h2&gt;
  
  
  22. Build a Risk Scoring Engine
&lt;/h2&gt;

&lt;p&gt;Security conditions become easier to manage when centralized inside a risk engine.&lt;/p&gt;

&lt;p&gt;Example scoring model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;New IP                 +10
New ASN                +15
New country            +20
New device             +25
Impossible travel      +30
High-risk network      +40
Suspicious behavior    +50
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example decision policy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0–29    → Allow
30–59   → Step-up authentication
60–79   → Restrict sensitive operations
80+     → Revoke session
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact values should be calibrated using application telemetry and false-positive rates.&lt;/p&gt;

&lt;p&gt;A centralized risk engine also makes policy easier to evolve.&lt;/p&gt;




&lt;h2&gt;
  
  
  23. Create Security Events
&lt;/h2&gt;

&lt;p&gt;Security events should be recorded for important anomalies.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"event"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"session_anomaly"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"user_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"usr_123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"session_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ses_456"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"risk_score"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;87&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"reasons"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"new_device"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"new_country"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"asn_change"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"ip"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"x.x.x.x"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"country"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"US"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Useful event types include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;login_success
login_failure
session_created
session_rotated
session_revoked
session_anomaly
mfa_challenge
mfa_success
mfa_failure
refresh_token_reuse
password_changed
security_setting_changed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These events support detection, investigation, auditing, and automated response.&lt;/p&gt;




&lt;h2&gt;
  
  
  24. Design for Credential Theft
&lt;/h2&gt;

&lt;p&gt;A robust authentication system assumes credentials can be exposed.&lt;/p&gt;

&lt;p&gt;The security model should therefore look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Credential Theft
                       │
                       ▼
                Attacker Request
                       │
                       ▼
               Session Validation
                       │
                       ▼
                 Risk Analysis
                       │
          ┌────────────┼────────────┐
          ▼            ▼            ▼
        Low         Medium         High
          │            │             │
        Allow       Step-Up       Revoke
                     MFA          Session
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The stolen credential becomes only one input into the decision process.&lt;/p&gt;




&lt;h2&gt;
  
  
  25. Recommended Authentication Architecture
&lt;/h2&gt;

&lt;p&gt;For a modern SaaS application, a strong architecture can look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                         Browser
                            │
                            │ HTTPS
                            ▼
                  ┌────────────────────┐
                  │    HttpOnly Cookie │
                  │    __Host-session  │
                  └─────────┬──────────┘
                            │
                            ▼
                  ┌────────────────────┐
                  │    Authentication  │
                  │       Layer        │
                  └─────────┬──────────┘
                            │
             ┌──────────────┼──────────────┐
             ▼              ▼              ▼
       Session Store   Risk Engine    Authorization
             │              │              │
             ▼              ▼              ▼
        PostgreSQL        Redis        RBAC / ABAC
             │              │              │
             └──────────────┼──────────────┘
                            ▼
                       Application
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A practical stack can be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Browser
  │
  ▼
HttpOnly session cookie
  │
  ▼
Authentication API
  │
  ├── Session validation
  ├── CSRF / Origin validation
  ├── Risk evaluation
  ├── Authorization
  ├── Rate limiting
  └── Security event logging
  │
  ├── PostgreSQL
  │      └── Users + Sessions
  │
  └── Redis
         ├── Session cache
         ├── Rate limits
         └── Risk signals
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Authentication Security Checklist
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Cookie Security
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;[x] &lt;code&gt;HttpOnly&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;[x] &lt;code&gt;Secure&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;[x] &lt;code&gt;SameSite=Lax&lt;/code&gt; or &lt;code&gt;Strict&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;[x] &lt;code&gt;__Host-&lt;/code&gt; prefix where applicable&lt;/li&gt;
&lt;li&gt;[x] No sensitive data inside the cookie&lt;/li&gt;
&lt;li&gt;[x] Strong random session identifier&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Session Security
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;[x] Server-side session storage&lt;/li&gt;
&lt;li&gt;[x] Session rotation after login&lt;/li&gt;
&lt;li&gt;[x] Session expiration&lt;/li&gt;
&lt;li&gt;[x] Idle timeout&lt;/li&gt;
&lt;li&gt;[x] Server-side revocation&lt;/li&gt;
&lt;li&gt;[x] Explicit session states&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Leak Detection
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;[x] IP monitoring&lt;/li&gt;
&lt;li&gt;[x] ASN monitoring&lt;/li&gt;
&lt;li&gt;[x] Country monitoring&lt;/li&gt;
&lt;li&gt;[x] Impossible-travel detection&lt;/li&gt;
&lt;li&gt;[x] Device tracking&lt;/li&gt;
&lt;li&gt;[x] User-Agent analysis&lt;/li&gt;
&lt;li&gt;[x] Behavioral anomaly detection&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Account Protection
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;[x] RBAC / authorization&lt;/li&gt;
&lt;li&gt;[x] Step-up authentication&lt;/li&gt;
&lt;li&gt;[x] MFA / passkeys&lt;/li&gt;
&lt;li&gt;[x] CSRF protection&lt;/li&gt;
&lt;li&gt;[x] Origin validation&lt;/li&gt;
&lt;li&gt;[x] Rate limiting&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Token Protection
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;[x] Short-lived access tokens&lt;/li&gt;
&lt;li&gt;[x] Refresh token rotation&lt;/li&gt;
&lt;li&gt;[x] Refresh token reuse detection&lt;/li&gt;
&lt;li&gt;[x] Token-family revocation&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Monitoring
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;[x] Authentication events&lt;/li&gt;
&lt;li&gt;[x] Session anomaly events&lt;/li&gt;
&lt;li&gt;[x] Security audit logs&lt;/li&gt;
&lt;li&gt;[x] Automated response&lt;/li&gt;
&lt;li&gt;[x] Session revocation controls&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Design Principle
&lt;/h2&gt;

&lt;p&gt;A secure authentication system should operate under this assumption:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Credentials may leak.
Sessions may be copied.
Devices may change.
Networks may change.
Attackers may replay valid credentials.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The authentication system should continuously answer three questions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Is this session valid?

2. Does this request look trustworthy?

3. Does this operation require stronger authentication?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resulting security model becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Credential
    │
    ▼
Session Validation
    │
    ▼
Risk Assessment
    │
    ▼
Authorization
    │
    ▼
Action Assurance
    │
    ├── Normal action
    │       └── Allow
    │
    └── Sensitive action
            └── MFA / Passkey
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A secure authentication system is built around the assumption that credentials can eventually be exposed. The strongest defense combines secure cookie handling, server-side session management, continuous risk assessment, strict authorization, step-up authentication, token rotation, rate limiting, and security event monitoring to reduce the value of stolen credentials and contain unauthorized access.&lt;/p&gt;

&lt;p&gt;By treating authentication as a continuous security process rather than a single login event, applications can detect suspicious sessions earlier, respond automatically, and protect sensitive operations even when an attacker possesses a valid credential.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbaqw46ws8gao04ggydhg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbaqw46ws8gao04ggydhg.png" alt="Infoleak Prevention for Authentication System Infographic" width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>cybersecurity</category>
      <category>security</category>
    </item>
  </channel>
</rss>
