Class: Parse::Agent::MCPRackApp::SessionOwnerRegistry Private

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/agent/mcp_rack_app.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Per-app store of in-flight cancellable requests. Lookups for cancellation are keyed by [correlation_id, request_id], but every #register returns an opaque entry-id token that uniquely identifies the registration. #deregister requires that entry-id and removes the matching token only when it still owns the slot — so a second registration under the same (correlation_id, request_id) key cannot cause the first registration's on_close to evict the wrong token.

SSEBody registers an entry before spawning its dispatcher_thread and deregisters via the MCPRackApp-supplied on_close hook. A notifications/cancelled POST calls #cancel to trip the matching CancellationToken.

Identity binding: cancellation requires the cancelling request's Mcp-Session-Id (sanitized into agent.correlation_id) to match the original request's. This prevents an attacker who guesses sequential JSON-RPC request ids from cancelling other clients' in-flight requests. A registration with a nil correlation_id is dropped silently (cancellation is disabled for the request).

Scope: per MCPRackApp instance. Cancellation does NOT span multiple mount points within a process, nor multiple processes in a clustered deployment.

Binds an MCP session id to the principal that established it, so a listening stream (the server→client notification channel) can only be attached by the same principal — closing the cross-session hijack where any authenticated caller who knows/guesses another session's id could subscribe to its notifications or evict its listener via overwrite.

Trust model and limitations (mirrored in the docs):

  • Initialize-bound vs TOFU. A session established through an initialize POST is bound to that caller's principal authoritatively. A session id that was never seen by initialize (the decoupled notifications: bus, where app code pushes to arbitrary ids) is claimed trust-on-first-use by whoever attaches a listener first; subsequent attaches by a different principal are refused. TOFU is strictly better than the prior bearer model (eviction-after-claim is closed) but a first-mover attacker can still claim an unused id — so notification-bus ids should be high-entropy.
  • Per-instance / single-process, exactly like CancellationRegistry: it does not span Puma workers or survive restart. In a cluster the GET stream and the initialize POST may land on different workers, so the initialize-binding degrades to TOFU there.
  • Principal fidelity depends on the factory. The fingerprint is derived from the agent the factory builds (session_token → acl_user → acl_role), or an operator-supplied principal_resolver. A master-key-everywhere factory yields one shared "mk" principal, so owner-binding is a no-op unless a principal_resolver (or per-user impersonation) supplies a real identity.

LRU-bounded so an initialize-without-DELETE stream of sessions can't grow it without limit. Live sessions (an attached listening stream or a pending approval) are pinned and never evicted; an evicted idle id downgrades to TOFU on its next attach. Each principal may hold at most max_per_principal bindings: past that, its own least recently used idle binding is evicted, so one caller flooding initialize cannot push other principals' idle sessions out of the registry.

Constant Summary collapse

DEFAULT_MAX_ENTRIES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

10_000
DEFAULT_MAX_PER_PRINCIPAL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

100
SHARED_PRINCIPALS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Fingerprints shared by every caller of an endpoint (a master-key factory with no principal_resolver). A per-principal bound on them would be a bound on the whole endpoint, so only the global one applies.

%w[mk].freeze

Instance Method Summary collapse

Constructor Details

#initialize(max_entries: DEFAULT_MAX_ENTRIES, pinned: nil, max_per_principal: DEFAULT_MAX_PER_PRINCIPAL) ⇒ SessionOwnerRegistry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SessionOwnerRegistry.

Parameters:

  • pinned (#call, nil) (defaults to: nil) —

    ->(session_id) { Boolean }; a pinned session's binding is never evicted (see #evict_lru!).

  • max_per_principal (Integer, nil) (defaults to: DEFAULT_MAX_PER_PRINCIPAL) —

    bindings one principal may hold; nil disables the per-principal bound.



2243
2244
2245
2246
2247
2248
2249
2250
2251
# File 'lib/parse/agent/mcp_rack_app.rb', line 2243

def initialize(max_entries: DEFAULT_MAX_ENTRIES, pinned: nil,
               max_per_principal: DEFAULT_MAX_PER_PRINCIPAL)
  @owners = {} # session_id => principal fingerprint (insertion-ordered for LRU)
  @counts = Hash.new(0) # principal fingerprint => bindings held
  @max = max_entries
  @max_per_principal = max_per_principal
  @pinned = pinned
  @mutex = Mutex.new
end

Instance Method Details

#authorize_attach(session_id, fingerprint) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Authorize a listening-stream attach. Returns true when the session is unclaimed (claims it TOFU for this principal) or already owned by this principal (refreshing its LRU position); false on a principal mismatch. Blank inputs fail closed.



2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
# File 'lib/parse/agent/mcp_rack_app.rb', line 2282

def authorize_attach(session_id, fingerprint)
  return false if blank?(session_id) || blank?(fingerprint)
  @mutex.synchronize do
    owner = @owners[session_id]
    if owner.nil?
      store(session_id, fingerprint)
      retain_or_reject!(session_id, fingerprint)
    elsif owner == fingerprint
      @owners.delete(session_id)
      @owners[session_id] = owner
      true
    else
      false
    end
  end
end

#bind(session_id, fingerprint) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Bind a session to a principal at initialize. An unclaimed session is claimed; a re-initialize by the owning principal refreshes the binding. A session already owned by a different principal is NOT rebound, so knowing another caller's session id is not enough to take it over (and with it, its log level, elicitation capability, and listening stream).

Returns:

  • (Boolean) —

    true when bound to fingerprint; false on a principal mismatch or blank input.



2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
# File 'lib/parse/agent/mcp_rack_app.rb', line 2262

def bind(session_id, fingerprint)
  return false if blank?(session_id) || blank?(fingerprint)
  @mutex.synchronize do
    owner = @owners[session_id]
    return false if owner && owner != fingerprint
    if owner
      @owners.delete(session_id)
      @owners[session_id] = fingerprint
      true
    else
      store(session_id, fingerprint)
      retain_or_reject!(session_id, fingerprint)
    end
  end
end

#claimed_by_other?(session_id, fingerprint) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

True when the session is bound to a principal other than this one. An unclaimed session is not claimed by anyone else.

Returns:

  • (Boolean)


2301
2302
2303
2304
2305
2306
2307
2308
# File 'lib/parse/agent/mcp_rack_app.rb', line 2301

def claimed_by_other?(session_id, fingerprint)
  return false if blank?(session_id)
  @mutex.synchronize do
    owner = @owners[session_id]
    touch(session_id, owner) if owner && owner == fingerprint
    !owner.nil? && owner != fingerprint
  end
end

#controllable_by?(session_id, fingerprint) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

True when the session is unbound (never initialized or attached) or bound to this principal. Used to gate per-session control messages (cancellation, elicitation replies): an unbound session has no owner to protect, while a bound one only accepts its owner.

Returns:

  • (Boolean)


2314
2315
2316
2317
2318
2319
2320
# File 'lib/parse/agent/mcp_rack_app.rb', line 2314

def controllable_by?(session_id, fingerprint)
  return false if blank?(session_id) || blank?(fingerprint)
  @mutex.synchronize do
    owner = @owners[session_id]
    owner.nil? || owner == fingerprint
  end
end

#forget(session_id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Drop a session's owner binding (explicit DELETE termination). Not called on mere stream close, so a reconnecting owner keeps its claim and an attacker can't grab the id during a brief disconnect.



2336
2337
2338
2339
# File 'lib/parse/agent/mcp_rack_app.rb', line 2336

def forget(session_id)
  return if blank?(session_id)
  @mutex.synchronize { remove(session_id) }
end

#owned_by?(session_id, fingerprint) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

True when session_id is bound to exactly this principal. Never claims an unbound session (unlike #authorize_attach).

Returns:

  • (Boolean)


2324
2325
2326
2327
2328
2329
2330
2331
# File 'lib/parse/agent/mcp_rack_app.rb', line 2324

def owned_by?(session_id, fingerprint)
  return false if blank?(session_id) || blank?(fingerprint)
  @mutex.synchronize do
    owned = @owners[session_id] == fingerprint
    touch(session_id, fingerprint) if owned
    owned
  end
end

#size ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns current number of bound sessions (tests/metrics).

Returns:

  • (Integer) —

    current number of bound sessions (tests/metrics).



2342
2343
2344
# File 'lib/parse/agent/mcp_rack_app.rb', line 2342

def size
  @mutex.synchronize { @owners.size }
end