T09 · Insecure Skill Coding Practices
- Location
scripts/refactor-memory.js:190- Finding
Unconditional Replacement of the Persistent Memory Index
- Content
View full analysis
Vulnerability Details
File Location:
scripts/refactor-memory.js, lines 188-191
Vulnerability Type: Destructive file overwrite
Risk Level: MediumVulnerable Code
js ` await writeFile(MEMORY_INDEX, index, 'utf-8') console.log(`✅ Updated index: ${MEMORY_INDEX}`)Technical Analysis
MEMORY_INDEXresolves toMEMORY.mdin the selected OpenClaw workspace. ThewriteFileoperation uses its default behavior, which truncates an existing file before writing the newly generated index.The script neither reads and merges the existing index nor checks whether it contains user-maintained information. It also does not create a backup, request confirmation, or use an atomic replacement process. Consequently, running a migration that identifies at least one eligible Markdown file causes the entire existing persistent memory index to be replaced.
This is particularly unsafe because
MEMORY.mdcan contain long-term Agent context unrelated to the files being migrated.Attack Path
- An eligible Markdown file exists or is placed in the workspace's top-level
memory/directory. - A user or Agent invokes the migration script with write access to the workspace.
- The script classifies at least one file, causing
migrated.lengthto become greater than zero. updateMemoryIndex()constructs a new index containing only the migration results.writeFile(MEMORY_INDEX, ...)truncates and replaces the existingMEMORY.md.- Any unrelated long-term memory previously stored in that file is lost.
No privilege escalation occurs. The operation is limited to files writable by the account running the script.
Impact Assessment
An attacker able to influence the workspace's
memory/directory could trigger replacement when an authorized user runs the migration. The resulting impact includes:- Loss of persistent Agent context and user-maintained memory.
- Removal of existing refere ...[truncated 323 chars]
- An eligible Markdown file exists or is placed in the workspace's top-level
- Remediation
View remediation
Remediation Suggestions
- Read and preserve the existing
MEMORY.mdcontent before generating updates. - Merge new entries into the existing index instead of replacing the entire file.
- Create a timestamped backup before any modification.
- Require explicit confirmation before replacing a non-empty index.
- Write the updated content to a temporary file in the same directory, flush it, and atomically rename it into place.
- Implement a functional
--dry-runmode that displays all planned changes without writing files. - Detect duplicate entries so repeated migrations remain idempotent.
- Read and preserve the existing
