T09 · Insecure Skill Coding Practices
- Location
scripts/workspace_search.py:11- Finding
User-Controlled GNU grep Option Injection
- Content
View full analysis
Vulnerability Details
File Location:
scripts/workspace_search.py, lines 11–21
Vulnerability Type: Command argument injection through missing end-of-options delimiter
Risk Level: MediumVulnerable Code
python def grep_content(pattern, max_results=20): """Search file content using grep.""" cmd = [ "grep", "-rn", "--include=*.py", "--include=*.md", "--include=*.ts", "--include=*.js", "--include=*.json", "-i", pattern, WORKSPACE_PATH ] try: result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)Technical Analysis
The user-controlled
patternis inserted into thegrepargument list before the workspace path without a--end-of-options delimiter. Consequently, a pattern beginning with-can be interpreted by GNUgrepas one or more command-line options rather than as the intended search expression.Although the use of an argument list with
subprocess.run()prevents shell metacharacter injection, it does not prevent argument or option injection. For example, a compact argument such as-f/path/to/fileinstructs GNUgrepto read search patterns from the specified local file. Other injected options can modify matching, recursion, output, and resource-consumption behavior.Attack Path
- An attacker or untrusted caller supplies an option-like search pattern:
bash python scripts/workspace_search.py grep -f/path/to/readable/file - The script constructs the following effective invocation:
bash grep -rn --include=*.py --include=*.md --include=*.ts \ --include=*.js --include=*.json -i \ -f/path/to/readable/file ~/.openclaw/workspace - GNU
grepinterprets-f/path/to/readable/fileas an option and reads that file as a source of regular-expression patterns. - Those patterns are applied to files in the OpenClaw workspace.
- Matching out ...[truncated 879 chars]
- An attacker or untrusted caller supplies an option-like search pattern:
- Remediation
View remediation
Remediation Suggestions
Insert GNU's
--end-of-options delimiter immediately before the user-controlled pattern so that values beginning with-are handled as search expressions:python cmd = [ "grep", "-rn", "--include=*.py", "--include=*.md", "--include=*.ts", "--include=*.js", "--include=*.json", "-i", "--", pattern, WORKSPACE_PATH ]Additional hardening measures:
- Validate the pattern's length to reduce denial-of-service risk from excessively large or computationally expensive regular expressions.
- If regular-expression support is unnecessary, add
-Fto perform fixed-string matching. - If both literal and regular-expression searches are required, expose them as explicit modes rather than inferring behavior from user input.
- Check
result.returncodeand distinguish no-match status from operational errors. - Avoid returning raw exception details to untrusted callers where those details could expose local paths or runtime information.
- Add regression tests using patterns such as
-f/etc/passwd,--help, and--versionto verify that they are treated solely as search text.
