Skip to content

generate_ssh_config can delete user content from ~/.ssh/config when the managed markers are orphaned #31

Closed

Description

@TMHSDigital

Why it matters

generate_ssh_config with target: "user_ssh_config" can silently delete user-written content from ~/.ssh/config. That breaks the README's promise that content outside the markers is never touched, and ~/.ssh/config is a file people care about a lot.

Evidence

src/sshconfig.ts:43-55 (applyManagedBlock) looks for the first BEGIN and the first END. If only one marker exists (the user deleted # END tailscale-mcp by hand, a merge went wrong, a dotfiles sync dropped a line), it treats the config as having no managed block and appends a new one. The orphaned BEGIN stays behind. On the next run, the first BEGIN (the orphan) pairs with the new END, and everything between them is replaced.

Repro against dist/sshconfig.js:

let cfg = "Host a\n  HostName a\n# BEGIN tailscale-mcp\nHost keepme\n  HostName important\n";
let r1 = applyManagedBlock(cfg, body);   // keepme still present
let r2 = applyManagedBlock(r1, body);    // keepme GONE

Output: keepme after run1: true after run2: false.

The same thing happens when END comes before BEGIN, or when there are duplicate marker pairs.

Suggested approach

  • Parse the markers strictly: require exactly one BEGIN line and exactly one END line after it, each matched as a whole line (not with indexOf anywhere in the file).
  • If the markers are malformed (orphaned, reversed, or duplicated), refuse to write and return a structured error (e.g. SSH_CONFIG_MARKERS_CORRUPT) that names the line numbers and tells the user how to fix it.
  • Add tests for orphan BEGIN, orphan END, reversed, duplicate pairs, and markers inside a comment line.

Done when

  • No input to applyManagedBlock can ever drop a byte outside a well-formed marker pair (a property-style test is ideal).
  • Malformed markers produce a structured error and leave the file untouched.

Activity

  1. added
    bug
    priority: P1
    on Oct 25, 2026
  2. added a commit that references this issue on Oct 25, 2026
    9ea857c
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

    bugpriority: P1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions