T09 · Insecure Skill Coding Practices
- Location
scripts/watch.mjs:12- Finding
Path Traversal and Unsafe Temporary State File Handling
- Content
View full analysis
Vulnerability Details
File Location:
scripts/watch.mjs, lines 12–13, 33–36, 54–63
Vulnerability Type: User-controlled path traversal and unsafe temporary-file handling
Risk Level: MediumVulnerable Code
js const STATE_DIR = path.join(os.tmpdir(), "clawsearch-watch"); fs.mkdirSync(STATE_DIR, { recursive: true }); const args = process.argv.slice(2); if (args.length === 0) usage(); const query = args[0]; const tag = args.includes("--tag") ? args[args.indexOf("--tag") + 1] : query.replace(/\W+/g, "_").slice(0, 40); const notify = args.includes("--notify") ? args[args.indexOf("--notify") + 1] : null; const stateFile = path.join(STATE_DIR, `${tag}.json`); // ... let previous = new Set(); if (fs.existsSync(stateFile)) { try { previous = new Set(JSON.parse(fs.readFileSync(stateFile, "utf8"))); } catch {} } const fresh = results.filter(r => !previous.has(r.url)); // Gem baseline fs.writeFileSync(stateFile, JSON.stringify([...current], null, 2));Technical Analysis
The value supplied through
--tagis incorporated directly intostateFilewithout validation or canonical containment checks. AlthoughSTATE_DIRis intended to constrain monitoring state to a temporary directory,path.join()does not prevent traversal components such as../. A crafted tag can therefore resolve to a.jsonpath outsideclawsearch-watch.The state directory is also predictable and created without explicitly restrictive permissions. The code separately checks existence and then reads or writes the path, and it does not reject symbolic links or use an exclusive, atomic file-creation strategy. In a shared temporary directory, these properties create additional local symlink and race-condition exposure.
The default tag generated from the query removes non-word characters and is comparatively constrained. The vulnerability specifically arises when an attacker or untrusted caller can control the explicit
--tagargument.Attack Path
- T ...[truncated 1796 chars]
- Remediation
View remediation
Remediation Suggestions
-
Validate explicit tags using a strict allowlist and length limit:
js const TAG_PATTERN = /^[A-Za-z0-9_-]{1,40}$/; if (!tag || !TAG_PATTERN.test(tag)) { throw new Error("Invalid tag: use 1-40 letters, digits, underscores, or hyphens"); } -
Resolve and verify the final path remains beneath the intended directory:
js const root = path.resolve(STATE_DIR); const stateFile = path.resolve(root, `${tag}.json`); if (!stateFile.startsWith(`${root}${path.sep}`)) { throw new Error("State path escapes the state directory"); } -
Create the state directory with restrictive permissions:
js fs.mkdirSync(STATE_DIR, { recursive: true, mode: 0o700 }); -
Reject symbolic-link targets by inspecting the destination with
lstatSync()and using no-follow file-opening semantics where supported. -
Replace the direct write with an atomic operation:
- Create a uniquely named temporary file inside
STATE_DIR. - Open it with exclusive creation and mode
0600. - Write and flush the data.
- Atomically rename it to the validated destination.
- Create a uniquely named temporary file inside
-
Avoid a separate existence check followed by file access. Open the validated file directly and handle
ENOENTto reduce time-of-check/time-of-use exposure. -
Treat tags as identifiers rather than path fragments. A robust alternative is to hash the tag and use the digest as the filename while storing the original tag only as data.
-
