Skip to content
Permalink
Browse files Browse the repository at this point in the history
transfers: Always remove existing files before beginning a transfer.
The user must approve overwriting, or they've must have allowed it
via preferences. Either way, if we've made it this far, we can get
rid of existing files before starting the transfer.

This eliminates the risk of any existing symbolic directory links
potentially being followed outside of the download directory, which
would not be detected by 5244c33.
  • Loading branch information
mtwebster committed Sep 20, 2022
1 parent f4907ef commit 8bfd2f8
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/transfers.py
Expand Up @@ -180,18 +180,17 @@ def __init__(self, op):
self.current_mtime = 0
self.current_mtime_usec = 0

if op.existing:
for name in op.top_dir_basenames:
try:
path = os.path.join(self.save_path, name)
if os.path.isdir(path): # file not found is ok
shutil.rmtree(path)
else:
os.remove(path)
except FileNotFoundError:
pass
except Exception as e:
logging.warning("Problem removing existing files. Transfer may not succeed: %s" % e)
for name in op.top_dir_basenames:
try:
path = os.path.join(self.save_path, name)
if os.path.isdir(path): # file not found is ok
shutil.rmtree(path)
else:
os.remove(path)
except FileNotFoundError:
pass
except Exception as e:
logging.warning("Problem removing existing files. Transfer may not succeed: %s" % e)

# We write files top-down. If we're preserving permissions and we receive
# a folder in some hierarchy that is not writable, we won't be able to create
Expand Down Expand Up @@ -222,8 +221,7 @@ def receive_data(self, s):
make_symbolic_link(self.op, path, s.symlink_target)
else:
if self.current_stream == None:
flags = Gio.FileCreateFlags.REPLACE_DESTINATION
self.current_stream = self.current_gfile.replace(None, False, flags, None)
self.current_stream = self.current_gfile.create(Gio.FileCreateFlags.NONE, None)

if not s.chunk:
return
Expand Down

0 comments on commit 8bfd2f8

Please sign in to comment.