T09 · Insecure Skill Coding Practices
- Location
scripts/pick.py:11- Finding
Unbounded Weighted-Selection Expansion Allows Memory Exhaustion
- Content
View full analysis
Vulnerability Details
File Location:
scripts/pick.py, lines 11–14
Vulnerability Type: Uncontrolled memory allocation through attacker-controlled weights
Risk Level: MediumVulnerable Code
python weight_map = dict(w.split(':') for w in weights.split(',')) for m in pool: w = int(weight_map.get(m, 1)) weight_list.extend([m] * w) return random.sample(weight_list, min(count, len(pool)))Technical Analysis
The
--weightedargument is parsed into integer weights without validating their range. Each weight is then represented by duplicating the corresponding member name inweight_list.Memory consumption is therefore proportional to the sum of user-supplied weights rather than the number of members. An attacker who can invoke the skill can provide an extremely large positive integer, causing Python to attempt a correspondingly large allocation. This may stall the process, raise
MemoryError, or trigger operating-system termination.Attack Path
- The attacker invokes the skill with at least one member and an excessively large weight, for example:
bash python3 scripts/pick.py --from "Alice" --weighted "Alice:1000000000" int()accepts the large value.[m] * wattempts to construct a list containing one billion references.weight_list.extend(...)consumes excessive process and system memory.- The skill or its hosting agent may become unresponsive or terminate.
Impact Assessment
Exploitation does not grant additional privileges, code execution, or data access. Its impact is availability loss within the privileges of the invoking process. It can terminate or degrade the skill, the hosting agent, and potentially other workloads on the same resource-constrained host.
- The attacker invokes the skill with at least one member and an excessively large weight, for example:
- Remediation
View remediation
Remediation Suggestions
- Parse weights with explicit exception handling and reject malformed values.
- Require every weight to be a positive integer within a conservative maximum.
- Limit the number of members and the aggregate accepted weight.
- Avoid materializing repeated entries. Use a bounded weighted-selection algorithm based on cumulative weights or
random.choices(). - If selections must be unique, implement weighted sampling without replacement rather than sampling duplicated list entries.
- Add tests covering zero, negative, malformed, and extremely large weights.
- Run the skill with appropriate memory and execution-time limits as defense in depth.
Example validation pattern:
python MAX_WEIGHT = 10_000 try: weight = int(weight_map.get(member, 1)) except ValueError: raise ValueError(f"Invalid weight for {member}") if not 1 <= weight <= MAX_WEIGHT: raise ValueError(f"Weight for {member} must be between 1 and {MAX_WEIGHT}")
