def safe_id(identifier): if not SAFENAME.match(identifier): identifier = u'sha1-' + sha1(identifier.encode('utf-8')).hexdigest() elif SHA1_ID.match(identifier): # could collide with "safe" id and should never happen anyway raise ValueError("illegal doc id: {!r}".format(identifier)) return identifier
def safejoin(root, subpath): if not SAFENAME.match(subpath): raise BadName(u"unsafe path name: %r" % subpath) path = join(root, subpath) if commonprefix([root + sep, path]) != root + sep: raise BadName(u"invalid relative path: %r" % subpath) return path
def safepath(path): if (path.startswith(("/", ".")) or "/../" in path or path.endswith("/..") or not SAFENAME.match(path)): raise BadName("unsafe path name: %r" % path) return path
def safepath(path): if (path.startswith(("/", ".")) or "/../" in path or path.endswith("/..") or not SAFENAME.match(path)): raise BadName(u"unsafe path name: %r" % path) return path
def safejoin(root, subpath): """Join root to subpath ensuring that the result is actually inside root """ root = realpath(root) if not SAFENAME.match(subpath): raise BadName(u"unsafe path name: %r" % subpath) path = realpath(join(root, subpath)) if commonprefix([root + sep, path]) != root + sep: raise BadName(u"invalid relative path: %r" % subpath) return path