Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SVCS-294] Add rename validation #213

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
20 changes: 20 additions & 0 deletions waterbutler/core/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class BaseProvider(metaclass=abc.ABCMeta):
"""

BASE_URL = None
FORBIDDEN_FILENAME_CHARS = []
FORBIDDEN_FOLDERNAME_CHARS = ['/']
Copy link
Contributor

@NyanHelsing NyanHelsing Jul 16, 2018

Choose a reason for hiding this comment

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

Make this an abc property on the BaseProvider


def __init__(self, auth: dict,
credentials: dict,
Expand Down Expand Up @@ -410,6 +412,24 @@ async def handle_naming(self,

return dest_path

def validate_rename(self, name, folder):
"""Some providers don't allow files or folders to be renamed with slashes or special
characters this function weeds out those illegal names before we make the request
"""
if not name:
raise exceptions.InvalidParameters('Rename parameter is required for renaming')

elif folder and any((char in self.FORBIDDEN_FOLDERNAME_CHARS) for char in name):
bad_chars = ', '.join([char for char in self.FORBIDDEN_FOLDERNAME_CHARS if char in name])
raise exceptions.InvalidParameters("New folder name contains forbidden character(s) {}".format(bad_chars))

elif not folder and any((char in self.FORBIDDEN_FILENAME_CHARS) for char in name):
bad_chars = ', '.join([char for char in self.FORBIDDEN_FILENAME_CHARS if char in name])
raise exceptions.InvalidParameters("New file name contains forbidden character(s) {}".format(bad_chars))

else:
return name

def can_intra_copy(self,
other: 'BaseProvider',
path: wb_path.WaterButlerPath=None) -> bool:
Expand Down
1 change: 1 addition & 0 deletions waterbutler/providers/dropbox/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class DropboxProvider(provider.BaseProvider):
Quirks: Dropbox paths are case-insensitive.
"""
NAME = 'dropbox'
FORBIDDEN_FILENAME_CHARS = ['/']
BASE_URL = provider_settings.BASE_URL

def __init__(self, auth, credentials, settings):
Expand Down
7 changes: 7 additions & 0 deletions waterbutler/server/api/v1/provider/movecopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@ async def move_or_copy(self):
self.path = await self.provider.validate_v1_path(self.path, **self.arguments)

if auth_action == 'rename': # 'rename' implies the file/folder does not change location

if not self.json.get('rename'):
raise exceptions.InvalidParameters('Rename is required for renaming')
self.provider.validate_rename(self.json.get('rename'), self.path.is_dir)

self.dest_auth = self.auth
self.dest_provider = self.provider
self.dest_path = self.path.parent
self.dest_resource = self.resource

provider_action = 'move'
else:
path = self.json.get('path', None)
if path is None:
Expand Down