Skip to content

Make ~/.ssh/config writes atomic, backed up, and permission-safe #42

Closed

Description

@TMHSDigital

Why it matters

~/.ssh/config is high-stakes. A write that's interrupted, written with the wrong permissions, or impossible to undo can lock a user out of their machines or make ssh refuse the file.

Evidence

src/tools/sshconfig.ts:62-92:

  • fs.writeFile(configPath, updated) writes in place. A crash or a concurrent write leaves a truncated config.
  • There's no backup before the in-place edit, so a bad managed block (see generate_ssh_config can delete user content from ~/.ssh/config when the managed markers are orphaned #31) can't be undone.
  • fs.mkdir(dir, { recursive: true }) creates ~/.ssh with default mode, not 0700. New files get the umask default rather than 0600. OpenSSH refuses a config that's writable by group or others ("Bad owner or permissions").
  • A read failure is swallowed (catch {}), so an EACCES on an existing config is treated as "no config" and the file is overwritten with just the managed block.

Suggested approach

  • Write to config.tmp, then rename it into place.
  • Keep config.tailscale-mcp.bak (the last pre-edit copy) and mention it in the response.
  • mkdir with mode 0o700 and write with mode 0o600, keeping the existing mode when the file already exists.
  • Only treat ENOENT as "no config". Rethrow other errors as a structured error.
  • On Windows, document that ACLs are left unchanged.

Done when

The write path is atomic, backed up, and permission-correct, and tests cover the EACCES and existing-mode cases.

Activity

  1. added
    enhancement
    priority: P2
    security-hardening
    on Oct 25, 2026
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

    enhancementpriority: P2security-hardening

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions