Use a temporary identity file for remote ssh copy

This commit is contained in:
Johannes Zellner
2025-11-13 10:27:33 +01:00
parent fbbd71e7f2
commit afde81ef3e

View File

@@ -208,11 +208,14 @@ async function copyInternal(config, fromPath, toPath, options, progressCallback)
cpOptions += config.noHardlinks ? '' : 'l'; // this will hardlink backups saving space
if (config._provider === mounts.MOUNT_TYPE_SSHFS) {
const identityFilePath = path.join(paths.SSHFS_KEYS_DIR, `identity_file_${path.basename(config._managedMountPath)}`);
// we use a temporary key file instead of passing it as stdin
const identityFilePath = `/tmp/identity_file_${config._managedMountPath.replaceAll('/', '-')}`;
if (!safe.fs.writeFileSync(identityFilePath, `${config.mountOptions.privateKey}\n`, { mode: 0o600 })) throw new BoxError(BoxError.FS_ERROR, `Could not write temporary private key: ${safe.error.message}`);
const sshOptions = [ '-o', '"StrictHostKeyChecking no"', '-i', identityFilePath, '-p', config.mountOptions.port, `${config.mountOptions.user}@${config.mountOptions.host}` ];
const sshArgs = sshOptions.concat([ 'cp', cpOptions, path.join(config.prefix, fromPath), path.join(config.prefix, toPath) ]);
const [remoteCopyError] = await safe(shell.spawn('ssh', sshArgs, { shell: true }));
safe.fs.unlinkSync(identityFilePath);
if (!remoteCopyError) return;
if (remoteCopyError.code === 255) throw new BoxError(BoxError.EXTERNAL_ERROR, `SSH connection error: ${remoteCopyError.message}`); // do not attempt fallback copy for ssh errors
debug('SSH remote copy failed, trying sshfs copy'); // this can happen for sshfs mounted windows server