Skip to content

Commit 9e37848

Browse files
authored
Merge pull request #523 from boscard/master
Enable caching of negative introspection responses
2 parents 4702769 + eb68d90 commit 9e37848

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,19 @@ from the cache. In order to avoid cache confusion it is recommended to
384384
set `opts.cache_segment` to unique strings for each set of related
385385
locations.
386386

387+
## Caching of negative Introspection responses
388+
389+
By default `introspection` cache will not store negative responses.
390+
This means that bad actor can potentialy try to exhaust introspection
391+
endpoint by flooding service with a lot of calls with inproper token.
392+
To prevent this situation `opts.introspection_enable_negative_cache`
393+
can be set to `true`. This will enable `introspection` cache to store
394+
negative responses for time defined in `exp` field.
395+
Caching negative introspection responses will offload traffic from
396+
introspection endpoint but also will expose NGINX for resource exhaustion
397+
attacks as storing negative introspection responses will use extra
398+
cache storage.
399+
387400
## Revoke tokens
388401

389402
The `revoke_tokens(opts, session)` function revokes the current refresh and access token. In contrast to a full logout, the session cookie will not be destroyed and the endsession endpoint will not be called. The function returns `true` if both tokens were revoked successfully. This function might be helpful in scenarios where you want to destroy/remove a session from the server side.

lib/resty/openidc.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,11 @@ function openidc.introspect(opts)
17801780

17811781
if v then
17821782
json = cjson.decode(v)
1783+
1784+
if not json or not json.active then
1785+
err = "invalid cached token"
1786+
end
1787+
17831788
return json, err
17841789
end
17851790

@@ -1810,12 +1815,13 @@ function openidc.introspect(opts)
18101815
end
18111816
json, err = openidc.call_token_endpoint(opts, introspection_endpoint, body, opts.introspection_endpoint_auth_method, "introspection")
18121817

1813-
18141818
if not json then
18151819
return json, err
18161820
end
18171821

1818-
if not json.active then
1822+
-- check if negative cache should be in use
1823+
local introspection_enable_negative_cache = opts.introspection_enable_negative_cache or false
1824+
if not json.active and not introspection_enable_negative_cache then
18191825
err = "invalid token"
18201826
return json, err
18211827
end
@@ -1824,6 +1830,7 @@ function openidc.introspect(opts)
18241830
local introspection_cache_ignore = opts.introspection_cache_ignore or false
18251831
local expiry_claim = opts.introspection_expiry_claim or "exp"
18261832

1833+
18271834
if not introspection_cache_ignore and json[expiry_claim] then
18281835
local introspection_interval = opts.introspection_interval or 0
18291836
local ttl = json[expiry_claim]
@@ -1839,6 +1846,10 @@ function openidc.introspect(opts)
18391846
set_cached_introspection(opts, access_token, cjson.encode(json), ttl)
18401847
end
18411848

1849+
if not json.active then
1850+
err = "invalid token"
1851+
end
1852+
18421853
return json, err
18431854

18441855
end

0 commit comments

Comments
 (0)