← All articlesWeb security

How to store a session secret hash and verify that an identifier alone cannot grant access

Separating the identifier from the secret changes what the server stores to verify a user session. The record retains the secret’s SHA-256 hash instead of the original secret, while the public identifier is used for lookup. But changing the record format alone is not enough: the handler for every request must verify the secret. We explain how this design works and how to test it using a fictional account portal and synthetic data.

Where the trust boundary lies

In this design, the cookie contains two values: a public identifier and a secret. The server stores the identifier to locate the record and the full SHA-256 hash of the secret. The identifier tells the server which session to check, but does not establish authentication by itself. Access must not be granted merely because a record with that identifier exists.

For every request, the server looks up the session by its identifier, hashes the supplied secret, and compares the result with the stored hash in constant time. The trust boundary lies between locating the record and verifying the secret: finding a record does not establish that the request belongs to the corresponding user.

Choosing the record format and generating the secret

For the fictional Mayak account portal, we define a server record with fields for the public identifier and the full SHA-256 hash of the verification secret. We do not store the original secret alongside the hash, including within a stored copy of the complete cookie. Doing so would fail the requirement for one-way storage.

Generate the secret independently of the identifier using a cryptographically secure pseudorandom number generator (CSPRNG). It requires at least 128 bits of entropy, preferably 160 bits or more. The public identifier does not count toward this amount. Fast SHA-256 hashing is sufficient for a random secret of this kind; password hashing algorithms are not required here.

Use a consistent representation of the secret when creating and verifying it. The implementation should explicitly define exactly which data is supplied to the hash function and apply the same rule in both places. Check this separately, particularly when session creation and request handling reside in different parts of the application.

Fictional example: accessing a profile and changing a setting

All checks below are original editorial suggestions, not an official procedure. Perform them only in your own test environment with synthetic data. Our fictional Mayak portal has a test user named Anna, a protected profile page, and a signature setting. We create a valid session for her and record the initial signature value.

Run the positive control in a clean client by supplying the correct identifier and secret pair. Success means gaining access specifically to Anna’s profile. Then change the test signature and check the stored state separately: confirm that the new value actually appears for this user. A successful HTTP response alone does not confirm that the change was accepted.

Verifying that an identifier alone is insufficient

For the negative controls, remove any other valid credentials from the client. First, send a request containing only an existing identifier. Then send one with the same identifier and an incorrect secret. In both cases, check the actual outcome: Anna’s protected data must remain inaccessible, and the action requiring authorization must not execute.

Before each attempt to change the signature, establish a known initial state. Afterwards, separately check the stored value through a control session using the correct pair. The signature must remain unchanged. This covers both reading and taking action: a rejection message or response status alone does not confirm that the change was rejected.

What to check in storage and code

After creating a synthetic session, inspect its server record and a test backup. The acceptance criterion is the presence of the public identifier and the full SHA-256 hash, with no original secret stored alongside it. Separately trace the generation code: confirm that it uses a CSPRNG, its parameters provide the required entropy, and the secret is generated independently of the identifier. A lack of duplicates among a few test values does not prove sufficient entropy.

In the request handler, confirm that a constant-time hash comparison function is used. Check the documentation and conditions of use for the selected function. Similar response times across a series of requests do not prove that this requirement is met. The requirement applies to hash comparison and does not imply constant-time processing of the entire request.

Fix priorities and limits of the result

If the identifier already grants access, the first fix must be mandatory secret verification when processing every request. Then update storage to retain the full SHA-256 hash without the original secret, and check independent generation, consistent representation, and constant-time comparison. Repeat the positive and negative controls, including checks of stored state. The login page and all subsequent authenticated pages must be accessible exclusively over TLS or another secure transport.

The conclusion is limited to a specific threat: one-way storage protects against read-only disclosure of session records and backups containing only hashes of the secrets. This design does not protect against cookie theft, modification of session records, or compromise of the application. For an additional review of your implementation, you can use CheckMyApp’s free audit of your website or the web part of your product.

Sources and documentation

  1. Session Management Cheat Sheet
  2. Authentication Cheat Sheet
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free