T09 · Insecure Skill Coding Practices
- Location
scripts/patch-live-card.py:31- Finding
Unscoped Source Transformation Can Corrupt the Target Watcher
- Content
View full analysis
Vulnerability Details
File Location:
scripts/patch-live-card.py, lines 31–41
Vulnerability Type: Unscoped source-code replacement
Risk Level: MediumVulnerable 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 contentTechnical 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_SIGNATUREwas not found. If the target watcher has changed between versions but still containsOLD_BODY_MARKER, the script can insert:python "reply_in_thread": reply_in_thread,into unrelated methods where
reply_in_threadis 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-pathargument 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
- A modified, incompatible, or newer
watcher.pycontains one or more occurrences of"content": json.dumps(card),. - Its expected
reply_card()signature is absent or has changed. - The user runs
python3 scripts/patch-live-card.py, potentially following the documented installation procedure. - The signature replacement makes no change, but the global body-marker replacem ...[truncated 917 chars]
- A modified, incompatible, or newer
- Remediation
View remediation
Remediation Suggestions
- Require the expected method signature to occur exactly once before attempting any modification.
- Scope the body-marker replacement to the body of
reply_card()rather than applying it to the entire file. - Require exactly one body-marker match in the intended method and abort on zero or multiple matches.
- Parse the target with Python's
astmodule or use a syntax-aware transformation instead of unrestricted string replacement. - Create a backup before modification and perform the update atomically using a temporary file followed by
os.replace(). - Validate the patched output with
ast.parse()before writing it. - Confirm after transformation that the new parameter and request-body field occur in the same intended method.
- Preserve original file permissions and provide a clear error without modifying the target when its structure is unsupported.
