deal with illegal filenames on windows

This commit is contained in:
ed 2020-05-06 23:06:26 +02:00
parent a1ecef8020
commit 15d5859750
5 changed files with 35 additions and 8 deletions

View file

@ -4,7 +4,6 @@ from __future__ import print_function, unicode_literals
import traceback import traceback
from .__init__ import PY2
from .util import Pebkac, Queue from .util import Pebkac, Queue

View file

@ -420,9 +420,11 @@ class HttpCli(object):
vfs, rem = self.conn.auth.vfs.get(self.vpath, self.uname, False, True) vfs, rem = self.conn.auth.vfs.get(self.vpath, self.uname, False, True)
self._assert_safe_rem(rem) self._assert_safe_rem(rem)
sanitized = sanitize_fn(new_dir)
if not nullwrite: if not nullwrite:
fdir = os.path.join(vfs.realpath, rem) fdir = os.path.join(vfs.realpath, rem)
fn = os.path.join(fdir, sanitize_fn(new_dir)) fn = os.path.join(fdir, sanitized)
if not os.path.isdir(fsenc(fdir)): if not os.path.isdir(fsenc(fdir)):
raise Pebkac(500, "parent folder does not exist") raise Pebkac(500, "parent folder does not exist")
@ -435,7 +437,7 @@ class HttpCli(object):
except: except:
raise Pebkac(500, "mkdir failed, check the logs") raise Pebkac(500, "mkdir failed, check the logs")
vpath = "{}/{}".format(self.vpath, new_dir).lstrip("/") vpath = "{}/{}".format(self.vpath, sanitized).lstrip("/")
html = self.conn.tpl_msg.render( html = self.conn.tpl_msg.render(
h2='<a href="/{}">go to /{}</a>'.format( h2='<a href="/{}">go to /{}</a>'.format(
quotep(vpath), html_escape(vpath, quote=False) quotep(vpath), html_escape(vpath, quote=False)
@ -457,9 +459,11 @@ class HttpCli(object):
if not new_file.endswith(".md"): if not new_file.endswith(".md"):
new_file += ".md" new_file += ".md"
sanitized = sanitize_fn(new_file)
if not nullwrite: if not nullwrite:
fdir = os.path.join(vfs.realpath, rem) fdir = os.path.join(vfs.realpath, rem)
fn = os.path.join(fdir, sanitize_fn(new_file)) fn = os.path.join(fdir, sanitized)
if os.path.exists(fsenc(fn)): if os.path.exists(fsenc(fn)):
raise Pebkac(500, "that file exists already") raise Pebkac(500, "that file exists already")
@ -467,7 +471,7 @@ class HttpCli(object):
with open(fsenc(fn), "wb") as f: with open(fsenc(fn), "wb") as f:
f.write(b"`GRUNNUR`\n") f.write(b"`GRUNNUR`\n")
vpath = "{}/{}".format(self.vpath, new_file).lstrip("/") vpath = "{}/{}".format(self.vpath, sanitized).lstrip("/")
html = self.conn.tpl_msg.render( html = self.conn.tpl_msg.render(
h2='<a href="/{}?edit">go to /{}?edit</a>'.format( h2='<a href="/{}?edit">go to /{}?edit</a>'.format(
quotep(vpath), html_escape(vpath, quote=False) quotep(vpath), html_escape(vpath, quote=False)

View file

@ -24,7 +24,7 @@ class TcpSrv(object):
ip = "127.0.0.1" ip = "127.0.0.1"
eps = {ip: "local only"} eps = {ip: "local only"}
if self.args.i != ip: if self.args.i != ip:
eps = self.detect_interfaces(self.args.i) or eps eps = self.detect_interfaces(self.args.i) or {self.args.i: "external"}
for ip, desc in sorted(eps.items(), key=lambda x: x[1]): for ip, desc in sorted(eps.items(), key=lambda x: x[1]):
self.log( self.log(

View file

@ -13,7 +13,7 @@ import threading
from copy import deepcopy from copy import deepcopy
from .__init__ import WINDOWS from .__init__ import WINDOWS
from .util import Pebkac, Queue, fsenc from .util import Pebkac, Queue, fsenc, sanitize_fn
class Up2k(object): class Up2k(object):
@ -48,6 +48,7 @@ class Up2k(object):
self.r_hash = re.compile("^[0-9a-zA-Z_-]{43}$") self.r_hash = re.compile("^[0-9a-zA-Z_-]{43}$")
def handle_json(self, cj): def handle_json(self, cj):
cj["name"] = sanitize_fn(cj["name"])
wark = self._get_wark(cj) wark = self._get_wark(cj)
now = time.time() now = time.time()
with self.mutex: with self.mutex:

View file

@ -356,7 +356,30 @@ def undot(path):
def sanitize_fn(fn): def sanitize_fn(fn):
return fn.replace("\\", "/").split("/")[-1].strip() fn = fn.replace("\\", "/").split("/")[-1]
if WINDOWS:
for bad, good in [
["<", ""],
[">", ""],
[":", ""],
['"', ""],
["/", ""],
["\\", ""],
["|", ""],
["?", ""],
["*", ""],
]:
fn = fn.replace(bad, good)
bad = ["con", "prn", "aux", "nul"]
for n in range(1, 10):
bad += "com{0} lpt{0}".format(n).split(" ")
if fn.lower() in bad:
fn = "_" + fn
return fn.strip()
def exclude_dotfiles(filepaths): def exclude_dotfiles(filepaths):