Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions notebook/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,10 @@ def rename_file(self, old_path, new_path):
if new_path == old_path:
return

# Perform path validation prior to converting to os-specific value since this
# is still relative to root_dir.
self._validate_path(new_path)

new_os_path = self._get_os_path(new_path)
old_os_path = self._get_os_path(old_path)

Expand Down Expand Up @@ -586,3 +590,22 @@ def get_kernel_path(self, path, model=None):
else:
parent_dir = ''
return parent_dir

@staticmethod
def _validate_path(path):
"""Checks if the path contains invalid characters relative to the current platform"""

if sys.platform == 'win32':
# On Windows systems, we MUST disallow colons otherwise an Alternative Data Stream will
# be created and confusion will reign! (See https://github.com/jupyter/notebook/issues/5190)
# Go ahead and add other invalid (and non-path-separator) characters here as well so there's
# consistent behavior - although all others will result in '[Errno 22]Invalid Argument' errors.
invalid_chars = (':', '>', '<', '*', '?', '|', '"')
else:
# On non-windows systems, allow the underlying file creation to perform enforcement when appropriate
invalid_chars = ()

for char in invalid_chars:
if char in path:
raise web.HTTPError(400, u"Path '{}' contains invalid characters {} relative to its platform. "
Copy link
Member

Choose a reason for hiding this comment

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

How about Path '{}' contains characters that are invalid for the filesystem. Path names on this filesystem cannot contain any of the following characters: {}.? I'm trying to make the message simpler, though it seems a little less actionable too (though in the validation method, it's not clear exactly who is calling us and what the recovery procedure should be...)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you - that reads much more cleanly. Here's a screen shot with the update:
Screen Shot 2020-01-28 at 3 25 12 PM

Previously, I was taking advantage of the parenthesis that sets print with. That could now be argued as unnecessary. If there's an easy pythonic way to remove those, I can do that - otherwise it seems okay to me.

Copy link
Member

Choose a reason for hiding this comment

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

You could just define it as a string instead of a tuple.

Copy link
Member

Choose a reason for hiding this comment

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

invalid_chars = ':><*?|"'

u"Please edit the path and re-submit the request.".format(path, invalid_chars))
5 changes: 5 additions & 0 deletions notebook/services/contents/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ def test_rename(self):
# Fetching the notebook under the new name is successful
assert isinstance(cm.get("changed_path"), dict)

# Test validation. Currently, only Windows has a non-empty set of invalid characters
if sys.platform == 'win32' and isinstance(cm, FileContentsManager):
with self.assertRaisesHTTPError(400):
cm.rename("changed_path", "prevent: in name")

# Ported tests on nested directory renaming from pgcontents
all_dirs = ['foo', 'bar', 'foo/bar', 'foo/bar/foo', 'foo/bar/foo/bar']
unchanged_dirs = all_dirs[:2]
Expand Down