Quellcode durchsuchen

HUE-920 [fb] Support for files and folders with special characters

Hashes are now not allowed on folder names
Enrico Berti vor 13 Jahren
Ursprung
Commit
5962e9c

+ 11 - 8
apps/filebrowser/src/filebrowser/templates/listdir_components.mako

@@ -542,7 +542,7 @@ from django.utils.translation import ugettext as _
 
             $("*[rel='tooltip']").tooltip({ placement: "bottom" });
             if (window.location.hash != null && window.location.hash.length > 1){
-                viewModel.targetPath("${url('filebrowser.views.view', path=urlencode('/'))}" + window.location.hash.substring(2));
+                viewModel.targetPath("${url('filebrowser.views.view', path=urlencode('/'))}" + stripHashes(window.location.hash.substring(2)));
             }
             viewModel.retrieveData();
 
@@ -564,14 +564,14 @@ from django.utils.translation import ugettext as _
             $("#hueBreadcrumbText").keyup(function(e){
                 if (e.keyCode == 13) {
                     var _el = $(this);
-                    viewModel.targetPath("${url('filebrowser.views.view', path=urlencode('/'))}" + _el.val().substring(1));
+                    viewModel.targetPath("${url('filebrowser.views.view', path=urlencode('/'))}" + stripHashes(_el.val().substring(1)));
                     viewModel.getStats(function (data) {
                         if (data.type != null && data.type == "file") {
                             location.href = data.url;
                             return false;
                         }
                         else {
-                            window.location.hash = _el.val();
+                            window.location.hash = stripHashes(_el.val());
                         }
                     });
                 }
@@ -596,7 +596,7 @@ from django.utils.translation import ugettext as _
                 var target = "";
                 var hash = window.location.hash.substring(1);
                 if (hash != null && hash != "") {
-                    target = "${url('filebrowser.views.view', path=urlencode('/'))}" + hash.substring(1);
+                    target = "${url('filebrowser.views.view', path=urlencode('/'))}" + stripHashes(hash.substring(1));
                 }
                 if (window.location.href.indexOf("#") == -1){
                     target = "${current_request_path}";
@@ -609,6 +609,9 @@ from django.utils.translation import ugettext as _
 
         });
 
+        function stripHashes(str) {
+          return str.replace(/#/gi, encodeURIComponent("#"));
+        }
 
         var Page = function (page) {
             if (page != null) {
@@ -656,7 +659,7 @@ from django.utils.translation import ugettext as _
                         // forcing root on empty breadcrumb url
                         this.url = "/";
                     }
-                    viewModel.targetPath("${url('filebrowser.views.view', path=urlencode('/'))}" + this.url);
+                    viewModel.targetPath("${url('filebrowser.views.view', path=urlencode('/'))}" + stripHashes(this.url));
                     window.location.hash = this.url;
                 }
             }
@@ -803,11 +806,11 @@ from django.utils.translation import ugettext as _
 
             self.viewFile = function (file) {
                 if (file.type == "dir") {
-                    self.targetPath("${url('filebrowser.views.view', path=urlencode('/'))}" + "." + file.path);
-                    window.location.hash = file.path;
+                    self.targetPath("${url('filebrowser.views.view', path=urlencode('/'))}" + "." + stripHashes(file.path));
+                    window.location.hash = stripHashes(file.path);
                 }
                 else {
-                    location.href = "${url('filebrowser.views.view', path=urlencode('/'))}" + file.path;
+                    location.href = "${url('filebrowser.views.view', path=urlencode('/'))}" + stripHashes(file.path);
                 }
             };
 

+ 4 - 2
apps/filebrowser/src/filebrowser/views.py

@@ -907,6 +907,8 @@ def generic_op(form_class, request, op, parameter_names, piggyback=None, templat
 def rename(request):
     def smart_rename(src_path, dest_path):
         """If dest_path doesn't have a directory specified, use same dir."""
+        if "#" in dest_path:
+          raise PopupException(_("Sorry, could not rename folder \"%s\" to \"%s\": Hashes are not allowed in filenames." % (src_path, dest_path)))
         if "/" not in dest_path:
             src_dir = os.path.dirname(src_path)
             dest_path = os.path.join(src_dir, dest_path)
@@ -919,8 +921,8 @@ def mkdir(request):
     def smart_mkdir(path, name):
         # Make sure only one directory is specified at a time.
         # No absolute directory specification allowed.
-        if posixpath.sep in name:
-            raise PopupException(_("Sorry, could not name folder \"%s\": Slashes are not allowed in filenames." % name))
+        if posixpath.sep in name or "#" in name:
+            raise PopupException(_("Sorry, could not name folder \"%s\": Slashes or hashes are not allowed in filenames." % name))
         request.fs.mkdir(os.path.join(path, name))
 
     return generic_op(MkDirForm, request, smart_mkdir, ["path", "name"], "path")

+ 3 - 1
apps/filebrowser/src/filebrowser/views_test.py

@@ -146,10 +146,12 @@ def test_mkdir_singledir():
     success_path = 'mkdir_singledir'
     path_absolute = '/mkdir_singledir'
     path_fail = 'fail/foo'
+    path_other_failure = 'fail#bar'
     prefix = '/tmp/test-filebrowser/'
     # Two of the following post requests should throw exceptions.
     # See https://issues.cloudera.org/browse/HUE-793.
     c.post('/filebrowser/mkdir', dict(path=prefix, name=path_fail))
+    c.post('/filebrowser/mkdir', dict(path=prefix, name=path_other_failure))
     c.post('/filebrowser/mkdir', dict(path=prefix, name=path_absolute))
     c.post('/filebrowser/mkdir', dict(path=prefix, name=success_path))
 
@@ -373,7 +375,7 @@ def test_listdir():
     orig_paths = [
       u'greek-Ελληνικά',
       u'chinese-漢語',
-      'listdir%20.,<>~`!@#$%^&()_-+="',
+      'listdir%20.,<>~`!@$%^&()_-+="',
     ]
 
     prefix = '/test-filebrowser/'