]> khamidou — GitHub mirrors - cagibi.git/commitdiff
added a function to secure a path. Converted the
authorkarim hamidou <khamidou@gmail.com>
Tue, 17 Jul 2012 07:12:45 +0000 (09:12 +0200)
committerkarim hamidou <khamidou@gmail.com>
Tue, 17 Jul 2012 07:12:45 +0000 (09:12 +0200)
code to use it

cagibi/cagibi.py
cagibi/server.py
cagibi/util.py [new file with mode: 0644]

index c9f8f4bb5cd7abac136e39b7d233f80fb80b861a..d44dc92a1ee62d0f1fcb1e5de0898e004f091102 100644 (file)
@@ -4,6 +4,7 @@
 from watch import FileWatcher
 from rsync import *
 from config import load_config, save_config
+from util import secure_path
 import urllib, urllib2
 import json
 import os
@@ -12,11 +13,23 @@ client_config = {}
 server_url = "http://localhost:8080/"
 cagibi_folder = "."
 
-def changesHandler(modified, added, removed):
+
+def upload_local_changes(modified, added, removed):
+    pattern = "%s/files/%s/hashes" % server_url
+
     for file in modified:
-        fd = open(file, "rb")
-        hashes = blockchecksums(fd)
-        print hashes
+        url = pattern % file
+        fd = urllib2.urlopen(url)
+        hashes = json.load(fd)
+        patchedfile = open(secure_path(cagibi_folder, file), "rb")
+        deltas = rsyncdelta(patchedfile, hashes)
+        patchedfile.close()
+
+        # Send the deltas to the server.
+        post_data = {}
+        post_data["deltas"] = json.dumps(deltas)
+        post_string = urllib.urlencode(post_data)
+        fd = urllib2.urlopen(url, post_string)
 
 def checkout_upstream_changes():
     """Checkout changes on the server"""
@@ -34,7 +47,7 @@ def checkout_upstream_changes():
             local_files[file] = {}
             local_files[file]["rev"] = server_files[file]["rev"]
             url = "%s/files/%s/data" % (server_url, file) 
-            urllib.urlretrieve(url, os.path.join(cagibi_folder, file))
+            urllib.urlretrieve(url, secure_path(cagibi_folder, file))
             print "Retrieved %s" % file
             modified = True
 
@@ -42,7 +55,7 @@ def checkout_upstream_changes():
             if server_files[file]["rev"] > local_files[file]["rev"]:
                 # Get it too, but using the rsync algo
 
-                unpatched = open(os.path.join(cagibi_folder, file).encode('ascii', 'ignore'), "rb")
+                unpatched = open(secure_path(cagibi_folder, file).encode('ascii', 'ignore'), "rb")
                 hashes = blockchecksums(unpatched) 
                 json_hashes = json.dumps(hashes)
                 post_data = {}
@@ -57,9 +70,11 @@ def checkout_upstream_changes():
                 save_to = os.tmpfile()
                 patchstream(unpatched, save_to, json_response)
                 unpatched.close()
-                os.unlink(os.path.join(cagibi_folder, file))
+                os.unlink(secure_path(cagibi_folder, file))
                 save_to.seek(0)
-                file_copy = open(os.path.join(cagibi_folder, file).encode('ascii', 'ignore'), "w+b")
+                
+                # FIXME: rename instead of copying ?
+                file_copy = open(secure_path(cagibi_folder, file).encode('ascii', 'ignore'), "w+b")
                 file_copy.write(save_to.read())
                 file_copy.close()
                 save_to.close()
@@ -80,6 +95,6 @@ if __name__ == "__main__":
         cagibi_folder = client_config["folder"]
 
     fw = FileWatcher()
-    fw.addHandler(changesHandler)
+    fw.addHandler(upload_local_changes)
     checkout_upstream_changes()
     fw.watch()
index fbc208b526e255d994f0b5a588ca8c0eca9bf4f1..0f793dd407e1d1293dd36a64bd8adabb2a40ef33 100644 (file)
@@ -16,7 +16,7 @@ def filelist():
 
     for file in os.listdir(cagibi_folder):
        dir[file] = {}
-       dir[file]["mtime"] = os.path.getmtime(os.path.join(cagibi_folder, file))
+       dir[file]["mtime"] = os.path.getmtime(secure_path(cagibi_folder, file))
        if file in files_info:
            dir[file]["rev"] = files_info[file]["rev"]
 
@@ -33,7 +33,7 @@ def create_file(filename):
     
     # FIXME: possible race condition
     if not os.path.exists(filename) and filename not in files_info:
-        fd = open(os.path.join(cagibi_folder, filename), "wb")
+        fd = open(secure_path(cagibi_folder, filename), "wb")
         fd.write(contents)
         fd.close()
         files_info[filename] = {"rev": 1}
@@ -49,7 +49,7 @@ def delete_file(filename):
     
     # FIXME: possible race condition
     if os.path.exists(filename) and filename in files_info:
-        os.remove(os.path.join(cagibi_folder, filename))
+        os.remove(secure_path(cagibi_folder, filename))
         del files_info[filename]
         save_config(files_info, filename="files.json")
         return "Ok."
@@ -62,13 +62,45 @@ def file_info(filename):
 
 @get('/files/<filename>/data')
 def file_data(filename):
+    """Return the contents of the file"""
     return static_file(filename, root=cagibi_folder)
 
+@get('/files/<filename>/hashes')
+def file_hashes(filename):
+    """Return the hashes of a file"""
+    unpatched = open(secure_path(cagibi_folder, filename), "rb")
+    hashes = blockchecksums(unpatched)
+    unpatched.close()
+    return json.dumps(hashes)
+
+@post('/files/<filename>/hashes')
+def update_file_hashes(filename):
+    """Updates a file using the deltas received by a client"""
+    deltas = json.loads(request.forms.get("hashes"))
+
+    unpatched = open(secure_path(cagibi_folder, filename), "rb")
+    save_to = os.tmpfile()
+    patchstream(unpatched, save_to, json_response)
+    unpatched.close()
+    os.unlink(secure_path(cagibi_folder, filename))
+    save_to.seek(0)
+
+    # FIXME: rename instead of copying ?
+    file_copy = open(secure_path(cagibi_folder, filename).encode('ascii', 'ignore'), "w+b")
+    file_copy.write(save_to.read())
+    file_copy.close()
+    save_to.close()
+    
+    files_info[filename] = {"rev": 1}
+    save_config(files_info, filename="files.json")
+
+    pass
+
 @post('/files/<filename>/deltas')
 def return_deltas(filename):
     """return the deltas corresponding to the file. The client must send in its request the hashes of the file"""  
     hashes = json.loads(request.forms.get("hashes"))
-    patchedfile = open(os.path.join(cagibi_folder, filename), "rb")
+    patchedfile = open(secure_path(cagibi_folder, filename), "rb")
     deltas = rsyncdelta(patchedfile, hashes)
     patchedfile.close()
 
diff --git a/cagibi/util.py b/cagibi/util.py
new file mode 100644 (file)
index 0000000..ad0d38c
--- /dev/null
@@ -0,0 +1,6 @@
+import os
+
+def secure_path(folder_path, file):
+    """Make sure that a file/path is located in the folder "folder" """
+    #FIXME: make it work for directories.
+    return folder_path + os.path.sep + os.path.basename(file)