Skip to content

prevent tunnel sockets from overwriting existing files - #1141

Open
shizuku198411 wants to merge 6 commits into
canonical:mainfrom
shizuku198411:fix/tunnel-prevent-file-overwrite
Open

shizuku198411 wants to merge 6 commits into
canonical:mainfrom
shizuku198411:fix/tunnel-prevent-file-overwrite

Conversation

@shizuku198411

Copy link
Copy Markdown
Contributor

Description

Fixes #1140

Prevent the tunnel interface from overwriting existing files when creating a Unix socket.

Previously, Workshop could replace an existing regular file with a Unix socket during tunnel setup, potentially causing data loss.

This PR adds an os.Lstat() check to reject socket creation if the target path already exists, regardless of its file type.

Changes:

  • Reject existing paths, including regular files, directories, symlinks, and Unix sockets.
  • Add unit tests covering each case and nonexistent paths.
  • Return a clear error without modifying existing files.

Verification:

  • Confirmed that existing regular files are no longer overwritten.
  • Confirmed that Unix sockets are created successfully when the target path does not exist.
  • Verified that start, stop, and refresh continue to work as expected.

Self-review quick check

  • Make decisions that cost a lot to reverse explicit in the PR description.
  • Avoid nested conditions.
  • Delete dead code and redundant comments.
  • Normalise symmetries by sticking to doing identical things identically.
  • Check that coupled code elements, files, and directories are adjacent. For example, test data is stored as close as possible to a test.
  • Put variable declaration and initialisation together.
  • Divide large expressions into digestable and self-explanatory ones. Use multiple variables if required.
  • Put a blank line between two logically different chunks of code.
  • Follow the style guide for new error messages.

Docs

  • I confirm the PR has no implications for documentation.

@jonathan-conder

Copy link
Copy Markdown
Contributor

I think we should overwrite the file if it's another socket; it could be left over from another session that closed abruptly. But if it's a regular file we should probably avoid deleting it, yes.

@shizuku198411

Copy link
Copy Markdown
Contributor Author

I agree, allowing existing sockets to be overwritten makes sense.
I've updated the commit accordingly, while ensuring that regular files are protected from being overwritten.

Comment thread internal/interfaces/builtin/tunnel.go Outdated

if realDir == user.HomeDir || strings.HasPrefix(realDir, user.HomeDir+"/") {
return nil
return checkSocketPath(listen.Address)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this doesn't really fit into the purpose of authorizePath, can we move it up to the call site?

It also only applies to files on the host; is it possible to apply the same logic to files inside the workshop?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the review. you're right, the file check doesn't belong in authorizePath(). I've moved it to the call site, where it now runs as a separate validation step.

I also confirmed that existing files can be overwritten inside the workshop.
However, since this involves a manually established connection, it follows a different code path.
I'll look into the appropriate place to add the same protection.

@jonathan-conder jonathan-conder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe we can move the original checkSocketPath into the backend Setup as well? similar to how mounts create directories on the host and in the workshop.

I made changes here recently so there may be some merge conflicts, sorry!

@shizuku198411
shizuku198411 force-pushed the fix/tunnel-prevent-file-overwrite branch from f5bfc75 to d37daad Compare November 23, 2026 23:06
@shizuku198411

Copy link
Copy Markdown
Contributor Author

@jonathan-conder
I overlooked that both mount directions go through the backend Setup. Thank you for pointing that out!

I’ve moved the host-side check into the backend Setup as well, so both host and workshop paths are now protected through the same validation flow.

This branch has not been deployed

No deployments
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.

Tunnel interface overwrites existing files when creating Unix sockets

2 participants