T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/patch-live-card.py:31
- Finding
- Unscoped Source Transformation Can Corrupt the Target Watcher## Vulnerability Details **File Location**: `scripts/patch-live-card.py`, lines 31–41 **Vulnerability Type**: Unscoped source-code replacement **Risk Level**: Medium ### Vulnerable Code ```python def apply_patch(content: str) -> str: """Apply the patch to watcher.py content.""" # Patch signature content = content.replace(OLD_SIGNATURE, NEW_SIGNATURE) # Patch body - add reply_in_thread to the request body # Only add if not already present if '"reply_in_thread": reply_in_thread' not in content: content = content.replace( OLD_BODY_MARKER, NEW_BODY_LINES, ) return content ``` ### Technical Analysis The patcher uses unrestricted `str.replace()` operations across the complete target file. In particular, every occurrence of: ```python "content": json.dumps(card), ``` is replaced, rather than only the occurrence inside the expected `reply_card()` method. The body replacement can also proceed when `OLD_SIGNATURE` was not found. If the target watcher has changed between versions but still contains `OLD_BODY_MARKER`, the script can insert: ```python "reply_in_thread": reply_in_thread, ``` into unrelated methods where `reply_in_thread` is undefined. The script verifies only that the resulting text differs before writing it; it does not validate match counts, method scope, or Python syntax. The optional `--watcher-path` argument allows the user to select another file, increasing the scope of accidental modification, although exploitation still requires the patch script to be invoked against that file. ### Attack Path 1. A modified, incompatible, or newer `watcher.py` contains one or more occurrences of `"content": json.dumps(card),`. 2. Its expected `reply_card()` signature is absent or has changed. 3. The user runs `python3 scripts/patch-live-card.py`, potentially following the documented installation procedure. 4. The signature replacement makes no change, but the global body-marker replacem ...[truncated 917 chars]
- Remediation
- ## Remediation Suggestions 1. Require the expected method signature to occur exactly once before attempting any modification. 2. Scope the body-marker replacement to the body of `reply_card()` rather than applying it to the entire file. 3. Require exactly one body-marker match in the intended method and abort on zero or multiple matches. 4. Parse the target with Python's `ast` module or use a syntax-aware transformation instead of unrestricted string replacement. 5. Create a backup before modification and perform the update atomically using a temporary file followed by `os.replace()`. 6. Validate the patched output with `ast.parse()` before writing it. 7. Confirm after transformation that the new parameter and request-body field occur in the same intended method. 8. Preserve original file permissions and provide a clear error without modifying the target when its structure is unsupported.
