T09 · Insecure Skill Coding Practices
- Location
scripts/cron-limited.sh:62- Finding
Arbitrary Python Code Execution Through Unsafely Interpolated Arguments
- Content
View full analysis
Vulnerability Details
File Location:
scripts/cron-limited.sh, lines 62-136
Vulnerability Type: Python source-code injection
Risk Level: HighVulnerable Code
bash get_next_lunar_date() { local month=$1 local day=$2 local year=${3:-""} /tmp/lunar-venv/bin/python3 -c " from lunarcalendar import Lunar, Converter import datetime today = datetime.date.today() if '$year': year = int('$year') else: year = today.year lunar = Lunar(year, $month, $day) solar = Converter.Lunar2Solar(lunar) print(f'{solar.year}-{solar.month:02d}-{solar.day:02d}') " } calculate_reminder_date() { local solar_date="$1" local days_before=$2 /tmp/lunar-venv/bin/python3 -c " import datetime date = datetime.date.fromisoformat('$solar_date') reminder = date - datetime.timedelta(days=$days_before) print(reminder.isoformat()) " } save_birthday_config() { local lunar_month="$1" local lunar_day="$2" local message="$3" local time="$4" local days_before="$5" local channel="$6" local to="$7" init_config /tmp/lunar-venv/bin/python3 << PYEOF import json with open('$CONFIG_FILE', 'r') as f: data = json.load(f) for entry in data: if entry['lunar_month'] == $lunar_month and entry['lunar_day'] == $lunar_day: entry['message'] = '''$message'''.strip() entry['time'] = '$time' entry['days_before'] = $days_before entry['channel'] = '$channel' entry['to'] = '$to' with open('$CONFIG_FILE', 'w') as f: json.dump(data, f, ensure_ascii=False, indent=2) print('updated') exit(0) new_entry = { 'id': 'lunar_${lunar_month}_${lunar_day}', 'lunar_month': $lunar_month, 'lunar_day': $lunar_day, 'message': '''$message'''.strip(), 'time': '$time', 'days_before': $days_before, 'channel': '$channel', ...[truncated 2276 chars]- Remediation
View remediation
Remediation Suggestions
- Do not construct Python source code using shell-variable interpolation.
- Pass all data as ordinary values through positional arguments, environment variables, or JSON on standard input. For example:
bash python3 - "$month" "$day" "$year" <<'PYEOF' import sys from lunarcalendar import Lunar, Converter month = int(sys.argv[1]) day = int(sys.argv[2]) year = int(sys.argv[3]) if sys.argv[3] else None # Continue using parsed values. PYEOF - Quote the heredoc delimiter (
<<'PYEOF') to disable shell interpolation inside Python code. - Serialize birthday records as JSON and let Python parse the JSON as data rather than source code.
- Validate all fields before use:
- Lunar month and day must contain digits only and must be within supported calendar ranges.
days_beforemust be a non-negative integer within a reasonable upper bound.- Time must match a strict
HH:MMformat and represent a valid time. - Channel and destination values should be checked against expected formats.
- Use atomic, permission-restricted writes for
birthdays.jsonand fail safely on malformed configuration. - Add regression tests containing quotes, triple quotes, semicolons, newlines, Python comments, and expression-breaking payloads.
