Skip to content

Bound the per-issuer monitoring map against unbounded growth - #215

Open
djw8605 wants to merge 2 commits into
scitokens:masterfrom
djw8605:fix/bound-issuer-stats
Open

Bound the per-issuer monitoring map against unbounded growth#215
djw8605 wants to merge 2 commits into
scitokens:masterfrom
djw8605:fix/bound-issuer-stats

Conversation

@djw8605

@djw8605 djw8605 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Problem

MonitoringStats::get_issuer_stats() inserts a permanent ~200-byte entry keyed by the iss claim of any syntactically valid token — before the signature or issuer is validated (verify_async records async_validations_started, and get_public_key_pem records expired_keys for unknown issuers). An attacker replaying unsigned tokens with random issuer strings grows the map without bound.

The existing DDoS protection (MAX_FAILED_ISSUERS = 100) only covers the separate failed-lookup map — and its recording path (record_failed_issuer_lookup) is currently never called from anywhere, so it protects nothing.

Fix

  • Cap tracked issuers at MAX_TRACKED_ISSUERS (1000). Once at capacity, statistics for further issuers aggregate into a single shared overflow bucket, reported as "<other>" in the monitoring JSON.
  • Entries are still never erased, preserving the reference stability that in-flight validations depend on (see Fix use-after-free race between monitoring reset() and in-flight validations #214).
  • The per-issuer JSON serialization moves into a serialize_issuer_stats() helper so the overflow bucket reuses it; no change to the JSON schema for existing issuers.

Note: this branch is stacked on #214 (contains its commit) since both change MonitoringStats internals — merging #214 first makes this a clean one-commit diff.

Testing

  • ctest unit, env_config, and monitoring suites pass (the monitoring suite exercises stats collection, JSON output, and reset).

🤖 Generated with Claude Code

djw8605 and others added 2 commits July 6, 2026 13:02
MonitoringStats::get_issuer_stats() documents that returned references
remain valid for the lifetime of the singleton, and Validator::verify()
holds an IssuerStats pointer across the entire validation loop.  But
MonitoringStats::reset() called m_issuer_stats.clear(), destroying the
entries.  Any thread calling scitoken_reset_monitoring_stats() while
another thread had a verification in flight left that thread updating
freed memory.

Zero the counters in place instead of erasing the entries, and skip
all-zero entries when serializing to JSON so reset still produces an
empty report.  The failed-issuer map holds plain values only accessed
under the mutex, so clearing it remains safe.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
MonitoringStats::get_issuer_stats() inserts a permanent entry keyed by
the 'iss' claim of any syntactically valid token -- before the
signature or issuer is validated (verify_async and get_public_key_pem
both call it for unknown issuers).  An attacker replaying unsigned
tokens with random issuer strings could grow the map without bound.
The existing MAX_FAILED_ISSUERS protection only covers the separate
failed-lookup map (whose recording path is currently never called).

Cap the tracked issuers at MAX_TRACKED_ISSUERS (1000); further issuers
share a single aggregate overflow bucket reported as '<other>' in the
JSON output.  Entries are still never erased, preserving the reference
stability that in-flight validations depend on.

Also factors the per-issuer JSON serialization into a helper so the
overflow bucket reuses it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@djw8605 djw8605 added the ai-gen label Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant