Skip to content

fix: SSHConfig: atomically write ssh config #511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: cj/config-ssh/handle-malformed
Choose a base branch
from

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented May 20, 2025

Relates to #504

To avoid race conditions, write to a tempfile and then rename.

Smoke-tested by:

  • Setting immutable flag on /Users/cian/.ssh/config (sudo chflags uchg $PATH) and validating that the rename failed:
    Screenshot 2025-05-21 at 20 13 14

  • Setting immutable flag on /Users/cian/.ssh and validating that writing the temp file failed:
    Screenshot 2025-05-21 at 20 13 56

  • Once immutable flag removed (sudo chflags nouchg $PATH), connecting to workspace is successful.

@johnstcn johnstcn requested review from mafredri and code-asher May 20, 2025 16:42
@johnstcn johnstcn self-assigned this May 20, 2025
@johnstcn johnstcn force-pushed the cj/config-ssh/atomic-write branch from 9b53e6c to 2c491b7 Compare May 20, 2025 16:45
@johnstcn johnstcn force-pushed the cj/config-ssh/handle-malformed branch from 09777c5 to eda19a2 Compare May 20, 2025 17:03
@johnstcn johnstcn force-pushed the cj/config-ssh/atomic-write branch from 2c491b7 to 5277656 Compare May 20, 2025 17:03
@johnstcn johnstcn requested a review from bcpeinhardt May 20, 2025 17:04
src/sshConfig.ts Outdated
return this.fileSystem.writeFile(this.filePath, this.getRaw(), {
const randSuffix = Math.random().toString(36).substring(8)
const tempFilePath = `${this.filePath}.${randSuffix}`
await this.fileSystem.writeFile(tempFilePath, this.getRaw(), {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am probably overthinking this, but if there is somehow a file with this suffix already, would we want to error instead of overwrite? We could use flag: "wx" on the writeFile call to error if the path already exists.

Vanishingly unlikely, but maybe there could be a freak collision where a user has a ~/.ssh/config.conf and the random suffix was also conf.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We could just write to /tmp
  2. We could instead name it something like config.vscode-coder.${timestamp}.${randSuffix} to minimize the likelihood of collisions

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like 2 personally because I think it gives us a better chance to get the temp files for debugging, with /tmp they could be long gone.

But I could see an argument for /tmp to avoid "clutter".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a ULID? It would be sufficiently random and you could sort them by time to determine write order when debugging.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to guarantee atomicity, same folder is best (/tmp for instance could be on another filesystem).

To hide clutter in the case of error, I'd suggest a . prefix on the filename, perhaps .tmp.[filename].[rand]. At this point guaranteeing uniqueness beyond this would seem like overkill.

I.e. my suggestion is just /home/user/.ssh/config -> /home/user/.ssh/.tmp.config.XXXXXX.

One use-case that won't work here is if /home/user/.ssh/config is bind-mounted. In that situation we either error out or try to write the file directly. Erroring is obviously safest so perhaps we go with that.

Copy link
Member Author

@johnstcn johnstcn May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a ULID?

I like the idea in theory, but it would be another import.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to write down one more thing, we should also respect the existing file modes and permissions and make sure they're copied over.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I've also adjusted the tempfile naming convention to ~/.ssh/.config.vscode-coder-tmp.abc123 to make it abundantly clear that

  1. This refers to ~/.ssh/config
  2. It was created by vscode-coder

@johnstcn johnstcn force-pushed the cj/config-ssh/atomic-write branch from 5277656 to 698b334 Compare May 21, 2025 10:59
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@johnstcn johnstcn force-pushed the cj/config-ssh/atomic-write branch from 71e2751 to 8ac1e78 Compare May 21, 2025 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants