Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ Here are the possible session configuration options:
| `revocation` | `nil` | Storage used for cookie session revocation records. Use `nil` or `false` to disable, a storage name such as `"shm"`, `"redis"`, `"mysql"`, or `"postgres"`, a custom storage module name, or a storage `table` with `set`/`get` methods. |
| `revocation_fail_mode` | `"open"` | Behavior when the revocation store is unreachable, use `"open"` (treat as not revoked) or `"closed"` (reject the session). |
| `revocation_cache_ttl` | `5` | Worker-local cache TTL for successful revocation lookups (`true`/`false`) (in seconds). Set to `0` to disable. This is the maximum stale window before a worker observes a revocation written elsewhere. |
| `revocation_error_cache_ttl` | `5` | Worker-local cache TTL for revocation store errors (in seconds). Set to `0` to disable. `"open"` caches not-revoked; `"closed"` caches the error. |
| `revocation_error_cache_ttl` | `5` | Worker-local cache TTL for revocation store errors (in seconds). Set to `0` to disable. `"open"` caches not-revoked; `"closed"` caches the error; each is kept apart. |
| `dshm` | `nil` | Configuration for dshm storage, e.g. `{ prefix = "sessions" }` (see below) |
| `file` | `nil` | Configuration for file storage, e.g. `{ path = "/tmp", suffix = "session" }` (see below) |
| `memcached` | `nil` | Configuration for memcached storage, e.g. `{ prefix = "sessions" }` (see below) |
Expand Down Expand Up @@ -370,6 +370,18 @@ seconds. Store unavailability is cached for `revocation_error_cache_ttl`
seconds: `"open"` caches not-revoked, and `"closed"` caches the error, so a
burst of requests does not retry the store on every lookup.

Lookups and store errors are cached separately in each worker, up to 1024
entries each. Change the sizes with the nginx variables
`$session_revocation_cache_size` and `$session_revocation_error_cache_size`, for
example with a `map` in the `http` block. Each worker reads them once, so a change
needs a reload; invalid values are ignored with a warning.

```nginx
map $host $session_revocation_cache_size {
default 4096;
}
```

`session:destroy` (including a last-audience `session:logout`) runs the same
cached lookup first. A cached `true` means the identifier is already revoked
and no store write is made, and a cached error is returned without contacting
Expand All @@ -378,7 +390,38 @@ TTL equal to the remaining session lifetime (rolling and absolute timeouts),
and the local cache is updated immediately. When the write fails, the failure
is cached for the same identifier under `revocation_error_cache_ttl`, so the
next lookup sees it instead of the store. The revocation mark is a lightweight
sentinel; no session payload is stored.
sentinel; no session payload is stored. Lineage marks are written without a
lookup.

Cache entries are kept per revocation storage, identified by its settings:
equal settings share entries, different settings never do. Functions and other
values JSON cannot encode are left out, so a custom storage table needs a plain
field, such as a name, that tells it apart. The settings are encoded with cjson,
which needs `encode_sort_keys` and `encode_skip_unsupported_value_types`
(OpenResty 1.31.8.1 or later); without them, configuring revocation raises an
error.

A revocation updates the local worker cache at once; other workers see it when
their cached lookup expires. Set `revocation_cache_ttl` and
`revocation_error_cache_ttl` to `0` to check the store on every request. The
marks live only in the revocation storage: if it loses its data, revoked cookies
are accepted again until they expire.

Every save gives a cookie session a new session id, so revoking only the
destroyed id would leave the other cookies of the same login usable, such as one
an attacker refreshed before the logout. With revocation enabled, a random
lineage id is created on the first save and kept, encrypted, through every
rotation. `session:destroy` revokes it along with the session id, so every cookie
of that login is rejected; a new login starts a new lineage. The lineage mark is
kept for a full rolling window plus `stale_ttl`, capped by the absolute timeout
(the `remember_*` windows for a remember cookie). A remember cookie, or a cookie
opened for an audience it does not have, is checked against the lineages of all
its audiences. Cookies issued before lineage ids existed are revoked by their
own session id only.

Lineage marks cost one more lookup on a cache miss and one more write per
audience on destroy. They are written before the session id mark, with the
audience in use last, so a failed logout can be retried.

Use `revocation_fail_mode` to control behavior when the storage is unavailable:

Expand Down Expand Up @@ -999,6 +1042,12 @@ end
Destroy the session and clear the cookies. In error case it returns `nil`
and an error message, otherwise `true`.

With revocation enabled it also revokes the session's lineage, so every cookie
of the same login is rejected (see [Session Revocation Configuration](#session-revocation-configuration)).
No cookie is cleared until every revocation mark is written, so with
`revocation_fail_mode = "closed"` a store failure leaves the cookies in place and
`session:destroy` can be called again.

```lua
local session, err, exists = require "resty.session".open()
if exists then
Expand Down
2 changes: 1 addition & 1 deletion dev/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openresty/openresty:1.27.1.2-0-noble
FROM openresty/openresty:1.31.9.1-0-noble

ENV DEBIAN_FRONTEND noninteractive
ENV TEST_NGINX_BINARY openresty
Expand Down
Loading
Loading