From: karim hamidou Date: Tue, 17 Jul 2012 07:12:45 +0000 (+0200) Subject: added a function to secure a path. Converted the X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=352c5c1431b9c2534eaff2ee8322474f817e893f;p=cagibi.git added a function to secure a path. Converted the code to use it --- diff --git a/cagibi/cagibi.py b/cagibi/cagibi.py index c9f8f4b..d44dc92 100644 --- a/cagibi/cagibi.py +++ b/cagibi/cagibi.py @@ -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() diff --git a/cagibi/server.py b/cagibi/server.py index fbc208b..0f793dd 100644 --- a/cagibi/server.py +++ b/cagibi/server.py @@ -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//data') def file_data(filename): + """Return the contents of the file""" return static_file(filename, root=cagibi_folder) +@get('/files//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//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//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 index 0000000..ad0d38c --- /dev/null +++ b/cagibi/util.py @@ -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)