From 585536c5dc86e1cc64555c53fb4fb5b5a9457762 Mon Sep 17 00:00:00 2001 From: Josh Schneier Date: Sat, 26 Aug 2023 16:46:57 -0400 Subject: [PATCH] [dropbox] fix name mangling by replacing lstrip with removeprefix (#1279) --- storages/backends/dropbox.py | 8 +++++++- tests/test_dropbox.py | 8 +++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/storages/backends/dropbox.py b/storages/backends/dropbox.py index 407ad2d3..27b8d2d7 100644 --- a/storages/backends/dropbox.py +++ b/storages/backends/dropbox.py @@ -33,6 +33,12 @@ class DropboxStorageException(Exception): DropBoxStorageException = DropboxStorageException +def removeprefix(prefix, name): + if name.startswith(prefix): + name = name[len(prefix):] + return name + + class DropboxFile(File): def __init__(self, name, storage): self.name = name @@ -172,7 +178,7 @@ def _save(self, name, content): content.close() # .save() validates the filename isn't absolute but Dropbox requires an absolute filename. # Work with the absolute name internally but strip it off before passing up-the-chain. - return name.lstrip(self.root_path) + return removeprefix(self.root_path, name).lstrip('/') def _chunked_upload(self, content, dest_path): upload_session = self.client.files_upload_session_start( diff --git a/tests/test_dropbox.py b/tests/test_dropbox.py index c0e8ac92..7ba46df9 100644 --- a/tests/test_dropbox.py +++ b/tests/test_dropbox.py @@ -197,9 +197,11 @@ def test_jailed(self, *args): @mock.patch('dropbox.Dropbox.files_upload', return_value='foo') @mock.patch('dropbox.Dropbox.files_get_metadata', return_value=None) def test_saves(self, *args): - self.storage = dropbox.DropboxStorage('foo', root_path='/bar') - name = self.storage.save('xyz', File(io.BytesIO(b'abc'), 'def')) - self.assertEqual(name, 'xyz') + self.storage = dropbox.DropboxStorage('foo', root_path='/app.qoo.foo/') + for filename in ['xyz', 'quark']: + with self.subTest(filename=filename): + name = self.storage.save(filename, File(io.BytesIO(b'abc'), 'def')) + self.assertEqual(name, filename) def test_suspicious(self, *args): self.storage = dropbox.DropboxStorage('foo', root_path='/bar')