Skip to content

feat(*): revoke every cookie of a login on destroy - #217

Open
Hockenba wants to merge 13 commits into
bungle:masterfrom
Hockenba:feat/revoke-session-lineage-on-destroy
Open

Hockenba wants to merge 13 commits into
bungle:masterfrom
Hockenba:feat/revoke-session-lineage-on-destroy

Conversation

@Hockenba

@Hockenba Hockenba commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #209 and #213.

Summary

session:save gives a cookie session a new session ID, but session:destroy only revoked the ID being destroyed. Older cookies of the same login, including one an attacker refreshed before the logout, stayed valid until they timed out.

  • Add a lineage ID: random, created on the first save and carried, encrypted, through every rotation. A new login gets a new one.
  • session:destroy writes a lineage mark, then the session ID mark. Every cookie of the login is rejected, other logins are unaffected, and a logout that fails halfway can be retried.
  • Key the revocation cache by the storage's settings instead of the storage instance.
  • Cache store errors separately and per revocation_fail_mode, so an error cached under "open" is never read under "closed".
  • Size the caches with the nginx variables $session_revocation_cache_size and $session_revocation_error_cache_size (default 1024).

The fingerprint needs a cjson build with encode_sort_keys, which OpenResty has from 1.31.8.1, so the test image now uses 1.31.9.1. On an older OpenResty, using revocation raises an error.

Cost: one extra lookup on a cache miss and one extra write per destroy. Cookies issued before this change have no lineage ID and are still revoked by their own session ID only. Marks live only in the revocation storage.

Consumer: apache/apisix#14242.

Test plan

  • make unit / busted spec/06-revocation-1_spec.lua spec/07-revocation-2_spec.lua
  • Lineage: revoked from any presented cookie, other logins unaffected, mark lifetime, failed writes and retry
  • Cache: hits, expiry, per-fail-mode errors, identity by storage settings
  • End to end with real APISIX, Keycloak and Redis

A cookie session gets a new session ID on every save, but destroy only
revoked the ID being destroyed, so older copies of the cookie and one an
attacker refreshed before the logout stayed valid until they timed out.

- generate a random lineage ID on the first save and carry it, encrypted,
  through every rotation; a new login gets a new lineage
- write a lineage mark on destroy, so every cookie of that login is rejected
  and other logins of the account are unaffected
- key the revocation cache by the storage settings instead of the instance
- keep store errors in a separate cache, per revocation_fail_mode
- size the caches with $session_revocation_cache_size and
  $session_revocation_error_cache_size
@Hockenba
Hockenba force-pushed the feat/revoke-session-lineage-on-destroy branch from aebf8cf to 07c5f3a Compare November 23, 2026 15:26
If the second write of a logout failed in closed mode, a retry found the
session id mark, rejected the cookie on open and never wrote the missing
lineage mark, so a cookie rotated earlier stayed valid. Writing the lineage
mark first means a failure between the two writes still revokes every cookie
of the login, and a logout that fails on the first write can be retried.
…error spec

The lineage mark is written first now, so the spec fails the session id mark,
which is the write whose error is cached.
@moonming

Copy link
Copy Markdown

Thanks for moving the lineage write ahead of the SID write. Could you also make a failed destroy() retryable when a cookie contains more than one audience? revoke_lineages() writes one marker at a time. If the current audience's marker is persisted and the next write fails, a fresh request at the same logout endpoint sees the first marker in open() and returns before destroy() can finish. The other audience's lineage can remain valid even after the store recovers.

Please add a regression test with two audiences and distinct lineages. Have the store fail on the second lineage write, restore it, and retry logout with the original cookie in a separate request. Please assert that the first logout reports the failure, the retry completes, and both audiences are rejected afterward. Using a fresh request matters here because retrying on the same in-memory session skips the failing open() path.

Could you also fix the failing OpenResty CI job? The first error is at utils.lua:887, where the installed cjson has no encode_sort_keys method. Please verify the fingerprinting code against the cjson version used by that job and rerun the suite.

config_fingerprint used the encode_sort_keys and encode_skip_unsupported_value_types
methods of cjson, which not every cjson build has, so the revocation specs failed
on the OpenResty job. Write the configuration in a canonical form with sorted
keys instead, and leave out functions and userdata as before.
A cookie is rejected on open once the lineage mark of its audience is stored. With
several audiences that mark could be stored before a failing write of another
audience, and the retry then stopped at open() with the other lineage still valid.
Write the lineage of the audience in use last.
Use cjson for the fingerprint, as encode_json and decode_json do, instead of a
second serializer. It needs a cjson build with encode_sort_keys and
encode_skip_unsupported_value_types, which the README now says.
The fingerprint of a revocation storage uses the encode_sort_keys option of cjson,
which the cjson of OpenResty has from 1.31.8.1. Build the test image on 1.31.9.1
and say in the README which OpenResty has the option.
…early without the cjson options

Name the helpers like the functions next to them: mark_lineages_revoked like
mark_session_revoked, and get_revocation_cache_size and get_lineage_revocation_key
like the other getters. Raise an error that says what is missing when cjson has no
encode_sort_keys or encode_skip_unsupported_value_types, and only identify the
revocation storage of sessions that use it, so sessions with server-side storage
are not affected.
… opened for another audience, write each mark once

A lineage belongs to one login, so a lineage mark rejects its cookies without
comparing times, as the session id mark does; the comparison let clock skew
between nodes keep the rotated cookies valid. A cookie opened for an audience it
does not have is checked against all its lineages, so saving it for the new
audience cannot carry a logged-out login on. A remember-cookie logout writes the
lineage marks once. A storage named by an alias that load_storage accepts is
identified by the same settings it reads.
…heck in open()

Move the lineage check of open() into is_lineage_revoked, name the vague locals,
shorten the comments and the README revocation text, and keep the README wording
of master where it still applies.
@Hockenba

Copy link
Copy Markdown
Contributor Author

Thanks for moving the lineage write ahead of the SID write. Could you also make a failed destroy() retryable when a cookie contains more than one audience? revoke_lineages() writes one marker at a time. If the current audience's marker is persisted and the next write fails, a fresh request at the same logout endpoint sees the first marker in open() and returns before destroy() can finish. The other audience's lineage can remain valid even after the store recovers.

Please add a regression test with two audiences and distinct lineages. Have the store fail on the second lineage write, restore it, and retry logout with the original cookie in a separate request. Please assert that the first logout reports the failure, the retry completes, and both audiences are rejected afterward. Using a fresh request matters here because retrying on the same in-memory session skips the failing open() path.

Could you also fix the failing OpenResty CI job? The first error is at utils.lua:887, where the installed cjson has no encode_sort_keys method. Please verify the fingerprinting code against the cjson version used by that job and rerun the suite.
Addressed.

Multiple audiences: the lineage mark of the audience in use is now written last, so a failed logout still opens on retry and writes all the marks again. Added the test you described: two audiences with separate lineages, a failure on the second lineage write, then a retry in a new request with the original cookie. The first logout reports the failure, the retry completes, and both audiences are rejected afterward.

CI: the job failed because the cjson in OpenResty 1.27 has no encode_sort_keys. OpenResty's cjson has it from 1.31.8.1, so dev/Dockerfile now uses 1.31.9.1, and the library raises a clear error when the option is missing. The full suite passes in that image (145 tests).

Add get_storage_settings, which gives the name and settings of a storage,
also when it is given by an alias, and use it in load_storage and for the
revocation storage identity, so a new alias cannot be missed in one of them.
@bungle

bungle commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

@Hockenba,

Why do we need lineage? Why cannot we just revoke old session with say revocation in effect after 10 secs (aka future revocation) on save with ttl of the remaining lifetime, or more (+10 by default) (so that revocation stays in effect)? I am pretty sure that is what is happening when using server side storage where we ttl the old session (kinda opposite but similar). (opposite because server side storage key found means there is sessions whereas with revocation table it means that session is not valid anymore). (see stale_ttl config parameter).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants