Back to skill

Security audit

LLM Result Cache

Security checks for vulnerabilities and agentic risk

Overview

This is a small disclosed Python disk-cache helper with ordinary local file persistence and no hidden network, credential, or execution behavior.

Install only if you are comfortable with results being written to a local JSON cache file you choose. Avoid caching secrets or regulated data unless you protect the file appropriately, and use a custom key_fn whenever the result depends on more than the first argument, user identity, tenant, model, prompt, or options.

Vulnerability Patterns
  • Insecure Skill Coding PracticesFinds exploitable flaws such as hardcoded secrets or command injection
  • Skill Instruction HijackingAlters the agent's session goals or safety constraints when the skill loads
  • Agent Memory PoisoningWrites attacker-controlled rules into memory that affect later sessions
  • Remote Payload Retrieval and ExecutionFetches external code whose behavior can change after review
  • Embedded Malicious CodeShips malicious scripts inside the skill and executes them locally
Findings (2)

T09 · Insecure Skill Coding Practices

Warning
Location
llm_result_cache.py:91
Finding
Incomplete Default Cache Key Causes Cross-Context Result Confusion<![CDATA[ ## Vulnerability Details **File Location**: `llm_result_cache.py`, lines 91–98 **Vulnerability Type**: Insecure cache-key construction **Risk Level**: Medium ### Vulnerable Code ```python def decorator(fn: Callable) -> Callable: @wraps(fn) def wrapper(*args, **kwargs): key = key_fn(*args, **kwargs) if key_fn else str(args[0]) if args else "" hit = cache.get(key) if hit is not None: return hit result = fn(*args, **kwargs) if isinstance(result, dict): cache.set(key, result) return result ``` ### Technical Analysis When no custom `key_fn` is supplied, the decorator derives the cache key exclusively from the first positional argument. It ignores: - Additional positional arguments - All keyword arguments - Tenant or user identity - Authorization context - Model and prompt versions - Configuration options that can affect the result Consequently, semantically different function calls can resolve to the same cache entry. For example, calls such as `analyze(document, tenant="A")` and `analyze(document, tenant="B")` use an identical default key if `document` is the same. The cached value is returned immediately, so the wrapped function and any authorization-sensitive or context-sensitive processing it performs are skipped. ### Attack Path 1. An application decorates a multi-argument or keyword-sensitive function with `@cached(cache)` without providing `key_fn`. 2. An attacker or ordinary user invokes the function with a chosen first argument and attacker-controlled secondary arguments. 3. The resulting dictionary is stored under a key derived only from the first argument. 4. Another caller invokes the function with the same first argument but different tenant, permission, model, prompt, or processing options. 5. The decorator finds the previously stored value and returns it without executing the wrapped function. 6. The second caller receives a result generated for a differe ...[truncated 667 chars]
Remediation
<![CDATA[ ## Remediation Suggestions Construct the default key from all arguments rather than only the first positional argument. 1. Canonically serialize both `args` and sorted `kwargs`. 2. Hash the serialization to produce a bounded key. 3. Include all security-relevant context, including tenant identity, user identity, model version, prompt version, and processing options. 4. Reject unsupported or non-deterministically serializable arguments instead of silently producing an incomplete key. 5. For security-sensitive functions, require an explicit `key_fn` rather than supplying an unsafe fallback. 6. Add tests proving that changes to any result-affecting argument produce distinct cache keys. Example approach: ```python import hashlib import json def default_key(args, kwargs): payload = json.dumps( {"args": args, "kwargs": kwargs}, sort_keys=True, separators=(",", ":"), default=repr, ) return hashlib.sha256(payload.encode("utf-8")).hexdigest() ``` Applications should still provide an explicit key function where identity or authorization context is not directly present in the function arguments. ]]>

T09 · Insecure Skill Coding Practices

Note
Location
llm_result_cache.py:43
Finding
Structurally Invalid Cache Data Can Cause Persistent Denial of Service<![CDATA[ ## Vulnerability Details **File Location**: `llm_result_cache.py`, lines 43–75 **Vulnerability Type**: Insufficient validation of persisted JSON data **Risk Level**: Low ### Vulnerable Code ```python def _load(self) -> dict[str, Any]: if self.cache_file.exists(): try: return json.loads(self.cache_file.read_text()) except Exception: # Corrupted cache file — treat as empty rather than crashing. # (This is a CACHE, not a source of truth, so losing it is # safe; a spend/audit ledger would need fail-closed handling # instead — see the ad-budget-governor skill for that case.) return {} return {} def _save(self, cache: dict[str, Any]) -> None: try: self.cache_file.write_text(json.dumps(cache, indent=2)) except Exception: pass # best-effort — a failed cache write should never break the caller def get(self, key: str) -> Optional[dict]: cache = self._load() entry = cache.get(key) if not entry: return None if time.time() - entry.get("cached_at", 0) > self.ttl_seconds: return None return entry.get("value") def set(self, key: str, value: dict) -> None: cache = self._load() cache[key] = {"cached_at": time.time(), "value": value} if len(cache) > self.max_entries: oldest_first = sorted(cache.items(), key=lambda kv: kv[1].get("cached_at", 0)) cache = dict(oldest_first[-self.max_entries:]) self._save(cache) ``` ### Technical Analysis The `_load()` method catches decoding and file-reading exceptions, but it does not validate the type or schema of successfully decoded JSON. Valid JSON is not necessarily a valid cache structure. For example: - A root value of `[]`, `null`, a string, or a number does not support `cache.get()` or item assignment. - An entry value that is a string, list, or number does not support `entry.get()`. - During eviction, any non-dictionary entry causes `k ...[truncated 1839 chars]
Remediation
<![CDATA[ ## Remediation Suggestions Validate the complete decoded structure before returning it from `_load()`: 1. Require the root JSON value to be a dictionary. 2. Require every cache entry to be a dictionary. 3. Validate that `cached_at` is a finite numeric timestamp. 4. Validate the presence and expected type of `value` according to the public API. 5. Treat any invalid root or entry as an empty cache, or discard only invalid entries. 6. Catch expected exceptions narrowly while ensuring malformed persisted state cannot escape into `get()` or `set()`. 7. Write updates atomically using a temporary file in the same directory followed by `os.replace()`. 8. Apply restrictive file permissions when cached values may contain sensitive data. Example validation pattern: ```python def _load(self) -> dict[str, Any]: try: decoded = json.loads(self.cache_file.read_text()) except (OSError, UnicodeError, json.JSONDecodeError): return {} if not isinstance(decoded, dict): return {} valid = {} for key, entry in decoded.items(): if not isinstance(key, str) or not isinstance(entry, dict): continue cached_at = entry.get("cached_at") value = entry.get("value") if not isinstance(cached_at, (int, float)): continue if not isinstance(value, dict): continue valid[key] = entry return valid ``` Atomic replacement should be used to prevent readers from observing partially written files. ]]>
Vulnerability Patterns
  • MCP Least PrivilegeUnderdeclared Capability, Wildcard Permission, Missing Permission Declaration
  • Prompt InjectionInstruction Override, Hidden Instructions, Exfiltration Commands
  • Data ExfiltrationExternal Transmission, Env Variable Harvesting, File System Enumeration
  • Privilege EscalationExcessive Permissions, Sudo/Root Execution, Credential Access
  • Supply ChainUnpinned Dependencies, External Script Fetching, Obfuscated Code
Findings (1)

Lp3

Medium
Category
MCP Least Privilege
Confidence
70% confidence
Finding
Without declared permissions the skill's intent is opaque and cannot be validated.

Static analysis

No suspicious patterns detected.