소스 검색

Improve URI sanitation
The old implementation failed to sanitize URIs
like ".", "..", "../.." or "//"

Unrud 10 년 전
부모
커밋
ee095a463d
1개의 변경된 파일8개의 추가작업 그리고 3개의 파일을 삭제
  1. 8 3
      radicale/__init__.py

+ 8 - 3
radicale/__init__.py

@@ -177,12 +177,17 @@ class Application(object):
 
     @staticmethod
     def sanitize_uri(uri):
-        """Unquote and remove /../ to prevent access to other data."""
+        """Unquote and make absolute to prevent access to other data."""
         uri = unquote(uri)
         trailing_slash = "/" if uri.endswith("/") else ""
         uri = posixpath.normpath(uri)
-        trailing_slash = "" if uri == "/" else trailing_slash
-        return uri + trailing_slash
+        new_uri = "/"
+        for part in uri.split("/"):
+            if not part or part in (".", ".."):
+                continue
+            new_uri = posixpath.join(new_uri, part)
+        trailing_slash = "" if new_uri.endswith("/") else trailing_slash
+        return new_uri + trailing_slash
 
     def collect_allowed_items(self, items, user):
         """Get items from request that user is allowed to access."""