mcp-ssh-manager 3.8.3 → 3.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -154,9 +154,14 @@ export function removeHostKey(host, port = 22) {
154
154
  */
155
155
  export async function addHostKey(host, port = 22, keyData = null) {
156
156
  try {
157
- // Backup current known_hosts
158
- if (fs.existsSync(KNOWN_HOSTS_PATH)) {
157
+ // Backup current known_hosts. Copy first and treat "not there" as nothing
158
+ // to back up, rather than checking then copying: between the two calls the
159
+ // file can be replaced, and the check adds a race without preventing the
160
+ // failure it appears to guard against.
161
+ try {
159
162
  fs.copyFileSync(KNOWN_HOSTS_PATH, KNOWN_HOSTS_BACKUP);
163
+ } catch (error) {
164
+ if (error.code !== 'ENOENT') throw error;
160
165
  }
161
166
 
162
167
  // If no key data provided, fetch it
@@ -168,11 +173,11 @@ export async function addHostKey(host, port = 22, keyData = null) {
168
173
  keyData = fingerprints.map(fp => fp.fullKey).join('\n');
169
174
  }
170
175
 
171
- // Ensure .ssh directory exists
176
+ // Ensure .ssh directory exists. No existsSync guard: mkdirSync with
177
+ // recursive:true is already a no-op on an existing directory, and checking
178
+ // first only opens a window where the directory can change underneath us.
172
179
  const sshDir = path.dirname(KNOWN_HOSTS_PATH);
173
- if (!fs.existsSync(sshDir)) {
174
- fs.mkdirSync(sshDir, { mode: 0o700, recursive: true });
175
- }
180
+ fs.mkdirSync(sshDir, { mode: 0o700, recursive: true });
176
181
 
177
182
  // Append to known_hosts
178
183
  fs.appendFileSync(KNOWN_HOSTS_PATH, keyData + '\n');