Compare commits

..
Author SHA1 Message Date
copilot-swe-agent[bot]andwhyour df7f13c6bf Optimize writeFileWithLock to avoid redundant chmod calls
- Track if file is newly created to skip redundant chmod
- Only call chmod for existing files that need permission changes
- Improves performance and reduces unnecessary system calls

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
2025-11-07 16:20:30 +00:00
copilot-swe-agent[bot]andwhyour 62831835a5 Fix SSH config file permissions race condition
- Modified writeFileWithLock to create files with correct permissions immediately
- Changed string mode values to proper octal numbers (0o600, 0o400)
- This eliminates the race condition where files existed with wrong permissions

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
2025-11-07 16:18:49 +00:00
copilot-swe-agent[bot] 8998b4078f Initial plan 2025-11-07 16:07:38 +00:00
3 changed files with 16 additions and 11 deletions
+4 -4
View File
@@ -26,13 +26,13 @@ export default class SshKeyService {
if (_exist) { if (_exist) {
config = await fs.readFile(this.sshConfigFilePath, { encoding: 'utf-8' }); config = await fs.readFile(this.sshConfigFilePath, { encoding: 'utf-8' });
} else { } else {
await writeFileWithLock(this.sshConfigFilePath, '', { mode: '600' }); await writeFileWithLock(this.sshConfigFilePath, '', { mode: 0o600 });
} }
if (!config.includes(this.sshConfigHeader)) { if (!config.includes(this.sshConfigHeader)) {
await writeFileWithLock( await writeFileWithLock(
this.sshConfigFilePath, this.sshConfigFilePath,
`${this.sshConfigHeader}\n\n${config}`, `${this.sshConfigHeader}\n\n${config}`,
{ mode: '600' }, { mode: 0o600 },
); );
} }
} }
@@ -46,7 +46,7 @@ export default class SshKeyService {
path.join(this.sshPath, alias), path.join(this.sshPath, alias),
`${key}${os.EOL}`, `${key}${os.EOL}`,
{ {
mode: '400', mode: 0o400,
}, },
); );
} catch (error) { } catch (error) {
@@ -83,7 +83,7 @@ export default class SshKeyService {
config, config,
{ {
encoding: 'utf8', encoding: 'utf8',
mode: '600', mode: 0o600,
}, },
); );
} }
+8 -3
View File
@@ -19,9 +19,13 @@ export async function writeFileWithLock(
if (typeof options === 'string') { if (typeof options === 'string') {
options = { encoding: options }; options = { encoding: options };
} }
let isNewFile = false;
if (!(await fileExist(filePath))) { if (!(await fileExist(filePath))) {
const fileHandle = await open(filePath, 'w'); // Create the file with the specified mode if provided, otherwise use default
fileHandle.close(); const fileMode = options?.mode || 0o666;
const fileHandle = await open(filePath, 'w', fileMode);
await fileHandle.close();
isNewFile = true;
} }
const lockfilePath = getUniqueLockPath(filePath); const lockfilePath = getUniqueLockPath(filePath);
@@ -35,7 +39,8 @@ export async function writeFileWithLock(
lockfilePath, lockfilePath,
}); });
await writeFile(filePath, content, { encoding: 'utf8', ...options }); await writeFile(filePath, content, { encoding: 'utf8', ...options });
if (options?.mode) { // Only chmod if the file already existed (not just created with the correct mode)
if (!isNewFile && options?.mode) {
await chmod(filePath, options.mode); await chmod(filePath, options.mode);
} }
await release(); await release();
+4 -4
View File
@@ -11362,7 +11362,7 @@ snapshots:
postcss-preset-env: 7.5.0(postcss@8.5.6) postcss-preset-env: 7.5.0(postcss@8.5.6)
rollup-plugin-visualizer: 5.9.0(rollup@3.29.5) rollup-plugin-visualizer: 5.9.0(rollup@3.29.5)
systemjs: 6.15.1 systemjs: 6.15.1
vite: 4.5.2(@types/node@17.0.45)(less@4.4.2)(lightningcss@1.22.1)(sass@1.54.0)(terser@5.44.1) vite: 4.5.2(@types/node@17.0.45)(less@4.1.3)(lightningcss@1.22.1)(sass@1.54.0)(terser@5.44.1)
transitivePeerDependencies: transitivePeerDependencies:
- '@types/node' - '@types/node'
- lightningcss - lightningcss
@@ -11877,7 +11877,7 @@ snapshots:
'@babel/plugin-transform-react-jsx-self': 7.27.1(@babel/core@7.28.5) '@babel/plugin-transform-react-jsx-self': 7.27.1(@babel/core@7.28.5)
'@babel/plugin-transform-react-jsx-source': 7.27.1(@babel/core@7.28.5) '@babel/plugin-transform-react-jsx-source': 7.27.1(@babel/core@7.28.5)
react-refresh: 0.14.2 react-refresh: 0.14.2
vite: 4.5.2(@types/node@17.0.45)(less@4.4.2)(lightningcss@1.22.1)(sass@1.54.0)(terser@5.44.1) vite: 4.5.2(@types/node@17.0.45)(less@4.1.3)(lightningcss@1.22.1)(sass@1.54.0)(terser@5.44.1)
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
@@ -18090,7 +18090,7 @@ snapshots:
react: 18.3.1 react: 18.3.1
react-dom: 18.3.1(react@18.3.1) react-dom: 18.3.1(react@18.3.1)
vite@4.5.2(@types/node@17.0.45)(less@4.4.2)(lightningcss@1.22.1)(sass@1.54.0)(terser@5.44.1): vite@4.5.2(@types/node@17.0.45)(less@4.1.3)(lightningcss@1.22.1)(sass@1.54.0)(terser@5.44.1):
dependencies: dependencies:
esbuild: 0.18.20 esbuild: 0.18.20
postcss: 8.5.6 postcss: 8.5.6
@@ -18098,7 +18098,7 @@ snapshots:
optionalDependencies: optionalDependencies:
'@types/node': 17.0.45 '@types/node': 17.0.45
fsevents: 2.3.3 fsevents: 2.3.3
less: 4.4.2 less: 4.1.3
lightningcss: 1.22.1 lightningcss: 1.22.1
sass: 1.54.0 sass: 1.54.0
terser: 5.44.1 terser: 5.44.1