Skip to content

Replace the shared vfs_cache temp dir in DefaultFileReplicator - #775

Open
rootvector2 wants to merge 2 commits into
apache:masterfrom
rootvector2:replicator-temp-dir
Open

Replace the shared vfs_cache temp dir in DefaultFileReplicator#775
rootvector2 wants to merge 2 commits into
apache:masterfrom
rootvector2:replicator-temp-dir

Conversation

@rootvector2

Copy link
Copy Markdown
Contributor

DefaultFileReplicator pins its replica directory to <java.io.tmpdir>/vfs_cache, a fixed name under a directory every local user can write to, and leaves creation to mkdirs under the default umask. Everything the library replicates lands there world-readable, including the whole remote archive that ZipFileSystem and TarFileSystem copy through replicateFile before opening it. Because the name is known up front, another local user can also pre-create vfs_cache as a symlink and collect the replicas, or swap a replicated jar before VFSClassLoader reads it; found while auditing where the library writes files on the caller's behalf.

Allocate the directory with Files.createTempDirectory on first use instead. It picks an unguessable name and creates it rwx------ on POSIX file systems in a single step, so there is no window between creation and a chmod. Keeping this in the replicator puts the guarantee where the directory's lifetime is already managed, and an application that wants a specific location still passes one to the DefaultFileReplicator(File) constructor.

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

The replica directory was pinned to <java.io.tmpdir>/vfs_cache and left to mkdirs under the default umask, so replicated files landed world-readable in a directory any local user can predict and pre-create. Allocate it with Files.createTempDirectory on first use, which picks an unguessable name and creates it rwx------ on POSIX file systems in one step.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Hardens DefaultFileReplicator’s replica-directory handling by replacing the predictable, shared <java.io.tmpdir>/vfs_cache directory with a per-instance directory created via Files.createTempDirectory, reducing the risk of local symlink/pre-creation attacks and improving default permissions on POSIX file systems.

Changes:

  • Lazily allocates the replica directory using Files.createTempDirectory("vfs_cache") instead of a fixed temp path.
  • Moves temp-dir creation behind a synchronized accessor and keeps init() focused on fileCount initialization.
  • Adds unit tests asserting the temp directory is not the shared vfs_cache name and is rwx------ on POSIX.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
commons-vfs2/src/main/java/org/apache/commons/vfs2/impl/DefaultFileReplicator.java Replaces fixed vfs_cache temp directory with a lazily-created createTempDirectory location.
commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/DefaultFileReplicatorTest.java Adds tests ensuring the temp dir isn’t a shared/predictable name and is owner-only on POSIX.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +128 to +129
protected File createAndAddFile(final File parent, final String baseName) throws FileSystemException {
final File file = createFile(tempDir, baseName);
final File file = createFile(getTempDir(), baseName);

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.

good catch, createAndAddFile now uses parent as passed in and the javadoc no longer claims it is ignored. allocateFile already supplies the temp dir, so behavior is unchanged.

@garydgregory garydgregory left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@rootvector2 Please review Copilot's comment.
TY!

createAndAddFile ignored its parent argument and always used the temp
directory, making the protected API misleading. Use parent as passed in;
allocateFile already supplies the temp directory, so behavior is unchanged.
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.

3 participants