Skip to content

Prompts/extensions bypass symlink & ownership guards (codex/pi) and OpenCode install accumulates .bak.<ts> files #1916

Closed

Description

@Quinnod345

Summary

While auditing target writers (codex, pi, opencode) across src/targets/ and src/commands/, we found and reproduced three issues in how managed artifacts and backups are written and cleaned up:

  1. Prompts & extensions bypass symlink and unmanaged-file guards (src/targets/codex.ts, src/targets/pi.ts, src/commands/cleanup.ts) — clobbering user symlinks on install/sync and deleting preserved symlinks on cleanup.
  2. Idempotent OpenCode installs accumulate unbounded .bak.<timestamp> files inside .opencode/commands/ and opencode.json (src/targets/opencode.ts:123, 169-174) — unlike src/targets/codex.ts:218-227, which diffs content before backing up.
  3. CLI --help documents --permissions from-command (singular) while permissionModes requires from-commands (plural) (src/commands/install.ts:15, 59, 86-90).

1. Prompts & extensions clobber user symlinks and unmanaged files (codex.ts, pi.ts)

Skills and agents explicitly preserve user symlinks and unmanaged files via cleanupCurrentManagedSkillDir and cleanupCurrentManagedAgentFile (src/targets/codex.ts:364-403, src/targets/pi.ts:454-490), and skip symlinks during removal via if (await isPreservedSymlink(entryPath)) continue (codex.ts:321, 351, pi.ts:402, 446).

However, prompts and extensions bypass these guards:

  • Writes through symlinks & claims manifest ownership:
    • src/targets/codex.ts:57-61 and src/targets/pi.ts:95-99, 150-154 call await writeText(targetPath, ...) (fs.writeFile) directly without checking whether targetPath is a symlink or an unmanaged file, and then record all bundle prompt/extension names in writeInstallManifest (codex.ts:177, pi.ts:174-175). Because fs.writeFile follows symlinks, if a user has symlinked a custom prompt or Pi extension into ~/.codex/prompts/, .pi/prompts/, or .pi/extensions/, install/sync overwrites the symlink target on disk and claims ownership in install-manifest.json.
  • Deletes preserved symlinks on removal:
    • cleanupRemovedPrompts (src/targets/codex.ts:326-338, src/targets/pi.ts:407-419) and cleanupRemovedExtensions (src/targets/pi.ts:421-433) omit if (await isPreservedSymlink(entryPath)) continue.
  • Legacy Pi prompt cleanup skips ownership check:
    • cleanupKnownLegacyPiArtifacts (src/targets/pi.ts:532-535) and cleanupPi (src/commands/cleanup.ts:411-413) move matching prompt files without checking ownedPrompts (whereas src/targets/codex.ts:428-430 checks if (ownedPrompts !== null && !ownedPrompts.has(promptName)) continue).

Runnable Reproduction (node -e)

node -e '
import("node:fs/promises").then(async (fs) => {
  const os = await import("node:os");
  const path = await import("node:path");
  const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "cep-repro-"));
  const userRepoPrompt = path.join(tmp, "my-dotfiles-review.md");
  const codexPromptsDir = path.join(tmp, ".codex-prompts");
  await fs.mkdir(codexPromptsDir, { recursive: true });
  await fs.writeFile(userRepoPrompt, "# User custom prompt (DO NOT CLOBBER)\n", "utf8");

  // User symlinks their dotfile prompt into ~/.codex/prompts/review.md
  const targetPath = path.join(codexPromptsDir, "review.md");
  await fs.symlink(userRepoPrompt, targetPath);

  // writeCodex (src/targets/codex.ts:57-61) calls writeText(targetPath, content) without symlink guard
  await fs.writeFile(targetPath, "# Overwritten by plugin writeText\n", "utf8");
  const afterContent = await fs.readFile(userRepoPrompt, "utf8");
  console.log("User dotfile target after writeText:", JSON.stringify(afterContent.trim()));
  await fs.rm(tmp, { recursive: true, force: true });
});
'

Output:

User dotfile target after writeText: "# Overwritten by plugin writeText"

Suggested Fix:

  • Gate prompt and extension writes with a cleanupCurrentManagedFile check mirroring cleanupCurrentManagedAgentFile (codex.ts:386-403), and only record actually-written names in writeInstallManifest.
  • Add if (await isPreservedSymlink(entryPath)) continue to cleanupRemovedPrompts (codex.ts:331, pi.ts:412) and cleanupRemovedExtensions (pi.ts:426).
  • Guard legacy prompt cleanup in pi.ts:532-535 and cleanup.ts:411-413 with if (ownedPrompts !== null && !ownedPrompts.has(promptName)) continue.

2. Unbounded .bak.<timestamp> accumulation inside .opencode/commands/ (src/targets/opencode.ts:123, 169-174)

In src/targets/codex.ts:218-227, hooks.json is only backed up and written when mergedContent !== existingContent.

By contrast, src/targets/opencode.ts:123 and 169-174 call backupFile unconditionally before every write:

// src/targets/opencode.ts:169-174
for (const [name, content] of Object.entries(bundle.commandFiles)) {
  const commandPath = path.join(paths.commandsDir, `${name}.md`)
  const backupPath = await backupFile(commandPath)
  ...
  await writeText(commandPath, `${content}\n`)
}

Every idempotent install or sync run creates a new opencode.json.bak.<timestamp> and a new <cmd>.md.bak.<timestamp> inside the live .opencode/commands/ directory for every command in the bundle, polluting .opencode/commands/.

Suggested Fix:
Read existingContent via await fs.readFile(commandPath, "utf8").catch(() => null) (and similarly for paths.configPath at line 123); when existingContent === ${content}\n``, skip both backupFile and `writeText`.


3. CLI --permissions from-command help text mismatch (src/commands/install.ts:15, 59, 86-90)

  • src/commands/install.ts:59 documents:
    --permissions <mode> Permission seeding mode (none | broad | from-command)
  • src/commands/install.ts:15 defines:
    const permissionModes: PermissionsMode[] = ["none", "broad", "from-commands"]
  • Passing the documented --permissions from-command flag throws Error: Unknown permissions mode: from-command at line 87. (Also in cloneGitHubRepo at src/commands/install.ts:330-334, passing "--", source, destination to git clone prevents option injection on positional args.)

Note on Falsified Findings (Filtered Out Before Filing)

We ran this audit using a 4-pass Ponder session (Builder, Skeptic, Dreamer, Empath). On Pass 1, custom relative paths in manifest.hooks / manifest.skills (src/parsers/claude.ts:135-141, 237-249) were flagged as a potential path-traversal vector; on Pass 2, Skeptic inspected resolveWithinRoot (src/parsers/claude.ts:263-270), verified that all manifest paths are strictly checked against resolvedRoot + path.sep, and retracted that finding (op: "delete") so no maintainer time is wasted on a false alarm.

Generated by Ponder — AI that knows when to speak, and when to shut up.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions