Use unique temporary ssh key file for each ssh remote operation

File operations may run in parallel so we cannot rely on a well defined
keyfilename
This commit is contained in:
Johannes Zellner
2025-12-12 15:50:29 +01:00
parent 1ef252fbc2
commit 854fbe53be
+3 -2
View File
@@ -27,6 +27,7 @@ exports = module.exports = {
const assert = require('node:assert'),
BoxError = require('../boxerror.js'),
crypto = require('crypto'),
debug = require('debug')('box:storage/filesystem'),
df = require('../df.js'),
fs = require('node:fs'),
@@ -210,7 +211,7 @@ async function copyInternal(config, fromPath, toPath, options, progressCallback)
if (config._provider === mounts.MOUNT_TYPE_SSHFS) {
// we use a temporary key file instead of passing it as stdin
const identityFilePath = `/tmp/identity_file${config._managedMountPath.replaceAll('/', '-')}`;
const identityFilePath = `/tmp/identity_file_${crypto.randomUUID()}`;
// have to unlink first, in case a previous run crash before cleanup. With mode 0c600 we cannot overwrite it
safe.fs.unlinkSync(identityFilePath);
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}`);
@@ -273,7 +274,7 @@ async function removeDir(config, limits, remotePathPrefix, progressCallback) {
if (config._provider === mounts.MOUNT_TYPE_SSHFS) {
// we use a temporary key file instead of passing it as stdin
const identityFilePath = `/tmp/identity_file${config._managedMountPath.replaceAll('/', '-')}`;
const identityFilePath = `/tmp/identity_file_${crypto.randomUUID()}`;
// have to unlink first, in case a previous run crash before cleanup. With mode 0c600 we cannot overwrite it
safe.fs.unlinkSync(identityFilePath);
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}`);