T09 · Insecure Skill Coding Practices
- Location
scripts/check_status.sh:50- Finding
Predictable Temporary File Allows Symlink-Based File Clobbering
- Content
View full analysis
Vulnerability Details
File Location:
scripts/check_status.sh, lines 50-59
Vulnerability Type: Predictable and insecure temporary-file handling
Risk Level: MediumVulnerable Code
bash if mcporter call healthdata.list_available_tables > /tmp/health_test.json 2>/dev/null; then echo "✅ 基础查询功能正常" # 显示前3个表名 if command -v jq &> /dev/null; then echo "前3个数据表:" jq -r '.[0:3][].name' /tmp/health_test.json 2>/dev/null | sed 's/^/ - /' fi rm -f /tmp/health_test.jsonTechnical Analysis
The script redirects command output to the fixed, globally predictable path
/tmp/health_test.json. It neither creates the file atomically with restrictive permissions nor verifies that the path is not a symbolic link.On systems where
/tmpis writable by other local users, an attacker can create that path as a symbolic link before the script runs. Shell redirection follows the link and truncates or overwrites its target with the output ofmcporter. The laterrm -fremoves the symbolic link itself and does not restore the overwritten target.The temporary MCP response may also be readable by other local users if the invoking process has a permissive
umask.Attack Path
- A local attacker identifies a file that the victim can overwrite.
- The attacker creates a symbolic link at the predictable path:
bash ln -s /path/to/victim-writable-target /tmp/health_test.json - The victim runs
scripts/check_status.sh. - The shell follows the symbolic link while processing the output redirection.
- The target is truncated and then populated with the output of
healthdata.list_available_tables. - The script removes
/tmp/health_test.json, deleting the link but leaving the target corrupted.
This attack requires local access sufficient to create entries in
/tmp. Its effective privileges are those of the user running the script.Impact Assessment
An attacker can cause arbitrary fi ...[truncated 697 chars]
- Remediation
View remediation
Remediation Suggestions
Create the temporary file atomically with
mktemp, store its path in a quoted variable, and register cleanup with a trap:bash tmp_file=$(mktemp "${TMPDIR:-/tmp}/health_test.XXXXXX") || { echo "Failed to create a secure temporary file" >&2 exit 1 } trap 'rm -f -- "$tmp_file"' EXIT HUP INT TERM if mcporter call healthdata.list_available_tables > "$tmp_file" 2>/dev/null; then echo "Basic query succeeded" if command -v jq &> /dev/null; then jq -r '.[0:3][].name' "$tmp_file" 2>/dev/null | sed 's/^/ - /' fi else echo "Basic query failed" fiAdditional hardening measures:
- Set a restrictive
umask, such asumask 077, before writing potentially sensitive MCP output. - Never run the status-check script with elevated privileges unless strictly necessary.
- Keep all temporary-path variables quoted and use
--before filename arguments. - Prefer processing the MCP output through a pipeline without writing it to disk when practical.
- Ensure cleanup occurs on normal exit and interruption by retaining the
trap.
- Set a restrictive
