Use shutil instead of os.system('cp -r') in LocalConnector upload/download#19
Open
rayair250-droid wants to merge 1 commit into
Open
Use shutil instead of os.system('cp -r') in LocalConnector upload/download#19rayair250-droid wants to merge 1 commit into
rayair250-droid wants to merge 1 commit into
Conversation
…nload The local upload/download methods shelled out to 'cp -r' via os.system with f-string-interpolated paths, which breaks on paths containing spaces or shell metacharacters (and is Unix-only). Replace with shutil.copytree/copy2, handling both file and directory sources, so copies are shell-free, robust to special characters, and portable.
Contributor
|
Thank you for catching this. The choice of cp -r over Python's shutil was because cp -r has specific, built-in logic handling target directory existence. I couldn't guarantee shutil would mirror that exact behavior, so I opted for the simpler approach. Since this feature is now deprecated, I prefer a straightforward fix: wrap both the source and target paths using shlex.quote() when generating the cp command. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
LocalConnector.uploadanddownloadmethods copy files by shelling out tocp -rviaos.systemwith f-string-interpolated paths:This breaks whenever
from_path/to_dircontain spaces or shell metacharacters (a real case for user-provided working directories), is a shell-injection surface, and is Unix-only.This replaces both with
shutil, handling file and directory sources (ascp -rdoes):Shell-free, robust to special characters, and portable. Return value is unchanged. Verified the copy logic on both a file (with a space in its name) and a directory.