[security/debug_source_code] add security mechanism to only read whitelisted files draft
authorLaurent Peuch <cortex@worlddomination.be>
Thu, 26 Sep 2019 06:27:53 +0200
changeset 13433 18deb53bb418
parent 13432 20c33beec0e8
child 13434 e6150cd580e5
[security/debug_source_code] add security mechanism to only read whitelisted files
cubicweb/pyramid/debug_source_code.py
--- a/cubicweb/pyramid/debug_source_code.py	Fri Sep 27 06:51:29 2019 +0200
+++ b/cubicweb/pyramid/debug_source_code.py	Thu Sep 26 06:27:53 2019 +0200
@@ -32,6 +32,8 @@
 
 DEBUG_DISPLAY_SOURCE_CODE_PATH = '_debug_display_source_code'
 
+FILES_WHITE_LIST = set()
+
 
 def source_code_url(object_or_class):
     if object_or_class is None:
@@ -46,6 +48,8 @@
         logging.debug("Error while trying to source code of '%s'" % object_or_class)
         return ""
 
+    FILES_WHITE_LIST.add(file_path)
+
     try:
         source_code, line = inspect.getsourcelines(object_or_class)
     except OSError:  # when we couldn't the source code/line
@@ -77,6 +81,10 @@
     if not os.path.exists(source_code_file):
         return Response("Error: file '%s' doesn't exist on the filesystem." % source_code_file)
 
+    # security
+    if source_code_file not in FILES_WHITE_LIST:
+        return Response("Error: can't access file")
+
     try:
         content = open(source_code_file, "r").read()
     except Exception as e: