Skip to content

Commit b724743

Browse files
sjnimsclaude
andauthored
docs: use secure mktemp pattern instead of predictable temp files (#158)
## Description Replace predictable temp file pattern `${FILE}.tmp.$$` with secure `mktemp` pattern in documentation examples. ## Type of Change - [x] Documentation update (improvements to README, CLAUDE.md, or component docs) ## Component(s) Affected - [x] Skills (methodology and best practices) ## Motivation and Context The `${FILE}.tmp.$$` pattern is a known security anti-pattern because: - `$$` (PID) is predictable and can be enumerated - Attackers could pre-create symlinks to sensitive files - Race conditions between check and use While this is documentation (not executable code), it teaches developers patterns they might copy into production scripts. Fixes #153 ## Solution Replaced all 6 instances with secure `mktemp` pattern: ```bash TEMP_FILE=$(mktemp) || exit 1 ``` ## Changes | File | Instances | |------|-----------| | `parsing-techniques.md` | 3 (lines 202, 219, 228) | | `real-world-examples.md` | 3 (lines 204, 295, 359) | ## Testing - [x] Linting passes (markdownlint) - [x] Verified no remaining `${FILE}.tmp.$$` patterns --- 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 52248e4 commit b724743

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

plugins/plugin-dev/skills/plugin-settings/references/parsing-techniques.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ Always use temp file + atomic move to prevent corruption:
198198
FILE=".claude/my-plugin.local.md"
199199
NEW_VALUE="updated_value"
200200

201-
# Create temp file
202-
TEMP_FILE="${FILE}.tmp.$$"
201+
# Create secure temp file (unpredictable name)
202+
TEMP_FILE=$(mktemp) || { echo "Failed to create temp file" >&2; exit 1; }
203+
trap 'rm -f "$TEMP_FILE"' EXIT
203204

204205
# Update field using sed
205206
sed "s/^field_name: .*/field_name: $NEW_VALUE/" "$FILE" > "$TEMP_FILE"
@@ -215,17 +216,17 @@ mv "$TEMP_FILE" "$FILE"
215216
CURRENT=$(echo "$FRONTMATTER" | grep '^iteration:' | sed 's/iteration: *//')
216217
NEXT=$((CURRENT + 1))
217218

218-
# Update file
219-
TEMP_FILE="${FILE}.tmp.$$"
219+
# Update file (secure temp file)
220+
TEMP_FILE=$(mktemp) || exit 1
220221
sed "s/^iteration: .*/iteration: $NEXT/" "$FILE" > "$TEMP_FILE"
221222
mv "$TEMP_FILE" "$FILE"
222223
```
223224

224225
### Update Multiple Fields
225226

226227
```bash
227-
# Update several fields at once
228-
TEMP_FILE="${FILE}.tmp.$$"
228+
# Update several fields at once (secure temp file)
229+
TEMP_FILE=$(mktemp) || exit 1
229230

230231
sed -e "s/^iteration: .*/iteration: $NEXT_ITERATION/" \
231232
-e "s/^pr_number: .*/pr_number: $PR_NUMBER/" \

plugins/plugin-dev/skills/plugin-settings/references/real-world-examples.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ NEXT_ITERATION=$((ITERATION + 1))
200200
# Extract prompt from markdown body
201201
PROMPT_TEXT=$(awk '/^---$/{i++; next} i>=2' "$RALPH_STATE_FILE")
202202

203-
# Update iteration counter
204-
TEMP_FILE="${RALPH_STATE_FILE}.tmp.$$"
203+
# Update iteration counter (secure temp file)
204+
TEMP_FILE=$(mktemp) || exit 1
205205
sed "s/^iteration: .*/iteration: $NEXT_ITERATION/" "$RALPH_STATE_FILE" > "$TEMP_FILE"
206206
mv "$TEMP_FILE" "$RALPH_STATE_FILE"
207207

@@ -292,12 +292,12 @@ enabled: true
292292
Both use temp file + atomic move:
293293
294294
```bash
295-
TEMP_FILE="${FILE}.tmp.$$"
295+
TEMP_FILE=$(mktemp) || exit 1
296296
sed "s/^field: .*/field: $NEW_VALUE/" "$FILE" > "$TEMP_FILE"
297297
mv "$TEMP_FILE" "$FILE"
298298
```
299299

300-
**Why:** Prevents corruption if process is interrupted.
300+
**Why:** Prevents corruption if process is interrupted. Using `mktemp` creates a secure, unpredictable filename.
301301

302302
### 4. Quote Handling
303303

@@ -355,8 +355,8 @@ echo "$VALUE"
355355
# BAD: Can corrupt file if interrupted
356356
sed -i "s/field: .*/field: $VALUE/" "$FILE"
357357

358-
# GOOD: Atomic
359-
TEMP_FILE="${FILE}.tmp.$$"
358+
# GOOD: Atomic with secure temp file
359+
TEMP_FILE=$(mktemp) || exit 1
360360
sed "s/field: .*/field: $VALUE/" "$FILE" > "$TEMP_FILE"
361361
mv "$TEMP_FILE" "$FILE"
362362
```

0 commit comments

Comments
 (0)