Skip to main content

ApprovalCache

Struct ApprovalCache 

Source
pub struct ApprovalCache { /* private fields */ }
Expand description

Session-scoped “approve for session” decision cache (§2.10). Keyed by Self::key(tool, subject) — so a repeated identical call (the SAME canonical command, or the same path) skips re-prompting for the rest of this agent’s lifetime, exactly like CC’s “don’t ask again”/oc’s “always” (cc§4, oc§4). Cheap and unconditional to construct — an agent that never enables capabilities.permissions simply never populates or consults it (§1.13-style “zero cost when off”).

Implementations§

Source§

impl ApprovalCache

Source

pub fn new() -> ApprovalCache

A fresh, empty cache.

Source

pub fn key(tool: &str, subject: Option<&str>) -> String

The cache key for a (tool, subject) pair — no hashing, so it stays legible-ish in logs/debug output (see length_prefixed’s doc comment for D-3’s length-prefixed encoding, which keeps this readable while still being provably unambiguous); a session cache has no untrusted-input DoS surface a hash would need to guard against (bounded by how many distinct calls one session can make).

Only use this directly for a request that HAS a subject (bash’s command, a file tool’s resolved path, apply_patch’s patch). For the general case — including a request with NO subject — use Self::key_for_request, which falls back to this exact function when subject is Some (so every existing bash/file-tool caller is unaffected) but does something different when it’s None — see that method’s doc comment for why (F2, Fable-5 adversarial review).

Source

pub fn key_for_request(req: &ApprovalRequest<'_>) -> String

F2 (Fable-5 adversarial review — HIGH, “‘allow for session’ over-grants tool-wide for subject-less tools”): the cache key resolve_ask actually uses, for ANY request shape.

ApprovalRequest::subject is command.or(path).or(patch) (Agent::permissions_gate_denial) — None for every MCP tool call and any tool whose interesting content lives in richer JSON args rather than a single command/path/patch string (e.g. mcp_db_query {"sql": "…"}). Before this fix, Self::key alone collapsed a subject-less request down to the bare tool name, so an AllowForSession granted for ONE call’s args ({"sql":"SELECT 1"}) silently auto-allowed EVERY later call to that tool regardless of args ({"sql":"DROP TABLE users"}) — an over-grant the user never saw, let alone approved.

The fix: when there’s no subject, fold a canonical digest of raw_args into the key too, so a session grant only ever auto-allows the exact SAME args again — a call with different args still reaches the handler. “Canonical” here means canonical_json_string’s recursively-key-sorted rendering, NOT Value’s own Display/to_string() — this crate’s own build happens to render Value’s keys already-sorted (serde_json’s default Map backing is a BTreeMap unless some dependency’s build pulls in the preserve_order feature and Cargo’s feature resolver unifies it into this target too), but a SECURITY-relevant cache key has no business depending on an indirect, easily- disturbed fact like that — so this re-sorts explicitly and is correct regardless.

When subject IS Some (bash/file tools/apply_patch), this is byte-identical to Self::key — those callers’ session-grant breadth is completely unchanged.

D-3 (Fable-5 delta review — LOW hardening): the None branch used to join tool and the args digest with a bare \u{0}args: separator that wasn’t itself length-guarded — see length_prefixed’s doc comment for the exact collision the review proved constructible against Self::key’s Some branch, and why length-prefixing every component (rather than trusting an unlengthed separator no caller-controlled byte could ever reproduce) closes it for good.

Source

pub fn is_approved(&self, key: &str) -> bool

Has key previously been granted “for session”?

Source

pub fn approve(&self, key: &str)

Record key as approved for the rest of this session. A poisoned lock (a prior panic while held) is treated as “cache unavailable” — silently drops the grant rather than panicking the caller; the next identical call simply re-prompts, which is the fail-closed direction (a lost cache entry costs an extra prompt, never a skipped one).

Trait Implementations§

Source§

impl Debug for ApprovalCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for ApprovalCache

Source§

fn default() -> ApprovalCache

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more